From e72d3cb249199160e8bbaf92c4e684b932519cec Mon Sep 17 00:00:00 2001 From: damianedwards Date: Tue, 15 Dec 2015 16:13:38 -0800 Subject: [PATCH] ResourceManagerStringLocalizerFactory trims app name from resource base name: - Only applies when LocalizationOptions.ResourcesPath is set - #167 --- ...ple.Startup.es-ES.resx => Startup.es-ES.resx} | 0 ...ple.Startup.fr-FR.resx => Startup.fr-FR.resx} | 0 ...ple.Startup.ja-JP.resx => Startup.ja-JP.resx} | 0 ...ple.Startup.zh-CN.resx => Startup.zh-CN.resx} | 0 ...ionSample.Startup.zh.resx => Startup.zh.resx} | 0 .../ResourceManagerStringLocalizerFactory.cs | 16 +++++++++++++--- ...mer.fr-FR.resx => Models.Customer.fr-FR.resx} | 0 ....resx => StartupResourcesInFolder.fr-FR.resx} | 0 8 files changed, 13 insertions(+), 3 deletions(-) rename samples/LocalizationSample/My/Resources/{LocalizationSample.Startup.es-ES.resx => Startup.es-ES.resx} (100%) rename samples/LocalizationSample/My/Resources/{LocalizationSample.Startup.fr-FR.resx => Startup.fr-FR.resx} (100%) rename samples/LocalizationSample/My/Resources/{LocalizationSample.Startup.ja-JP.resx => Startup.ja-JP.resx} (100%) rename samples/LocalizationSample/My/Resources/{LocalizationSample.Startup.zh-CN.resx => Startup.zh-CN.resx} (100%) rename samples/LocalizationSample/My/Resources/{LocalizationSample.Startup.zh.resx => Startup.zh.resx} (100%) rename test/LocalizationWebsite/Resources/{LocalizationWebsite.Models.Customer.fr-FR.resx => Models.Customer.fr-FR.resx} (100%) rename test/LocalizationWebsite/Resources/{LocalizationWebsite.StartupResourcesInFolder.fr-FR.resx => StartupResourcesInFolder.fr-FR.resx} (100%) diff --git a/samples/LocalizationSample/My/Resources/LocalizationSample.Startup.es-ES.resx b/samples/LocalizationSample/My/Resources/Startup.es-ES.resx similarity index 100% rename from samples/LocalizationSample/My/Resources/LocalizationSample.Startup.es-ES.resx rename to samples/LocalizationSample/My/Resources/Startup.es-ES.resx diff --git a/samples/LocalizationSample/My/Resources/LocalizationSample.Startup.fr-FR.resx b/samples/LocalizationSample/My/Resources/Startup.fr-FR.resx similarity index 100% rename from samples/LocalizationSample/My/Resources/LocalizationSample.Startup.fr-FR.resx rename to samples/LocalizationSample/My/Resources/Startup.fr-FR.resx diff --git a/samples/LocalizationSample/My/Resources/LocalizationSample.Startup.ja-JP.resx b/samples/LocalizationSample/My/Resources/Startup.ja-JP.resx similarity index 100% rename from samples/LocalizationSample/My/Resources/LocalizationSample.Startup.ja-JP.resx rename to samples/LocalizationSample/My/Resources/Startup.ja-JP.resx diff --git a/samples/LocalizationSample/My/Resources/LocalizationSample.Startup.zh-CN.resx b/samples/LocalizationSample/My/Resources/Startup.zh-CN.resx similarity index 100% rename from samples/LocalizationSample/My/Resources/LocalizationSample.Startup.zh-CN.resx rename to samples/LocalizationSample/My/Resources/Startup.zh-CN.resx diff --git a/samples/LocalizationSample/My/Resources/LocalizationSample.Startup.zh.resx b/samples/LocalizationSample/My/Resources/Startup.zh.resx similarity index 100% rename from samples/LocalizationSample/My/Resources/LocalizationSample.Startup.zh.resx rename to samples/LocalizationSample/My/Resources/Startup.zh.resx diff --git a/src/Microsoft.Extensions.Localization/ResourceManagerStringLocalizerFactory.cs b/src/Microsoft.Extensions.Localization/ResourceManagerStringLocalizerFactory.cs index 45e11b03cc..92a5bab228 100644 --- a/src/Microsoft.Extensions.Localization/ResourceManagerStringLocalizerFactory.cs +++ b/src/Microsoft.Extensions.Localization/ResourceManagerStringLocalizerFactory.cs @@ -20,7 +20,6 @@ namespace Microsoft.Extensions.Localization private readonly ConcurrentDictionary _localizerCache = new ConcurrentDictionary(); private readonly IApplicationEnvironment _applicationEnvironment; - private readonly string _resourcesRelativePath; /// @@ -69,7 +68,8 @@ namespace Microsoft.Extensions.Localization var baseName = string.IsNullOrEmpty(_resourcesRelativePath) ? typeInfo.FullName - : _applicationEnvironment.ApplicationName + "." + _resourcesRelativePath + typeInfo.FullName; + : _applicationEnvironment.ApplicationName + "." + _resourcesRelativePath + + TrimPrefix(typeInfo.FullName, _applicationEnvironment.ApplicationName + "."); return _localizerCache.GetOrAdd(baseName, _ => new ResourceManagerStringLocalizer( @@ -95,7 +95,7 @@ namespace Microsoft.Extensions.Localization var rootPath = location ?? _applicationEnvironment.ApplicationName; var assembly = Assembly.Load(new AssemblyName(rootPath)); - baseName = rootPath + "." + _resourcesRelativePath + baseName; + baseName = rootPath + "." + _resourcesRelativePath + TrimPrefix(baseName, rootPath + "."); return _localizerCache.GetOrAdd(baseName, _ => new ResourceManagerStringLocalizer( @@ -105,5 +105,15 @@ namespace Microsoft.Extensions.Localization _resourceNamesCache) ); } + + private static string TrimPrefix(string name, string prefix) + { + if (name.StartsWith(prefix, StringComparison.Ordinal)) + { + return name.Substring(prefix.Length); + } + + return name; + } } } \ No newline at end of file diff --git a/test/LocalizationWebsite/Resources/LocalizationWebsite.Models.Customer.fr-FR.resx b/test/LocalizationWebsite/Resources/Models.Customer.fr-FR.resx similarity index 100% rename from test/LocalizationWebsite/Resources/LocalizationWebsite.Models.Customer.fr-FR.resx rename to test/LocalizationWebsite/Resources/Models.Customer.fr-FR.resx diff --git a/test/LocalizationWebsite/Resources/LocalizationWebsite.StartupResourcesInFolder.fr-FR.resx b/test/LocalizationWebsite/Resources/StartupResourcesInFolder.fr-FR.resx similarity index 100% rename from test/LocalizationWebsite/Resources/LocalizationWebsite.StartupResourcesInFolder.fr-FR.resx rename to test/LocalizationWebsite/Resources/StartupResourcesInFolder.fr-FR.resx