This repository has been archived by the owner on Nov 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
/
ResourceManagerStringLocalizerFactory.cs
270 lines (234 loc) · 12 KB
/
ResourceManagerStringLocalizerFactory.cs
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Reflection;
using System.Resources;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Microsoft.Extensions.Localization
{
/// <summary>
/// An <see cref="IStringLocalizerFactory"/> that creates instances of <see cref="ResourceManagerStringLocalizer"/>.
/// </summary>
/// <remarks>
/// <see cref="ResourceManagerStringLocalizerFactory"/> offers multiple ways to set the relative path of
/// resources to be used. They are, in order of precedence:
/// <see cref="ResourceLocationAttribute"/> -> <see cref="LocalizationOptions.ResourcesPath"/> -> the project root.
/// </remarks>
public class ResourceManagerStringLocalizerFactory : IStringLocalizerFactory
{
private readonly IResourceNamesCache _resourceNamesCache = new ResourceNamesCache();
private readonly ConcurrentDictionary<string, ResourceManagerStringLocalizer> _localizerCache =
new ConcurrentDictionary<string, ResourceManagerStringLocalizer>();
private readonly string _resourcesRelativePath;
private readonly ILoggerFactory _loggerFactory;
/// <summary>
/// Creates a new <see cref="ResourceManagerStringLocalizer"/>.
/// </summary>
/// <param name="localizationOptions">The <see cref="IOptions{LocalizationOptions}"/>.</param>
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/>.</param>
public ResourceManagerStringLocalizerFactory(
IOptions<LocalizationOptions> localizationOptions,
ILoggerFactory loggerFactory)
{
if (localizationOptions == null)
{
throw new ArgumentNullException(nameof(localizationOptions));
}
if (loggerFactory == null)
{
throw new ArgumentNullException(nameof(loggerFactory));
}
_resourcesRelativePath = localizationOptions.Value.ResourcesPath ?? string.Empty;
_loggerFactory = loggerFactory;
if (!string.IsNullOrEmpty(_resourcesRelativePath))
{
_resourcesRelativePath = _resourcesRelativePath.Replace(Path.AltDirectorySeparatorChar, '.')
.Replace(Path.DirectorySeparatorChar, '.') + ".";
}
}
/// <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, GetRootNamespace(typeInfo.Assembly), GetResourcePath(typeInfo.Assembly));
}
/// <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));
}
if (string.IsNullOrEmpty(resourcesRelativePath))
{
return typeInfo.FullName;
}
else
{
// This expectation is defined by dotnet's automatic resource storage.
// We have to conform to "{RootNamespace}.{ResourceLocation}.{FullTypeName - AssemblyName}".
var assemblyName = new AssemblyName(typeInfo.Assembly.FullName).Name;
return baseNamespace + "." + resourcesRelativePath + TrimPrefix(typeInfo.FullName, assemblyName + ".");
}
}
/// <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));
}
if (string.IsNullOrEmpty(baseNamespace))
{
throw new ArgumentNullException(nameof(baseNamespace));
}
var assemblyName = new AssemblyName(baseNamespace);
var assembly = Assembly.Load(assemblyName);
var rootNamespace = GetRootNamespace(assembly);
var resourceLocation = GetResourcePath(assembly);
var locationPath = rootNamespace + "." + resourceLocation;
baseResourceName = locationPath + TrimPrefix(baseResourceName, baseNamespace + ".");
return baseResourceName;
}
/// <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 baseName = GetResourcePrefix(typeInfo);
var assembly = typeInfo.Assembly;
return _localizerCache.GetOrAdd(baseName, _ => CreateResourceManagerStringLocalizer(assembly, baseName));
}
/// <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));
}
if (location == null)
{
throw new ArgumentNullException(nameof(location));
}
return _localizerCache.GetOrAdd($"B={baseName},L={location}", _ =>
{
var assemblyName = new AssemblyName(location);
var assembly = Assembly.Load(assemblyName);
baseName = GetResourcePrefix(baseName, location);
return CreateResourceManagerStringLocalizer(assembly, baseName);
});
}
/// <summary>Creates a <see cref="ResourceManagerStringLocalizer"/> for the given input.</summary>
/// <param name="assembly">The assembly to create a <see cref="ResourceManagerStringLocalizer"/> for.</param>
/// <param name="baseName">The base name of the resource to search for.</param>
/// <returns>A <see cref="ResourceManagerStringLocalizer"/> for the given <paramref name="assembly"/> and <paramref name="baseName"/>.</returns>
/// <remarks>This method is virtual for testing purposes only.</remarks>
protected virtual ResourceManagerStringLocalizer CreateResourceManagerStringLocalizer(
Assembly assembly,
string baseName)
{
return new ResourceManagerStringLocalizer(
new ResourceManager(baseName, assembly),
assembly,
baseName,
_resourceNamesCache,
_loggerFactory.CreateLogger<ResourceManagerStringLocalizer>());
}
/// <summary>
/// Gets the resource prefix used to look up the resource.
/// </summary>
/// <param name="location">The general location of the resource.</param>
/// <param name="baseName">The base name of the resource.</param>
/// <param name="resourceLocation">The location of the resource within <paramref name="location"/>.</param>
/// <returns>The resource prefix used to look up the resource.</returns>
protected virtual string GetResourcePrefix(string location, string baseName, string resourceLocation)
{
// Re-root the base name if a resources path is set
return location + "." + resourceLocation + TrimPrefix(baseName, location + ".");
}
/// <summary>Gets a <see cref="ResourceLocationAttribute"/> from the provided <see cref="Assembly"/>.</summary>
/// <param name="assembly">The assembly to get a <see cref="ResourceLocationAttribute"/> from.</param>
/// <returns>The <see cref="ResourceLocationAttribute"/> associated with the given <see cref="Assembly"/>.</returns>
/// <remarks>This method is protected and virtual for testing purposes only.</remarks>
protected virtual ResourceLocationAttribute GetResourceLocationAttribute(Assembly assembly)
{
return assembly.GetCustomAttribute<ResourceLocationAttribute>();
}
/// <summary>Gets a <see cref="RootNamespaceAttribute"/> from the provided <see cref="Assembly"/>.</summary>
/// <param name="assembly">The assembly to get a <see cref="RootNamespaceAttribute"/> from.</param>
/// <returns>The <see cref="RootNamespaceAttribute"/> associated with the given <see cref="Assembly"/>.</returns>
/// <remarks>This method is protected and virtual for testing purposes only.</remarks>
protected virtual RootNamespaceAttribute GetRootNamespaceAttribute(Assembly assembly)
{
return assembly.GetCustomAttribute<RootNamespaceAttribute>();
}
private string GetRootNamespace(Assembly assembly)
{
var rootNamespaceAttribute = GetRootNamespaceAttribute(assembly);
return rootNamespaceAttribute?.RootNamespace ??
new AssemblyName(assembly.FullName).Name;
}
private string GetResourcePath(Assembly assembly)
{
var resourceLocationAttribute = GetResourceLocationAttribute(assembly);
// If we don't have an attribute assume all assemblies use the same resource location.
var resourceLocation = resourceLocationAttribute == null
? _resourcesRelativePath
: resourceLocationAttribute.ResourceLocation + ".";
resourceLocation = resourceLocation
.Replace(Path.DirectorySeparatorChar, '.')
.Replace(Path.AltDirectorySeparatorChar, '.');
return resourceLocation;
}
private static string TrimPrefix(string name, string prefix)
{
if (name.StartsWith(prefix, StringComparison.Ordinal))
{
return name.Substring(prefix.Length);
}
return name;
}
}
}