diff --git a/src/Microsoft.AspNet.Localization/RequestLocalizationMiddleware.cs b/src/Microsoft.AspNet.Localization/RequestLocalizationMiddleware.cs index aa803191b0..546f1deb21 100644 --- a/src/Microsoft.AspNet.Localization/RequestLocalizationMiddleware.cs +++ b/src/Microsoft.AspNet.Localization/RequestLocalizationMiddleware.cs @@ -78,8 +78,7 @@ namespace Microsoft.AspNet.Localization cultureInfo = GetCultureInfo( cultures, _options.SupportedCultures, - _options.FallBackToParentCultures, - currentDepth: 0); + _options.FallBackToParentCultures); } if (_options.SupportedUICultures != null) @@ -87,8 +86,7 @@ namespace Microsoft.AspNet.Localization uiCultureInfo = GetCultureInfo( uiCultures, _options.SupportedUICultures, - _options.FallBackToParentUICultures, - currentDepth: 0); + _options.FallBackToParentUICultures); } if (cultureInfo == null && uiCultureInfo == null) @@ -139,8 +137,7 @@ namespace Microsoft.AspNet.Localization private static CultureInfo GetCultureInfo( IList cultureNames, IList supportedCultures, - bool fallbackToAncestorCulture, - int currentDepth) + bool fallbackToAncestorCulture) { foreach (var cultureName in cultureNames) { @@ -148,7 +145,7 @@ namespace Microsoft.AspNet.Localization // the CultureInfo ctor if (cultureName != null) { - var cultureInfo = CultureInfoCache.GetCultureInfo(cultureName, supportedCultures); + var cultureInfo = GetCultureInfo(cultureName, supportedCultures, fallbackToAncestorCulture, currentDepth: 0); if (cultureInfo != null) { return cultureInfo; @@ -156,40 +153,31 @@ namespace Microsoft.AspNet.Localization } } - if (fallbackToAncestorCulture & currentDepth < MaxCultureFallbackDepth) - { - // Walk backwards through the culture list and remove any root cultures (those with no parent) - for (var i = cultureNames.Count - 1; i >= 0; i--) - { - var cultureName = cultureNames[i]; - if (cultureName != null) - { - var lastIndexOfHyphen = cultureName.LastIndexOf('-'); - if (lastIndexOfHyphen > 0) - { - // Trim the trailing section from the culture name, e.g. "fr-FR" becomes "fr" - cultureNames[i] = cultureName.Substring(0, lastIndexOfHyphen); - } - else - { - // The culture had no sections left to trim so remove it from the list of candidates - cultureNames.RemoveAt(i); - } - } - else - { - // Culture name was null so just remove it - cultureNames.RemoveAt(i); - } - } + return null; + } - if (cultureNames.Count > 0) + private static CultureInfo GetCultureInfo( + string cultureName, + IList supportedCultures, + bool fallbackToAncestorCulture, + int currentDepth) + { + var culture = CultureInfoCache.GetCultureInfo(cultureName, supportedCultures); + + if (culture == null && fallbackToAncestorCulture && currentDepth < MaxCultureFallbackDepth) + { + var lastIndexOfHyphen = cultureName.LastIndexOf('-'); + + if (lastIndexOfHyphen > 0) { - return GetCultureInfo(cultureNames, supportedCultures, fallbackToAncestorCulture, currentDepth + 1); + // Trim the trailing section from the culture name, e.g. "fr-FR" becomes "fr" + var parentCultureName = cultureName.Substring(0, lastIndexOfHyphen); + + culture = GetCultureInfo(parentCultureName, supportedCultures, fallbackToAncestorCulture, currentDepth + 1); } } - return null; + return culture; } } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Localization/RequestLocalizationOptions.cs b/src/Microsoft.AspNet.Localization/RequestLocalizationOptions.cs index 462875fe71..0707efa2f1 100644 --- a/src/Microsoft.AspNet.Localization/RequestLocalizationOptions.cs +++ b/src/Microsoft.AspNet.Localization/RequestLocalizationOptions.cs @@ -51,28 +51,34 @@ namespace Microsoft.AspNet.Localization } /// - /// Gets or sets a value indicating whether to set a request culture to an ancestor culture in the case the + /// Gets or sets a value indicating whether to set a request culture to an parent culture in the case the /// culture determined by the configured s is not in the - /// list but an ancestor culture is. + /// list but a parent culture is. /// Defaults to true; /// + /// + /// Note that the parent culture check is done using only the culture name. + /// /// /// If this property is true and the application is configured to support the culture "fr", but not the /// culture "fr-FR", and a configured determines a request's culture is - /// "fr-FR", then the request's culture will be set to the culture "fr", as it is an ancestor of "fr-FR". + /// "fr-FR", then the request's culture will be set to the culture "fr", as it is a parent of "fr-FR". /// public bool FallBackToParentCultures { get; set; } = true; /// - /// Gets or sets a value indicating whether to set a request UI culture to an ancestor culture in the case the + /// Gets or sets a value indicating whether to set a request UI culture to a parent culture in the case the /// UI culture determined by the configured s is not in the - /// list but an ancestor culture is. + /// list but a parent culture is. /// Defaults to true; /// + /// + /// Note that the parent culture check is done using ony the culture name. + /// /// /// If this property is true and the application is configured to support the UI culture "fr", but not /// the UI culture "fr-FR", and a configured determines a request's UI - /// culture is "fr-FR", then the request's UI culture will be set to the culture "fr", as it is an ancestor of + /// culture is "fr-FR", then the request's UI culture will be set to the culture "fr", as it is a parent of /// "fr-FR". /// public bool FallBackToParentUICultures { get; set; } = true;