@@ -78,17 +78,15 @@ public async Task Invoke(HttpContext context)
78
78
cultureInfo = GetCultureInfo (
79
79
cultures ,
80
80
_options . SupportedCultures ,
81
- _options . FallBackToParentCultures ,
82
- currentDepth : 0 ) ;
81
+ _options . FallBackToParentCultures ) ;
83
82
}
84
83
85
84
if ( _options . SupportedUICultures != null )
86
85
{
87
86
uiCultureInfo = GetCultureInfo (
88
87
uiCultures ,
89
88
_options . SupportedUICultures ,
90
- _options . FallBackToParentUICultures ,
91
- currentDepth : 0 ) ;
89
+ _options . FallBackToParentUICultures ) ;
92
90
}
93
91
94
92
if ( cultureInfo == null && uiCultureInfo == null )
@@ -139,57 +137,47 @@ private static void SetCurrentThreadCulture(RequestCulture requestCulture)
139
137
private static CultureInfo GetCultureInfo (
140
138
IList < string > cultureNames ,
141
139
IList < CultureInfo > supportedCultures ,
142
- bool fallbackToAncestorCulture ,
143
- int currentDepth )
140
+ bool fallbackToAncestorCulture )
144
141
{
145
142
foreach ( var cultureName in cultureNames )
146
143
{
147
144
// Allow empty string values as they map to InvariantCulture, whereas null culture values will throw in
148
145
// the CultureInfo ctor
149
146
if ( cultureName != null )
150
147
{
151
- var cultureInfo = CultureInfoCache . GetCultureInfo ( cultureName , supportedCultures ) ;
148
+ var cultureInfo = GetCultureInfo ( cultureName , supportedCultures , fallbackToAncestorCulture , currentDepth : 0 ) ;
152
149
if ( cultureInfo != null )
153
150
{
154
151
return cultureInfo ;
155
152
}
156
153
}
157
154
}
158
155
159
- if ( fallbackToAncestorCulture & currentDepth < MaxCultureFallbackDepth )
156
+ return null ;
157
+ }
158
+
159
+ private static CultureInfo GetCultureInfo (
160
+ string cultureName ,
161
+ IList < CultureInfo > supportedCultures ,
162
+ bool fallbackToAncestorCulture ,
163
+ int currentDepth )
164
+ {
165
+ var culture = CultureInfoCache . GetCultureInfo ( cultureName , supportedCultures ) ;
166
+
167
+ if ( culture == null && fallbackToAncestorCulture && currentDepth < MaxCultureFallbackDepth )
160
168
{
161
- // Walk backwards through the culture list and remove any root cultures (those with no parent)
162
- for ( var i = cultureNames . Count - 1 ; i >= 0 ; i -- )
163
- {
164
- var cultureName = cultureNames [ i ] ;
165
- if ( cultureName != null )
166
- {
167
- var lastIndexOfHyphen = cultureName . LastIndexOf ( '-' ) ;
168
- if ( lastIndexOfHyphen > 0 )
169
- {
170
- // Trim the trailing section from the culture name, e.g. "fr-FR" becomes "fr"
171
- cultureNames [ i ] = cultureName . Substring ( 0 , lastIndexOfHyphen ) ;
172
- }
173
- else
174
- {
175
- // The culture had no sections left to trim so remove it from the list of candidates
176
- cultureNames . RemoveAt ( i ) ;
177
- }
178
- }
179
- else
180
- {
181
- // Culture name was null so just remove it
182
- cultureNames . RemoveAt ( i ) ;
183
- }
184
- }
169
+ var lastIndexOfHyphen = cultureName . LastIndexOf ( '-' ) ;
185
170
186
- if ( cultureNames . Count > 0 )
171
+ if ( lastIndexOfHyphen > 0 )
187
172
{
188
- return GetCultureInfo ( cultureNames , supportedCultures , fallbackToAncestorCulture , currentDepth + 1 ) ;
173
+ // Trim the trailing section from the culture name, e.g. "fr-FR" becomes "fr"
174
+ var parentCultureName = cultureName . Substring ( 0 , lastIndexOfHyphen ) ;
175
+
176
+ culture = GetCultureInfo ( parentCultureName , supportedCultures , fallbackToAncestorCulture , currentDepth + 1 ) ;
189
177
}
190
178
}
191
179
192
- return null ;
180
+ return culture ;
193
181
}
194
182
}
195
183
}
0 commit comments