-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathGlobalResourceManagerStringLocalizerFactory.cs
More file actions
179 lines (152 loc) · 7.46 KB
/
GlobalResourceManagerStringLocalizerFactory.cs
File metadata and controls
179 lines (152 loc) · 7.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Reflection;
using System.Resources;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Options;
// http://stackoverflow.com/questions/34890555/asp-net-5-localization-using-view-components-in-a-separate-assembly
namespace Microsoft.Extensions.Localization
{
// https://github.com/aspnet/Localization/blob/dev/src/Microsoft.Extensions.Localization/ResourceManagerStringLocalizerFactory.cs
// https://github.com/dotnet/corefx/blob/master/src/System.Resources.ResourceManager/src/System/Resources/ResourceManager.cs
/// <summary>
/// An <see cref="IStringLocalizerFactory"/> that creates instances of <see cref="ResourceManagerStringLocalizer"/>.
/// </summary>
public class GlobalResourceManagerStringLocalizerFactory : IStringLocalizerFactory
{
private readonly IResourceNamesCache _resourceNamesCache = new ResourceNamesCache();
private readonly ConcurrentDictionary<string, GlobalResourceManagerStringLocalizer> _localizerCache =
new ConcurrentDictionary<string, GlobalResourceManagerStringLocalizer>();
private readonly IWebHostEnvironment _hostingEnvironment;
private readonly string _resourcesRelativePath;
private readonly GlobalResourceOptions globalResourceOptions;
/// <summary>
/// Creates a new <see cref="ResourceManagerStringLocalizer"/>.
/// </summary>
/// <param name="hostingEnvironment">The <see cref="IHostingEnvironment"/>.</param>
/// <param name="localizationOptions">The <see cref="IOptions{LocalizationOptions}"/>.</param>
public GlobalResourceManagerStringLocalizerFactory(
IWebHostEnvironment hostingEnvironment,
IOptions<LocalizationOptions> localizationOptions,
IOptions<GlobalResourceOptions> globalResourceOptionsAccessor
)
{
if (hostingEnvironment == null)
{
throw new ArgumentNullException(nameof(hostingEnvironment));
}
if (localizationOptions == null)
{
throw new ArgumentNullException(nameof(localizationOptions));
}
_hostingEnvironment = hostingEnvironment;
_resourcesRelativePath = localizationOptions.Value.ResourcesPath ?? string.Empty;
if (!string.IsNullOrEmpty(_resourcesRelativePath))
{
_resourcesRelativePath = _resourcesRelativePath.Replace(Path.AltDirectorySeparatorChar, '.')
.Replace(Path.DirectorySeparatorChar, '.') + ".";
}
globalResourceOptions = globalResourceOptionsAccessor.Value;
}
/// <summary>
/// Creates a <see cref="ResourceManagerStringLocalizer"/> using the <see cref="Assembly"/> and
/// <see cref="Type.FullName"/> of the specified <see cref="Type"/>.
/// </summary>
/// <param name="resourceSource">The <see cref="Type"/>.</param>
/// <returns>The <see cref="ResourceManagerStringLocalizer"/>.</returns>
public IStringLocalizer Create(Type resourceSource)
{
if (resourceSource == null)
{
throw new ArgumentNullException(nameof(resourceSource));
}
var typeInfo = resourceSource.GetTypeInfo();
var assembly = typeInfo.Assembly;
Assembly appAssembly = null;
ResourceManager appResourceManager = null;
string appBaseName = null;
// Re-root the base name if a resources path is set
string baseName;
if (assembly.FullName.StartsWith(_hostingEnvironment.ApplicationName))
{
baseName = string.IsNullOrEmpty(_resourcesRelativePath)
? typeInfo.FullName
: _hostingEnvironment.ApplicationName + "." + _resourcesRelativePath
+ TrimPrefix(typeInfo.FullName, _hostingEnvironment.ApplicationName + ".");
}
else
{
var libName = TrimOnFirstComma(assembly.FullName);
appAssembly = Assembly.GetEntryAssembly();
appBaseName = _hostingEnvironment.ApplicationName
+ "." + _resourcesRelativePath + TrimPrefix(typeInfo.FullName, libName + ".");
appResourceManager = new ResourceManager(appBaseName, appAssembly);
baseName = libName + "." + TrimPrefix(typeInfo.FullName, libName + ".");
}
return _localizerCache.GetOrAdd(baseName, _ =>
new GlobalResourceManagerStringLocalizer(
new ResourceManager(baseName, assembly),
assembly,
baseName,
_resourceNamesCache,
globalResourceOptions,
appResourceManager
)
);
}
/// <summary>
/// Creates a <see cref="ResourceManagerStringLocalizer"/>.
/// </summary>
/// <param name="baseName">The base name of the resource to load strings from.</param>
/// <param name="location">The location to load resources from.</param>
/// <returns>The <see cref="ResourceManagerStringLocalizer"/>.</returns>
public IStringLocalizer Create(string baseName, string location)
{
if (baseName == null)
{
throw new ArgumentNullException(nameof(baseName));
}
location = location ?? _hostingEnvironment.ApplicationName;
Assembly appAssembly = null;
ResourceManager appResourceManager = null;
string appBaseName = null;
if (location.StartsWith(_hostingEnvironment.ApplicationName))
{
baseName = location + "." + _resourcesRelativePath + TrimPrefix(baseName, location + ".");
}
else
{
appAssembly = Assembly.GetEntryAssembly();
appBaseName = _hostingEnvironment.ApplicationName
+ "." + _resourcesRelativePath + TrimPrefix(baseName, location + ".");
appResourceManager = new ResourceManager(appBaseName, appAssembly);
baseName = location + "." + TrimPrefix(baseName, location + ".");
}
return _localizerCache.GetOrAdd($"B={baseName},L={location}", _ =>
{
var assembly = Assembly.Load(new AssemblyName(location));
return new GlobalResourceManagerStringLocalizer(
new ResourceManager(baseName, assembly),
assembly,
baseName,
_resourceNamesCache,
globalResourceOptions,
appResourceManager
);
});
}
private static string TrimPrefix(string name, string prefix)
{
if (name.StartsWith(prefix, StringComparison.Ordinal))
{
return name.Substring(prefix.Length);
}
return name;
}
private static string TrimOnFirstComma(string name)
{
return name.Substring(0, name.IndexOf(","));
}
}
}