Skip to content

Commit 7c677dc

Browse files
committed
move checks outside of CultureData.cs
1 parent 7167047 commit 7c677dc

File tree

6 files changed

+32
-13
lines changed

6 files changed

+32
-13
lines changed

src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.Icu.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Diagnostics;
66
using System.Diagnostics.CodeAnalysis;
7+
using System.Runtime.CompilerServices;
78

89
namespace System.Globalization
910
{
@@ -63,11 +64,8 @@ private bool InitIcuCultureDataCore()
6364
{
6465
_iLanguage = CultureInfo.LOCALE_CUSTOM_UNSPECIFIED;
6566
}
66-
6767
_bNeutral = TwoLetterISOCountryName.Length == 0;
68-
6968
_sSpecificCulture = _bNeutral ? IcuLocaleData.GetSpecificCultureName(_sRealName) : _sRealName;
70-
7169
// Remove the sort from sName unless custom culture
7270
if (index > 0 && !_bNeutral && !IsCustomCultureId(_iLanguage))
7371
{
@@ -110,7 +108,6 @@ private string IcuGetLocaleInfo(LocaleStringData type)
110108
{
111109
Debug.Assert(!GlobalizationMode.Invariant);
112110
Debug.Assert(!GlobalizationMode.UseNls);
113-
114111
Debug.Assert(_sWindowsName != null, "[CultureData.IcuGetLocaleInfo] Expected _sWindowsName to be populated already");
115112
return IcuGetLocaleInfo(_sWindowsName, type);
116113
}
@@ -138,7 +135,6 @@ private unsafe string IcuGetLocaleInfo(string localeName, LocaleStringData type)
138135
Debug.Fail("[CultureData.IcuGetLocaleInfo(LocaleStringData)] Failed");
139136
return string.Empty;
140137
}
141-
142138
return new string(buffer);
143139
}
144140

@@ -226,6 +222,18 @@ private static string IcuGetLanguageDisplayName(string cultureName)
226222
return null;
227223
}
228224

225+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
226+
internal static void IcuIsEnsurePredefinedLocaleName(string name)
227+
{
228+
Debug.Assert(!GlobalizationMode.UseNls);
229+
230+
if (!Interop.Globalization.IsPredefinedLocale(name))
231+
{
232+
throw new CultureNotFoundException(nameof(name), SR.Format(SR.Argument_InvalidPredefinedCultureName, name),
233+
message: "View https://docs.microsoft.com/en-us/aspnet/core/blazor/globalization-localization?view=aspnetcore-5.0#blazor-webassembly to load a new culture");
234+
}
235+
}
236+
229237
private static string ConvertIcuTimeFormatString(ReadOnlySpan<char> icuFormatString)
230238
{
231239
Debug.Assert(icuFormatString.Length < ICU_ULOC_FULLNAME_CAPACITY);

src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.Nls.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Diagnostics;
66
using System.Diagnostics.CodeAnalysis;
77
using System.Text;
8+
using System.Runtime.CompilerServices;
89
using Internal.Runtime.CompilerServices;
910
using System.Runtime.InteropServices;
1011

@@ -85,6 +86,17 @@ private int[] NlsGetLocaleInfo(LocaleGroupingData type)
8586
return ConvertWin32GroupString(GetLocaleInfoFromLCType(_sWindowsName, (uint)type, _bUseOverrides));
8687
}
8788

89+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
90+
internal static void NlsIsEnsurePredefinedLocaleName(string name)
91+
{
92+
Debug.Assert(GlobalizationMode.UseNls);
93+
94+
if (CultureData.GetLocaleInfoExInt(name, Interop.Kernel32.LOCALE_ICONSTRUCTEDLOCALE) == 1)
95+
{
96+
throw new CultureNotFoundException(nameof(name), SR.Format(SR.Argument_InvalidPredefinedCultureName, name));
97+
}
98+
}
99+
88100
private string? NlsGetTimeFormatString()
89101
{
90102
Debug.Assert(ShouldUseUserOverrideNlsData);

src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -805,10 +805,13 @@ private static string NormalizeCultureName(string name, out bool isNeutralName)
805805

806806
if (GlobalizationMode.PredefinedOnly)
807807
{
808-
if (!Interop.Globalization.IsPredefinedLocale(cultureName))
808+
if (GlobalizationMode.UseNls)
809809
{
810-
throw new CultureNotFoundException(nameof(cultureName), SR.Format(SR.Argument_InvalidPredefinedCultureName, cultureName),
811-
message: "View https://docs.microsoft.com/en-us/aspnet/core/blazor/globalization-localization?view=aspnetcore-5.0#blazor-webassembly to load a new culture");
810+
NlsIsEnsurePredefinedLocaleName(cultureName);
811+
}
812+
else
813+
{
814+
IcuIsEnsurePredefinedLocaleName(cultureName);
812815
}
813816
}
814817

src/libraries/System.Private.CoreLib/src/System/Globalization/GlobalizationMode.Unix.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ internal static partial class GlobalizationMode
1111

1212
internal static bool UseNls => false;
1313

14-
internal static bool PredefinedOnly { get; } = GetPredefinedOnly();
1514

1615
private static bool GetGlobalizationInvariantMode()
1716
{

src/libraries/System.Private.CoreLib/src/System/Globalization/GlobalizationMode.Windows.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ internal static partial class GlobalizationMode
99
// So we need Invariant to be initialized first.
1010
internal static bool Invariant { get; } = GetInvariantSwitchValue();
1111

12-
internal static bool PredefinedOnly { get; } = GetPredefinedOnly();
13-
1412
internal static bool UseNls { get; } = !Invariant &&
1513
(GetSwitchValue("System.Globalization.UseNls", "DOTNET_SYSTEM_GLOBALIZATION_USENLS") ||
1614
!LoadIcu());

src/libraries/System.Private.CoreLib/src/System/Globalization/GlobalizationMode.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ private static bool GetInvariantSwitchValue() =>
1414
private static bool TryGetAppLocalIcuSwitchValue([NotNullWhen(true)] out string? value) =>
1515
TryGetStringValue("System.Globalization.AppLocalIcu", "DOTNET_SYSTEM_GLOBALIZATION_APPLOCALICU", out value);
1616

17-
internal static bool GetPredefinedOnly() =>
18-
GetSwitchValue("System.Globalization.PredefinedOnly", "DOTNET_SYSTEM_GLOBALIZATION_PREDEFINED_ONLY");
17+
internal static bool PredefinedOnly { get; } = GetSwitchValue("System.Globalization.PredefinedOnly", "DOTNET_SYSTEM_GLOBALIZATION_PREDEFINED_ONLY");
1918

2019
private static bool GetSwitchValue(string switchName, string envVariable)
2120
{

0 commit comments

Comments
 (0)