Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CultureNotFoundException when InvariantGlobalization is enabled #1213

Closed
wants to merge 3 commits into from

Conversation

ptupitsyn
Copy link

@ptupitsyn ptupitsyn commented Jun 25, 2022

Fixes #1126 .

Starting with .NET 6, when InvariantGlobalization is enabled, CultureInfo constructor throws CultureNotFoundException for all cultures except invariant: https://docs.microsoft.com/en-us/dotnet/core/compatibility/globalization/6.0/culture-creation-invariant-mode

  • Handle CultureNotFoundException when creating the default formatter.
  • Skip other cultures when there was an exception for en-US.

@dnfadmin
Copy link

dnfadmin commented Jun 25, 2022

CLA assistant check
All CLA requirements met.

@ThumbGen
Copy link

ThumbGen commented Dec 3, 2022

Any reason for not accepting this useful PR?

@gurustron
Copy link

@clairernovotny @MehdiK @hazzik can we please have this merged?

@ahmadalli
Copy link
Contributor

@clairernovotny @MehdiK @hazzik Would you please take a look at this PR if you've got the time?

@SimonCropp
Copy link
Collaborator

@ptupitsyn sorry for the delayed reply. can you rebase. and also add a test for this scenario

@ptupitsyn
Copy link
Author

@SimonCropp invariant globalization can only be enabled before the runtime starts: https://learn.microsoft.com/en-us/dotnet/core/runtime-config/globalization

I'm not sure how to test this scenario. Is it ok to spawn a new process?

# Conflicts:
#	src/Humanizer/Configuration/Configurator.cs
#	src/Humanizer/Localisation/Formatters/DefaultFormatter.cs
@SimonCropp
Copy link
Collaborator

@ptupitsyn i think the best way is to add a new test project with the following

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <InvariantGlobalization>true</InvariantGlobalization>
  </PropertyGroup>

</Project>

and add a few tests to it. enough to verify the change

@hazzik
Copy link
Member

hazzik commented Feb 19, 2024

Just a question: how to differentiate between culture not found due, for ex, misspelling, and globalization invariant?

@SimonCropp
Copy link
Collaborator

@hazzik perhaps it throws a different exception?

{
try
{
return new FormatterRegistry(new DefaultFormatter("en-US"), false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This try-catch does not guarantee that exception is expected at exactly new DefaultFormatter("en-US") and in particular at _culture = new(localeCode) part. It can be thrown anywhere inside the FormatterRegistry constructor which creates many more cultures.

I think it need to be localized to something like this:

CultureInfo GetDefaultCulture() {
  try {
    return new CultureInfo("en-US");
  } catch (CultureNotFoundException) {
    return CultureInfo.InvariantCulture;
  }
}

And then check if default culture is invariant and if so then skip all other initializers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The type initializer for 'Humanizer.Configuration.Configurator' threw an exception.
7 participants