Skip to content

Commit e72d3cb

Browse files
committed
ResourceManagerStringLocalizerFactory trims app name from resource base name:
- Only applies when LocalizationOptions.ResourcesPath is set - #167
1 parent 2e8ae96 commit e72d3cb

File tree

8 files changed

+13
-3
lines changed

8 files changed

+13
-3
lines changed

src/Microsoft.Extensions.Localization/ResourceManagerStringLocalizerFactory.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public class ResourceManagerStringLocalizerFactory : IStringLocalizerFactory
2020
private readonly ConcurrentDictionary<string, ResourceManagerStringLocalizer> _localizerCache =
2121
new ConcurrentDictionary<string, ResourceManagerStringLocalizer>();
2222
private readonly IApplicationEnvironment _applicationEnvironment;
23-
2423
private readonly string _resourcesRelativePath;
2524

2625
/// <summary>
@@ -69,7 +68,8 @@ public IStringLocalizer Create(Type resourceSource)
6968

7069
var baseName = string.IsNullOrEmpty(_resourcesRelativePath)
7170
? typeInfo.FullName
72-
: _applicationEnvironment.ApplicationName + "." + _resourcesRelativePath + typeInfo.FullName;
71+
: _applicationEnvironment.ApplicationName + "." + _resourcesRelativePath
72+
+ TrimPrefix(typeInfo.FullName, _applicationEnvironment.ApplicationName + ".");
7373

7474
return _localizerCache.GetOrAdd(baseName, _ =>
7575
new ResourceManagerStringLocalizer(
@@ -95,7 +95,7 @@ public IStringLocalizer Create(string baseName, string location)
9595

9696
var rootPath = location ?? _applicationEnvironment.ApplicationName;
9797
var assembly = Assembly.Load(new AssemblyName(rootPath));
98-
baseName = rootPath + "." + _resourcesRelativePath + baseName;
98+
baseName = rootPath + "." + _resourcesRelativePath + TrimPrefix(baseName, rootPath + ".");
9999

100100
return _localizerCache.GetOrAdd(baseName, _ =>
101101
new ResourceManagerStringLocalizer(
@@ -105,5 +105,15 @@ public IStringLocalizer Create(string baseName, string location)
105105
_resourceNamesCache)
106106
);
107107
}
108+
109+
private static string TrimPrefix(string name, string prefix)
110+
{
111+
if (name.StartsWith(prefix, StringComparison.Ordinal))
112+
{
113+
return name.Substring(prefix.Length);
114+
}
115+
116+
return name;
117+
}
108118
}
109119
}

0 commit comments

Comments
 (0)