-
Notifications
You must be signed in to change notification settings - Fork 382
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
System.Globalization.CultureNotFoundException UnitsNet.ToString() on system that Culture information is not available #1238
Comments
Thanks for the info. This is an oversight in our fallback mechanism. @angularsen I wonder if #1210 would be the best fix here. |
@tmilnthorp Yes I think we can solve it as part of that feature, maybe make a note to test this scenario. |
Same issue here. |
@PRIMETSS Just curious: Why do you need that in the first place? Culture support works just fine on the Raspberry Pi (at least for me), so this setting seems unnecessary. Or is it the container you're using? |
To get DotNet 7/8 (Core) runtime to currently function on OpenWRT Busybox musl-linux using package This package has now updated to icu72 recently, and have not checked if this removed that 'workaround' but from memory I think I still had same issue after. |
Will try to make some progress this weekend. |
…1210) Fixes #1126 Refs #1238 A quick draft to move the unit abbreviations into resource files. I also want to move methods for getting culture-specific abbreviations to the individual quantities. This should: - Reduce initialization time by removing the MapGeneratedLocalizations methods (TODO) - Reduce in-memory footprint by only loading a requested culture - Allow formatting to use methods on the quantities themselves - Which should allow for better extensibility as well - Remove the unit abbreviations cache (deprecate) ### TODOs - [ ] Test fallback to invariant culture `export DOTNET_SYSTEM_GLOBALIZATION_INVARIANT='1'` #1238 --------- Co-authored-by: Andreas Gullberg Larsen <andreas.larsen84@gmail.com>
Fixes #1238 Applications crashed when running on Linux or Raspberry PI systems if .NET cultures were not installed. Specifically, `UnitAbbreviationsCache.Default` threw an exception trying to instantiate the fallback `CultureInfo` with `en-US`. ### Changes - Change fallback culture to `InvariantCulture` - Add `CultureHelper.GetCultureOrInvariant()` to handle `CultureNotFoundException` - Change `UnitInfo` to map invariant culture to `en-US` localization
A possible fix is merged in #1266 , it would be good if you have the chance to build it locally and try it out. I have a few other fixes before I'm ready to publish a new nuget. |
When .ToString() is called via DotNet #7 App on a UnitsNet Type on a system that either does not have Culture support with the
export DOTNET_SYSTEM_GLOBALIZATION_INVARIANT='1'
environment variable set (required to run dotnet on PI) in Culture Invariant mode.Unhandled exception. System.TypeInitializationException: The type initializer for 'UnitsNet.UnitAbbreviationsCache' threw an exception. ---> System.Globalization.CultureNotFoundException: Only the invariant culture is supported in globalization-invariant mode. See https://aka.ms/GlobalizationInvariantMode for more information. (Parameter 'name') en-US is an invalid culture identifier. at System.Globalization.CultureInfo..ctor(String name, Boolean useUserOverride) at UnitsNet.UnitAbbreviationsCache..cctor() --- End of inner exception stack trace --- at UnitsNet.UnitAbbreviationsCache.get_Default() at UnitsNet.UnitFormatter.GetFormatArgs[TUnitType](TUnitType unit, Double value, IFormatProvider culture, IEnumerable
1 args)at UnitsNet.QuantityFormatter.ToStringWithSignificantDigitsAfterRadix[TUnitType](IQuantity
1 quantity, IFormatProvider formatProvider, Int32 number) at UnitsNet.QuantityFormatter.Format[TUnitType](IQuantity
1 quantity, String format, IFormatProvider formatProvider)at UnitsNet.Temperature.ToString(String format, IFormatProvider provider)
at UnitsNet.Temperature.ToString(String format)
at UnitsNet.Temperature.ToString()`
On OpenWRT SNAPShot (r22514) (Busybox Linux-musl ARM64) running on a Raspberry PI CM4 install latest DotNet SDK Version: 7.0.202 an ' System.Globalization.CultureNotFoundException' is thrown.
Expected behavior
string temp = sensorAht20.GetTemperature().ToString("F2")
Expected to return a 2 decimal places Temperature, eg '22.2' on system where Culture is not available.
https://learn.microsoft.com/en-us/dotnet/core/runtime-config/globalization
The text was updated successfully, but these errors were encountered: