@@ -13,9 +13,6 @@ namespace System.Globalization
13
13
{
14
14
internal partial class CultureData
15
15
{
16
- // Win32 constants
17
- const string LOCALE_NAME_SYSTEM_DEFAULT = @"!x-sys-default-locale" ;
18
-
19
16
// ICU constants
20
17
const int ICU_ULOC_KEYWORD_AND_VALUES_CAPACITY = 100 ; // max size of keyword or value
21
18
const int ICU_ULOC_FULLNAME_CAPACITY = 157 ; // max size of locale name
@@ -30,47 +27,35 @@ private unsafe bool InitCultureData()
30
27
Contract . Assert ( this . sRealName != null ) ;
31
28
32
29
string alternateSortName = string . Empty ;
33
- string realNameBuffer = null ;
34
- int index ;
30
+ string realNameBuffer = this . sRealName ;
35
31
36
- bool useSystemDefault = ( this . sRealName == LOCALE_NAME_SYSTEM_DEFAULT ) ;
37
- if ( ! useSystemDefault ) //ICU uses null to obtain the default (system) locale
32
+ // Basic validation
33
+ if ( realNameBuffer . Contains ( "@" ) )
38
34
{
39
- realNameBuffer = this . sRealName ;
40
-
41
- // Basic validation
42
- if ( realNameBuffer . Contains ( "@" ) )
43
- {
44
- return false ; // don't allow ICU variants to come in directly
45
- }
35
+ return false ; // don't allow ICU variants to come in directly
36
+ }
46
37
47
- // Replace _ (alternate sort) with @collation= for ICU
48
- index = realNameBuffer . IndexOf ( '_' ) ;
49
- if ( index > 0 )
38
+ // Replace _ (alternate sort) with @collation= for ICU
39
+ int index = realNameBuffer . IndexOf ( '_' ) ;
40
+ if ( index > 0 )
41
+ {
42
+ if ( index >= ( realNameBuffer . Length - 1 ) // must have characters after _
43
+ || realNameBuffer . Substring ( index + 1 ) . Contains ( "_" ) ) // only one _ allowed
50
44
{
51
- if ( index >= ( realNameBuffer . Length - 1 ) // must have characters after _
52
- || realNameBuffer . Substring ( index + 1 ) . Contains ( "_" ) ) // only one _ allowed
53
- {
54
- return false ; // fail
55
- }
56
- alternateSortName = realNameBuffer . Substring ( index + 1 ) ;
57
- realNameBuffer = realNameBuffer . Substring ( 0 , index ) + ICU_COLLATION_KEYWORD + alternateSortName ;
45
+ return false ; // fail
58
46
}
47
+ alternateSortName = realNameBuffer . Substring ( index + 1 ) ;
48
+ realNameBuffer = realNameBuffer . Substring ( 0 , index ) + ICU_COLLATION_KEYWORD + alternateSortName ;
59
49
}
60
50
61
51
// Get the locale name from ICU
62
- StringBuilder sb = StringBuilderCache . Acquire ( ICU_ULOC_FULLNAME_CAPACITY ) ;
63
- if ( ! Interop . GlobalizationInterop . GetLocaleName ( realNameBuffer , sb , sb . Capacity ) )
52
+ if ( ! GetLocaleName ( realNameBuffer , out this . sWindowsName ) )
64
53
{
65
- StringBuilderCache . Release ( sb ) ;
66
54
return false ; // fail
67
55
}
68
56
69
- // Success - use the locale name returned which may be different than realNameBuffer (casing)
70
- this . sWindowsName = StringBuilderCache . GetStringAndRelease ( sb ) ; // the name passed to subsequent ICU calls
71
-
72
57
// Replace the ICU collation keyword with an _
73
- index = realNameBuffer . IndexOf ( ICU_COLLATION_KEYWORD , StringComparison . Ordinal ) ;
58
+ index = this . sWindowsName . IndexOf ( ICU_COLLATION_KEYWORD , StringComparison . Ordinal ) ;
74
59
if ( index >= 0 )
75
60
{
76
61
this . sName = this . sWindowsName . Substring ( 0 , index ) + "_" + alternateSortName ;
@@ -79,21 +64,13 @@ private unsafe bool InitCultureData()
79
64
{
80
65
this . sName = this . sWindowsName ;
81
66
}
82
-
83
67
this . sRealName = this . sName ;
84
68
this . sSpecificCulture = this . sRealName ; // we don't attempt to find a non-neutral locale if a neutral is passed in (unlike win32)
85
69
86
70
this . iLanguage = this . ILANGUAGE ;
87
71
if ( this . iLanguage == 0 )
88
72
{
89
- if ( useSystemDefault )
90
- {
91
- this . iLanguage = LOCALE_CUSTOM_DEFAULT ;
92
- }
93
- else
94
- {
95
- this . iLanguage = LOCALE_CUSTOM_UNSPECIFIED ;
96
- }
73
+ this . iLanguage = LOCALE_CUSTOM_UNSPECIFIED ;
97
74
}
98
75
99
76
this . bNeutral = ( this . SISO3166CTRYNAME . Length == 0 ) ;
@@ -106,10 +83,41 @@ private unsafe bool InitCultureData()
106
83
this . sName = this . sWindowsName . Substring ( 0 , index ) ;
107
84
}
108
85
}
86
+ return true ;
87
+ }
109
88
89
+ internal static bool GetLocaleName ( string localeName , out string windowsName )
90
+ {
91
+ // Get the locale name from ICU
92
+ StringBuilder sb = StringBuilderCache . Acquire ( ICU_ULOC_FULLNAME_CAPACITY ) ;
93
+ if ( ! Interop . GlobalizationInterop . GetLocaleName ( localeName , sb , sb . Capacity ) )
94
+ {
95
+ StringBuilderCache . Release ( sb ) ;
96
+ windowsName = null ;
97
+ return false ; // fail
98
+ }
99
+
100
+ // Success - use the locale name returned which may be different than realNameBuffer (casing)
101
+ windowsName = StringBuilderCache . GetStringAndRelease ( sb ) ; // the name passed to subsequent ICU calls
102
+ return true ;
103
+ }
104
+
105
+ internal static bool GetDefaultLocaleName ( out string windowsName )
106
+ {
107
+ // Get the default (system) locale name from ICU
108
+ StringBuilder sb = StringBuilderCache . Acquire ( ICU_ULOC_FULLNAME_CAPACITY ) ;
109
+ if ( ! Interop . GlobalizationInterop . GetDefaultLocaleName ( sb , sb . Capacity ) )
110
+ {
111
+ StringBuilderCache . Release ( sb ) ;
112
+ windowsName = null ;
113
+ return false ; // fail
114
+ }
115
+
116
+ // Success - use the locale name returned which may be different than realNameBuffer (casing)
117
+ windowsName = StringBuilderCache . GetStringAndRelease ( sb ) ; // the name passed to subsequent ICU calls
110
118
return true ;
111
119
}
112
-
120
+
113
121
private string GetLocaleInfo ( LocaleStringData type )
114
122
{
115
123
Contract . Assert ( this . sWindowsName != null , "[CultureData.GetLocaleInfo] Expected this.sWindowsName to be populated already" ) ;
@@ -244,7 +252,7 @@ private static string GetRegionDisplayName(string isoCountryCode)
244
252
245
253
private static CultureInfo GetUserDefaultCulture ( )
246
254
{
247
- return new CultureInfo ( LOCALE_NAME_SYSTEM_DEFAULT ) ;
255
+ return CultureInfo . GetUserDefaultCulture ( ) ;
248
256
}
249
257
}
250
258
}
0 commit comments