Skip to content

Use CurrentCulture rather than CurrentUICulture #986

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

Merged
merged 4 commits into from
Nov 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ public static string GetAbbreviation({_unitEnumName} unit)
/// </summary>
/// <param name=""unit"">Unit to get abbreviation for.</param>
/// <returns>Unit abbreviation string.</returns>
/// <param name=""provider"">Format to use for localization. Defaults to <see cref=""CultureInfo.CurrentUICulture"" /> if null.</param>
/// <param name=""provider"">Format to use for localization. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
public static string GetAbbreviation({_unitEnumName} unit, IFormatProvider? provider)
{{
return UnitAbbreviationsCache.Default.GetDefaultAbbreviation(unit, provider);
Expand Down Expand Up @@ -437,7 +437,7 @@ private void GenerateStaticParseMethods()
/// We wrap exceptions in <see cref=""UnitsNetException"" /> to allow you to distinguish
/// Units.NET exceptions from other exceptions.
/// </exception>
/// <param name=""provider"">Format to use when parsing number and unit. Defaults to <see cref=""CultureInfo.CurrentUICulture"" /> if null.</param>
/// <param name=""provider"">Format to use when parsing number and unit. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
public static {_quantity.Name} Parse(string str, IFormatProvider? provider)
{{
return QuantityParser.Default.Parse<{_quantity.Name}, {_unitEnumName}>(
Expand Down Expand Up @@ -468,7 +468,7 @@ public static bool TryParse(string? str, out {_quantity.Name} result)
/// <example>
/// Length.Parse(""5.5 m"", new CultureInfo(""en-US""));
/// </example>
/// <param name=""provider"">Format to use when parsing number and unit. Defaults to <see cref=""CultureInfo.CurrentUICulture"" /> if null.</param>
/// <param name=""provider"">Format to use when parsing number and unit. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
public static bool TryParse(string? str, IFormatProvider? provider, out {_quantity.Name} result)
{{
return QuantityParser.Default.TryParse<{_quantity.Name}, {_unitEnumName}>(
Expand Down Expand Up @@ -496,7 +496,7 @@ public static bool TryParse(string? str, IFormatProvider? provider, out {_quanti
/// Parse a unit string.
/// </summary>
/// <param name=""str"">String to parse. Typically in the form: {{number}} {{unit}}</param>
/// <param name=""provider"">Format to use when parsing number and unit. Defaults to <see cref=""CultureInfo.CurrentUICulture"" /> if null.</param>
/// <param name=""provider"">Format to use when parsing number and unit. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
/// <example>
/// Length.ParseUnit(""m"", new CultureInfo(""en-US""));
/// </example>
Expand All @@ -522,7 +522,7 @@ public static bool TryParseUnit(string str, out {_unitEnumName} unit)
/// <example>
/// Length.TryParseUnit(""m"", new CultureInfo(""en-US""));
/// </example>
/// <param name=""provider"">Format to use when parsing number and unit. Defaults to <see cref=""CultureInfo.CurrentUICulture"" /> if null.</param>
/// <param name=""provider"">Format to use when parsing number and unit. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
public static bool TryParseUnit(string str, IFormatProvider? provider, out {_unitEnumName} unit)
{{
return UnitParser.Default.TryParse<{_unitEnumName}>(str, provider, out unit);
Expand Down Expand Up @@ -925,29 +925,29 @@ public override string ToString()
/// Gets the default string representation of value and unit using the given format provider.
/// </summary>
/// <returns>String representation.</returns>
/// <param name=""provider"">Format to use for localization and number formatting. Defaults to <see cref=""CultureInfo.CurrentUICulture"" /> if null.</param>
/// <param name=""provider"">Format to use for localization and number formatting. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
public string ToString(IFormatProvider? provider)
{{
return ToString(""g"", provider);
}}

/// <inheritdoc cref=""QuantityFormatter.Format{{TUnitType}}(IQuantity{{TUnitType}}, string, IFormatProvider)""/>
/// <summary>
/// Gets the string representation of this instance in the specified format string using <see cref=""CultureInfo.CurrentUICulture"" />.
/// Gets the string representation of this instance in the specified format string using <see cref=""CultureInfo.CurrentCulture"" />.
/// </summary>
/// <param name=""format"">The format string.</param>
/// <returns>The string representation.</returns>
public string ToString(string format)
{{
return ToString(format, CultureInfo.CurrentUICulture);
return ToString(format, CultureInfo.CurrentCulture);
}}

/// <inheritdoc cref=""QuantityFormatter.Format{{TUnitType}}(IQuantity{{TUnitType}}, string, IFormatProvider)""/>
/// <summary>
/// Gets the string representation of this instance in the specified format string using the specified format provider, or <see cref=""CultureInfo.CurrentUICulture"" /> if null.
/// Gets the string representation of this instance in the specified format string using the specified format provider, or <see cref=""CultureInfo.CurrentCulture"" /> if null.
/// </summary>
/// <param name=""format"">The format string.</param>
/// <param name=""provider"">Format to use for localization and number formatting. Defaults to <see cref=""CultureInfo.CurrentUICulture"" /> if null.</param>
/// <param name=""provider"">Format to use for localization and number formatting. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
/// <returns>The string representation.</returns>
public string ToString(string format, IFormatProvider? provider)
{{
Expand Down
2 changes: 1 addition & 1 deletion CodeGen/Generators/UnitsNetGen/StaticQuantityGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public static bool TryFrom(QuantityValue value, Enum unit, out IQuantity? quanti
/// <summary>
/// Try to dynamically parse a quantity string representation.
/// </summary>
/// <param name=""formatProvider"">The format provider to use for lookup. Defaults to <see cref=""CultureInfo.CurrentUICulture"" /> if null.</param>
/// <param name=""formatProvider"">The format provider to use for lookup. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
/// <param name=""quantityType"">Type of quantity, such as <see cref=""Length""/>.</param>
/// <param name=""quantityString"">Quantity string representation, such as ""1.5 kg"". Must be compatible with given quantity type.</param>
/// <param name=""quantity"">The resulting quantity if successful, otherwise <c>default</c>.</param>
Expand Down
16 changes: 8 additions & 8 deletions CodeGen/Generators/UnitsNetGen/UnitTestBaseClassGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,8 @@ public void BaseDimensionsShouldNeverBeNull()
[Fact]
public void ToString_ReturnsValueAndUnitAbbreviationInCurrentCulture()
{{
var prevCulture = Thread.CurrentThread.CurrentUICulture;
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(""en-US"");
var prevCulture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(""en-US"");
try {{");
foreach (var unit in _quantity.Units)
{
Expand All @@ -428,7 +428,7 @@ public void ToString_ReturnsValueAndUnitAbbreviationInCurrentCulture()
}}
finally
{{
Thread.CurrentThread.CurrentUICulture = prevCulture;
Thread.CurrentThread.CurrentCulture = prevCulture;
}}
}}

Expand All @@ -449,18 +449,18 @@ public void ToString_WithSwedishCulture_ReturnsUnitAbbreviationForEnglishCulture
[Fact]
public void ToString_SFormat_FormatsNumberWithGivenDigitsAfterRadixForCurrentCulture()
{{
var oldCulture = CultureInfo.CurrentUICulture;
var oldCulture = CultureInfo.CurrentCulture;
try
{{
CultureInfo.CurrentUICulture = CultureInfo.InvariantCulture;
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
Assert.Equal(""0.1{_baseUnitEnglishAbbreviation}"", new {_quantity.Name}(0.123456{_numberSuffix}, {_baseUnitFullName}).ToString(""s1""));
Assert.Equal(""0.12{_baseUnitEnglishAbbreviation}"", new {_quantity.Name}(0.123456{_numberSuffix}, {_baseUnitFullName}).ToString(""s2""));
Assert.Equal(""0.123{_baseUnitEnglishAbbreviation}"", new {_quantity.Name}(0.123456{_numberSuffix}, {_baseUnitFullName}).ToString(""s3""));
Assert.Equal(""0.1235{_baseUnitEnglishAbbreviation}"", new {_quantity.Name}(0.123456{_numberSuffix}, {_baseUnitFullName}).ToString(""s4""));
}}
finally
{{
CultureInfo.CurrentUICulture = oldCulture;
CultureInfo.CurrentCulture = oldCulture;
}}
}}

Expand Down Expand Up @@ -490,10 +490,10 @@ public void ToString_NullFormat_DefaultsToGeneralFormat(string cultureName)
[Theory]
[InlineData(null)]
[InlineData(""g"")]
public void ToString_NullProvider_EqualsCurrentUICulture(string format)
public void ToString_NullProvider_EqualsCurrentCulture(string format)
{{
var quantity = {_quantity.Name}.From{_baseUnit.PluralName}(1.0);
Assert.Equal(quantity.ToString(format, CultureInfo.CurrentUICulture), quantity.ToString(format, null));
Assert.Equal(quantity.ToString(format, CultureInfo.CurrentCulture), quantity.ToString(format, null));
}}

[Fact]
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ RotationalSpeed r = Angle.FromDegrees(90) / TimeSpan.FromSeconds(2);

### <a name="culture"></a>Culture and Localization

The culture for abbreviations defaults to Thread.CurrentUICulture and falls back to US English if not defined. Thread.CurrentCulture affects number formatting unless a custom culture is specified. The relevant methods are:
The culture for abbreviations defaults to Thread.CurrentCulture and falls back to US English if not defined. Thread.CurrentCulture affects number formatting unless a custom culture is specified. The relevant methods are:

* ToString()
* GetAbbreviation()
Expand All @@ -93,8 +93,9 @@ var usEnglish = new CultureInfo("en-US");
var russian = new CultureInfo("ru-RU");
var oneKg = Mass.FromKilograms(1);

// ToString() uses CurrentUICulture for abbreviation language and CurrentCulture for number formatting
Thread.CurrentThread.CurrentUICulture = russian;
// ToString() uses CurrentCulture for abbreviation language number formatting. This is consistent with the behavior of the .NET Framework,
// where DateTime.ToString() uses CurrentCulture for the whole string, likely because mixing an english date format with a russian month name might be confusing.
Thread.CurrentThread.CurrentCulture = russian;
string kgRu = oneKg.ToString(); // "1 кг"

// ToString() with specific culture and custom string format pattern
Expand Down
2 changes: 1 addition & 1 deletion UnitsNet.Tests/CustomCode/StonePoundsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void StonePoundsToString_FormatsNumberInDefaultCulture()
{
Mass m = Mass.FromStonePounds(3500, 1);
StonePounds stonePounds = m.StonePounds;
string numberInCurrentCulture = 3500.ToString("n0", CultureInfo.CurrentUICulture); // Varies between machines, can't hard code it
string numberInCurrentCulture = 3500.ToString("n0", CultureInfo.CurrentCulture); // Varies between machines, can't hard code it

Assert.Equal($"{numberInCurrentCulture} st 1 lb", stonePounds.ToString());
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading