Make resource lookup behavior overridable (#266)

This commit is contained in:
Ryan Brandenburg 2016-08-08 11:45:42 -07:00 коммит произвёл GitHub
Родитель 6d5999c0df
Коммит d84b069116
4 изменённых файлов: 71 добавлений и 8 удалений

3
.gitignore поставляемый
Просмотреть файл

@ -30,4 +30,5 @@ nuget.exe
*.*sdf *.*sdf
*.ipch *.ipch
*.sln.ide *.sln.ide
*launchSettings.json *launchSettings.json
**/Resources/*.Designer.cs

Просмотреть файл

@ -1,6 +1,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00 Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14 # Visual Studio 14
VisualStudioVersion = 14.0.23107.0 VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{FB313677-BAB3-4E49-8CDB-4FA4A9564767}" Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{FB313677-BAB3-4E49-8CDB-4FA4A9564767}"
EndProject EndProject

Просмотреть файл

@ -37,7 +37,8 @@ namespace LocalizationSample
#if !NETCOREAPP1_0 #if !NETCOREAPP1_0
supportedCultures.Add(new CultureInfo("zh-CHT")); supportedCultures.Add(new CultureInfo("zh-CHT"));
#endif #endif
var options = new RequestLocalizationOptions { var options = new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en-US"), DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = supportedCultures, SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures SupportedUICultures = supportedCultures

Просмотреть файл

@ -50,6 +50,70 @@ namespace Microsoft.Extensions.Localization
} }
} }
/// <summary>
/// Gets the resource prefix used to look up the resource.
/// </summary>
/// <param name="typeInfo">The type of the resource to be looked up.</param>
/// <returns>The prefix for resource lookup.</returns>
protected virtual string GetResourcePrefix(TypeInfo typeInfo)
{
if (typeInfo == null)
{
throw new ArgumentNullException(nameof(typeInfo));
}
return GetResourcePrefix(typeInfo, _hostingEnvironment.ApplicationName, _resourcesRelativePath);
}
/// <summary>
/// Gets the resource prefix used to look up the resource.
/// </summary>
/// <param name="typeInfo">The type of the resource to be looked up.</param>
/// <param name="baseNamespace">The base namespace of the application.</param>
/// <param name="resourcesRelativePath">The folder containing all resources.</param>
/// <returns>The prefix for resource lookup.</returns>
/// <remarks>
/// For the type "Sample.Controllers.Home" if there's a resourceRelativePath return
/// "Sample.Resourcepath.Controllers.Home" if there isn't one then it would return "Sample.Controllers.Home".
/// </remarks>
protected virtual string GetResourcePrefix(TypeInfo typeInfo, string baseNamespace, string resourcesRelativePath)
{
if (typeInfo == null)
{
throw new ArgumentNullException(nameof(typeInfo));
}
if (string.IsNullOrEmpty(baseNamespace))
{
throw new ArgumentNullException(nameof(baseNamespace));
}
return string.IsNullOrEmpty(resourcesRelativePath)
? typeInfo.FullName
: baseNamespace + "." + resourcesRelativePath + TrimPrefix(typeInfo.FullName, baseNamespace + ".");
}
/// <summary>
/// Gets the resource prefix used to look up the resource.
/// </summary>
/// <param name="baseResourceName">The name of the resource to be looked up</param>
/// <param name="baseNamespace">The base namespace of the application.</param>
/// <returns>The prefix for resource lookup.</returns>
protected virtual string GetResourcePrefix(string baseResourceName, string baseNamespace)
{
if (string.IsNullOrEmpty(baseResourceName))
{
throw new ArgumentNullException(nameof(baseResourceName));
}
var locationPath = baseNamespace == _hostingEnvironment.ApplicationName ?
baseNamespace + "." + _resourcesRelativePath :
baseNamespace + ".";
baseResourceName = locationPath + TrimPrefix(baseResourceName, baseNamespace + ".");
return baseResourceName;
}
/// <summary> /// <summary>
/// Creates a <see cref="ResourceManagerStringLocalizer"/> using the <see cref="Assembly"/> and /// Creates a <see cref="ResourceManagerStringLocalizer"/> using the <see cref="Assembly"/> and
/// <see cref="Type.FullName"/> of the specified <see cref="Type"/>. /// <see cref="Type.FullName"/> of the specified <see cref="Type"/>.
@ -67,10 +131,7 @@ namespace Microsoft.Extensions.Localization
var assembly = typeInfo.Assembly; var assembly = typeInfo.Assembly;
// Re-root the base name if a resources path is set // Re-root the base name if a resources path is set
var baseName = string.IsNullOrEmpty(_resourcesRelativePath) var baseName = GetResourcePrefix(typeInfo);
? typeInfo.FullName
: _hostingEnvironment.ApplicationName + "." + _resourcesRelativePath
+ TrimPrefix(typeInfo.FullName, _hostingEnvironment.ApplicationName + ".");
return _localizerCache.GetOrAdd(baseName, _ => return _localizerCache.GetOrAdd(baseName, _ =>
new ResourceManagerStringLocalizer( new ResourceManagerStringLocalizer(
@ -96,7 +157,7 @@ namespace Microsoft.Extensions.Localization
location = location ?? _hostingEnvironment.ApplicationName; location = location ?? _hostingEnvironment.ApplicationName;
baseName = location + "." + _resourcesRelativePath + TrimPrefix(baseName, location + "."); baseName = GetResourcePrefix(baseName, location);
return _localizerCache.GetOrAdd($"B={baseName},L={location}", _ => return _localizerCache.GetOrAdd($"B={baseName},L={location}", _ =>
{ {