parent
e72d3cb249
commit
b041a07652
|
|
@ -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<string> cultureNames,
|
||||
IList<CultureInfo> 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<CultureInfo> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -51,28 +51,34 @@ namespace Microsoft.AspNet.Localization
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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 <see cref="IRequestCultureProvider"/>s is not in the
|
||||
/// <see cref="SupportedCultures"/> list but an ancestor culture is.
|
||||
/// <see cref="SupportedCultures"/> list but a parent culture is.
|
||||
/// Defaults to <c>true</c>;
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Note that the parent culture check is done using only the culture name.
|
||||
/// </remarks>
|
||||
/// <example>
|
||||
/// If this property is <c>true</c> and the application is configured to support the culture "fr", but not the
|
||||
/// culture "fr-FR", and a configured <see cref="IRequestCultureProvider"/> 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".
|
||||
/// </example>
|
||||
public bool FallBackToParentCultures { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// 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 <see cref="IRequestCultureProvider"/>s is not in the
|
||||
/// <see cref="SupportedUICultures"/> list but an ancestor culture is.
|
||||
/// <see cref="SupportedUICultures"/> list but a parent culture is.
|
||||
/// Defaults to <c>true</c>;
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Note that the parent culture check is done using ony the culture name.
|
||||
/// </remarks>
|
||||
/// <example>
|
||||
/// If this property is <c>true</c> and the application is configured to support the UI culture "fr", but not
|
||||
/// the UI culture "fr-FR", and a configured <see cref="IRequestCultureProvider"/> 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".
|
||||
/// </example>
|
||||
public bool FallBackToParentUICultures { get; set; } = true;
|
||||
|
|
|
|||
Loading…
Reference in New Issue