Skip to content

Commit bdcea55

Browse files
pgrawehrtmilnthorp
andauthored
Use CurrentCulture rather than CurrentUICulture (#986)
Co-authored-by: Tristan Milnthorp <[email protected]>
1 parent 5767608 commit bdcea55

File tree

239 files changed

+2058
-2048
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

239 files changed

+2058
-2048
lines changed

CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ public static string GetAbbreviation({_unitEnumName} unit)
334334
/// </summary>
335335
/// <param name=""unit"">Unit to get abbreviation for.</param>
336336
/// <returns>Unit abbreviation string.</returns>
337-
/// <param name=""provider"">Format to use for localization. Defaults to <see cref=""CultureInfo.CurrentUICulture"" /> if null.</param>
337+
/// <param name=""provider"">Format to use for localization. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
338338
public static string GetAbbreviation({_unitEnumName} unit, IFormatProvider? provider)
339339
{{
340340
return UnitAbbreviationsCache.Default.GetDefaultAbbreviation(unit, provider);
@@ -437,7 +437,7 @@ private void GenerateStaticParseMethods()
437437
/// We wrap exceptions in <see cref=""UnitsNetException"" /> to allow you to distinguish
438438
/// Units.NET exceptions from other exceptions.
439439
/// </exception>
440-
/// <param name=""provider"">Format to use when parsing number and unit. Defaults to <see cref=""CultureInfo.CurrentUICulture"" /> if null.</param>
440+
/// <param name=""provider"">Format to use when parsing number and unit. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
441441
public static {_quantity.Name} Parse(string str, IFormatProvider? provider)
442442
{{
443443
return QuantityParser.Default.Parse<{_quantity.Name}, {_unitEnumName}>(
@@ -468,7 +468,7 @@ public static bool TryParse(string? str, out {_quantity.Name} result)
468468
/// <example>
469469
/// Length.Parse(""5.5 m"", new CultureInfo(""en-US""));
470470
/// </example>
471-
/// <param name=""provider"">Format to use when parsing number and unit. Defaults to <see cref=""CultureInfo.CurrentUICulture"" /> if null.</param>
471+
/// <param name=""provider"">Format to use when parsing number and unit. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
472472
public static bool TryParse(string? str, IFormatProvider? provider, out {_quantity.Name} result)
473473
{{
474474
return QuantityParser.Default.TryParse<{_quantity.Name}, {_unitEnumName}>(
@@ -496,7 +496,7 @@ public static bool TryParse(string? str, IFormatProvider? provider, out {_quanti
496496
/// Parse a unit string.
497497
/// </summary>
498498
/// <param name=""str"">String to parse. Typically in the form: {{number}} {{unit}}</param>
499-
/// <param name=""provider"">Format to use when parsing number and unit. Defaults to <see cref=""CultureInfo.CurrentUICulture"" /> if null.</param>
499+
/// <param name=""provider"">Format to use when parsing number and unit. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
500500
/// <example>
501501
/// Length.ParseUnit(""m"", new CultureInfo(""en-US""));
502502
/// </example>
@@ -522,7 +522,7 @@ public static bool TryParseUnit(string str, out {_unitEnumName} unit)
522522
/// <example>
523523
/// Length.TryParseUnit(""m"", new CultureInfo(""en-US""));
524524
/// </example>
525-
/// <param name=""provider"">Format to use when parsing number and unit. Defaults to <see cref=""CultureInfo.CurrentUICulture"" /> if null.</param>
525+
/// <param name=""provider"">Format to use when parsing number and unit. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
526526
public static bool TryParseUnit(string str, IFormatProvider? provider, out {_unitEnumName} unit)
527527
{{
528528
return UnitParser.Default.TryParse<{_unitEnumName}>(str, provider, out unit);
@@ -925,29 +925,29 @@ public override string ToString()
925925
/// Gets the default string representation of value and unit using the given format provider.
926926
/// </summary>
927927
/// <returns>String representation.</returns>
928-
/// <param name=""provider"">Format to use for localization and number formatting. Defaults to <see cref=""CultureInfo.CurrentUICulture"" /> if null.</param>
928+
/// <param name=""provider"">Format to use for localization and number formatting. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
929929
public string ToString(IFormatProvider? provider)
930930
{{
931931
return ToString(""g"", provider);
932932
}}
933933
934934
/// <inheritdoc cref=""QuantityFormatter.Format{{TUnitType}}(IQuantity{{TUnitType}}, string, IFormatProvider)""/>
935935
/// <summary>
936-
/// Gets the string representation of this instance in the specified format string using <see cref=""CultureInfo.CurrentUICulture"" />.
936+
/// Gets the string representation of this instance in the specified format string using <see cref=""CultureInfo.CurrentCulture"" />.
937937
/// </summary>
938938
/// <param name=""format"">The format string.</param>
939939
/// <returns>The string representation.</returns>
940940
public string ToString(string format)
941941
{{
942-
return ToString(format, CultureInfo.CurrentUICulture);
942+
return ToString(format, CultureInfo.CurrentCulture);
943943
}}
944944
945945
/// <inheritdoc cref=""QuantityFormatter.Format{{TUnitType}}(IQuantity{{TUnitType}}, string, IFormatProvider)""/>
946946
/// <summary>
947-
/// Gets the string representation of this instance in the specified format string using the specified format provider, or <see cref=""CultureInfo.CurrentUICulture"" /> if null.
947+
/// Gets the string representation of this instance in the specified format string using the specified format provider, or <see cref=""CultureInfo.CurrentCulture"" /> if null.
948948
/// </summary>
949949
/// <param name=""format"">The format string.</param>
950-
/// <param name=""provider"">Format to use for localization and number formatting. Defaults to <see cref=""CultureInfo.CurrentUICulture"" /> if null.</param>
950+
/// <param name=""provider"">Format to use for localization and number formatting. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
951951
/// <returns>The string representation.</returns>
952952
public string ToString(string format, IFormatProvider? provider)
953953
{{

CodeGen/Generators/UnitsNetGen/StaticQuantityGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public static bool TryFrom(QuantityValue value, Enum unit, out IQuantity? quanti
9999
/// <summary>
100100
/// Try to dynamically parse a quantity string representation.
101101
/// </summary>
102-
/// <param name=""formatProvider"">The format provider to use for lookup. Defaults to <see cref=""CultureInfo.CurrentUICulture"" /> if null.</param>
102+
/// <param name=""formatProvider"">The format provider to use for lookup. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
103103
/// <param name=""quantityType"">Type of quantity, such as <see cref=""Length""/>.</param>
104104
/// <param name=""quantityString"">Quantity string representation, such as ""1.5 kg"". Must be compatible with given quantity type.</param>
105105
/// <param name=""quantity"">The resulting quantity if successful, otherwise <c>default</c>.</param>

CodeGen/Generators/UnitsNetGen/UnitTestBaseClassGenerator.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,8 @@ public void BaseDimensionsShouldNeverBeNull()
416416
[Fact]
417417
public void ToString_ReturnsValueAndUnitAbbreviationInCurrentCulture()
418418
{{
419-
var prevCulture = Thread.CurrentThread.CurrentUICulture;
420-
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(""en-US"");
419+
var prevCulture = Thread.CurrentThread.CurrentCulture;
420+
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(""en-US"");
421421
try {{");
422422
foreach (var unit in _quantity.Units)
423423
{
@@ -428,7 +428,7 @@ public void ToString_ReturnsValueAndUnitAbbreviationInCurrentCulture()
428428
}}
429429
finally
430430
{{
431-
Thread.CurrentThread.CurrentUICulture = prevCulture;
431+
Thread.CurrentThread.CurrentCulture = prevCulture;
432432
}}
433433
}}
434434
@@ -449,18 +449,18 @@ public void ToString_WithSwedishCulture_ReturnsUnitAbbreviationForEnglishCulture
449449
[Fact]
450450
public void ToString_SFormat_FormatsNumberWithGivenDigitsAfterRadixForCurrentCulture()
451451
{{
452-
var oldCulture = CultureInfo.CurrentUICulture;
452+
var oldCulture = CultureInfo.CurrentCulture;
453453
try
454454
{{
455-
CultureInfo.CurrentUICulture = CultureInfo.InvariantCulture;
455+
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
456456
Assert.Equal(""0.1{_baseUnitEnglishAbbreviation}"", new {_quantity.Name}(0.123456{_numberSuffix}, {_baseUnitFullName}).ToString(""s1""));
457457
Assert.Equal(""0.12{_baseUnitEnglishAbbreviation}"", new {_quantity.Name}(0.123456{_numberSuffix}, {_baseUnitFullName}).ToString(""s2""));
458458
Assert.Equal(""0.123{_baseUnitEnglishAbbreviation}"", new {_quantity.Name}(0.123456{_numberSuffix}, {_baseUnitFullName}).ToString(""s3""));
459459
Assert.Equal(""0.1235{_baseUnitEnglishAbbreviation}"", new {_quantity.Name}(0.123456{_numberSuffix}, {_baseUnitFullName}).ToString(""s4""));
460460
}}
461461
finally
462462
{{
463-
CultureInfo.CurrentUICulture = oldCulture;
463+
CultureInfo.CurrentCulture = oldCulture;
464464
}}
465465
}}
466466
@@ -490,10 +490,10 @@ public void ToString_NullFormat_DefaultsToGeneralFormat(string cultureName)
490490
[Theory]
491491
[InlineData(null)]
492492
[InlineData(""g"")]
493-
public void ToString_NullProvider_EqualsCurrentUICulture(string format)
493+
public void ToString_NullProvider_EqualsCurrentCulture(string format)
494494
{{
495495
var quantity = {_quantity.Name}.From{_baseUnit.PluralName}(1.0);
496-
Assert.Equal(quantity.ToString(format, CultureInfo.CurrentUICulture), quantity.ToString(format, null));
496+
Assert.Equal(quantity.ToString(format, CultureInfo.CurrentCulture), quantity.ToString(format, null));
497497
}}
498498
499499
[Fact]

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ RotationalSpeed r = Angle.FromDegrees(90) / TimeSpan.FromSeconds(2);
8282

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

85-
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:
85+
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:
8686

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

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

UnitsNet.Tests/CustomCode/StonePoundsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void StonePoundsToString_FormatsNumberInDefaultCulture()
3535
{
3636
Mass m = Mass.FromStonePounds(3500, 1);
3737
StonePounds stonePounds = m.StonePounds;
38-
string numberInCurrentCulture = 3500.ToString("n0", CultureInfo.CurrentUICulture); // Varies between machines, can't hard code it
38+
string numberInCurrentCulture = 3500.ToString("n0", CultureInfo.CurrentCulture); // Varies between machines, can't hard code it
3939

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

UnitsNet.Tests/GeneratedCode/TestsBase/AccelerationTestsBase.g.cs

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet.Tests/GeneratedCode/TestsBase/AmountOfSubstanceTestsBase.g.cs

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)