diff --git a/.gitignore b/.gitignore index ad0b81df21..12fdef8038 100644 --- a/.gitignore +++ b/.gitignore @@ -30,4 +30,5 @@ nuget.exe *.*sdf *.ipch *.sln.ide -*launchSettings.json \ No newline at end of file +*launchSettings.json +**/Resources/*.Designer.cs \ No newline at end of file diff --git a/Localization.sln b/Localization.sln index 06bff65d67..e641bd0819 100644 --- a/Localization.sln +++ b/Localization.sln @@ -1,6 +1,6 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 14 -VisualStudioVersion = 14.0.23107.0 +VisualStudioVersion = 14.0.25420.1 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{FB313677-BAB3-4E49-8CDB-4FA4A9564767}" EndProject diff --git a/samples/LocalizationSample/Startup.cs b/samples/LocalizationSample/Startup.cs index 9000d66fb5..bd43778727 100644 --- a/samples/LocalizationSample/Startup.cs +++ b/samples/LocalizationSample/Startup.cs @@ -37,7 +37,8 @@ namespace LocalizationSample #if !NETCOREAPP1_0 supportedCultures.Add(new CultureInfo("zh-CHT")); #endif - var options = new RequestLocalizationOptions { + var options = new RequestLocalizationOptions + { DefaultRequestCulture = new RequestCulture("en-US"), SupportedCultures = supportedCultures, SupportedUICultures = supportedCultures diff --git a/src/Microsoft.Extensions.Localization/ResourceManagerStringLocalizerFactory.cs b/src/Microsoft.Extensions.Localization/ResourceManagerStringLocalizerFactory.cs index b08040ffda..95c672509c 100644 --- a/src/Microsoft.Extensions.Localization/ResourceManagerStringLocalizerFactory.cs +++ b/src/Microsoft.Extensions.Localization/ResourceManagerStringLocalizerFactory.cs @@ -50,6 +50,70 @@ namespace Microsoft.Extensions.Localization } } + /// + /// Gets the resource prefix used to look up the resource. + /// + /// The type of the resource to be looked up. + /// The prefix for resource lookup. + protected virtual string GetResourcePrefix(TypeInfo typeInfo) + { + if (typeInfo == null) + { + throw new ArgumentNullException(nameof(typeInfo)); + } + + return GetResourcePrefix(typeInfo, _hostingEnvironment.ApplicationName, _resourcesRelativePath); + } + + /// + /// Gets the resource prefix used to look up the resource. + /// + /// The type of the resource to be looked up. + /// The base namespace of the application. + /// The folder containing all resources. + /// The prefix for resource lookup. + /// + /// 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". + /// + 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 + "."); + } + + /// + /// Gets the resource prefix used to look up the resource. + /// + /// The name of the resource to be looked up + /// The base namespace of the application. + /// The prefix for resource lookup. + 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; + } + /// /// Creates a using the and /// of the specified . @@ -67,10 +131,7 @@ namespace Microsoft.Extensions.Localization var assembly = typeInfo.Assembly; // Re-root the base name if a resources path is set - var baseName = string.IsNullOrEmpty(_resourcesRelativePath) - ? typeInfo.FullName - : _hostingEnvironment.ApplicationName + "." + _resourcesRelativePath - + TrimPrefix(typeInfo.FullName, _hostingEnvironment.ApplicationName + "."); + var baseName = GetResourcePrefix(typeInfo); return _localizerCache.GetOrAdd(baseName, _ => new ResourceManagerStringLocalizer( @@ -96,7 +157,7 @@ namespace Microsoft.Extensions.Localization location = location ?? _hostingEnvironment.ApplicationName; - baseName = location + "." + _resourcesRelativePath + TrimPrefix(baseName, location + "."); + baseName = GetResourcePrefix(baseName, location); return _localizerCache.GetOrAdd($"B={baseName},L={location}", _ => {