|
1 | | -using System; |
2 | | -using System.Collections.Concurrent; |
3 | | -using System.IO; |
4 | | -using System.Reflection; |
5 | | -using System.Resources; |
6 | | -using Microsoft.AspNetCore.Hosting; |
7 | | -using Microsoft.Extensions.Options; |
8 | | - |
9 | | -//https://github.com/aspnet/Localization/issues/139 |
10 | | - |
11 | | -namespace Microsoft.Extensions.Localization |
12 | | -{ |
13 | | - /// <summary> |
14 | | - /// An <see cref="IStringLocalizerFactory"/> that creates instances of <see cref="ResourceManagerStringLocalizer"/>. |
15 | | - /// </summary> |
16 | | - public class PatchedResourceManagerStringLocalizerFactory : IStringLocalizerFactory |
17 | | - { |
18 | | - private readonly IResourceNamesCache _resourceNamesCache = new ResourceNamesCache(); |
19 | | - private readonly ConcurrentDictionary<string, ResourceManagerStringLocalizer> _localizerCache = |
20 | | - new ConcurrentDictionary<string, ResourceManagerStringLocalizer>(); |
21 | | - private readonly IHostingEnvironment _hostingEnvironment; |
22 | | - private readonly string _resourcesRelativePath; |
23 | | - |
24 | | - /// <summary> |
25 | | - /// Creates a new <see cref="ResourceManagerStringLocalizer"/>. |
26 | | - /// </summary> |
27 | | - /// <param name="hostingEnvironment">The <see cref="IHostingEnvironment"/>.</param> |
28 | | - /// <param name="localizationOptions">The <see cref="IOptions{LocalizationOptions}"/>.</param> |
29 | | - public PatchedResourceManagerStringLocalizerFactory( |
30 | | - IHostingEnvironment hostingEnvironment, |
31 | | - IOptions<LocalizationOptions> localizationOptions) |
32 | | - { |
33 | | - if (hostingEnvironment == null) |
34 | | - { |
35 | | - throw new ArgumentNullException(nameof(hostingEnvironment)); |
36 | | - } |
37 | | - |
38 | | - if (localizationOptions == null) |
39 | | - { |
40 | | - throw new ArgumentNullException(nameof(localizationOptions)); |
41 | | - } |
42 | | - |
43 | | - _hostingEnvironment = hostingEnvironment; |
44 | | - _resourcesRelativePath = localizationOptions.Value.ResourcesPath ?? string.Empty; |
45 | | - if (!string.IsNullOrEmpty(_resourcesRelativePath)) |
46 | | - { |
47 | | - _resourcesRelativePath = _resourcesRelativePath.Replace(Path.AltDirectorySeparatorChar, '.') |
48 | | - .Replace(Path.DirectorySeparatorChar, '.') + "."; |
49 | | - } |
50 | | - } |
51 | | - |
52 | | - /// <summary> |
53 | | - /// Creates a <see cref="ResourceManagerStringLocalizer"/> using the <see cref="Assembly"/> and |
54 | | - /// <see cref="Type.FullName"/> of the specified <see cref="Type"/>. |
55 | | - /// </summary> |
56 | | - /// <param name="resourceSource">The <see cref="Type"/>.</param> |
57 | | - /// <returns>The <see cref="ResourceManagerStringLocalizer"/>.</returns> |
58 | | - public IStringLocalizer Create(Type resourceSource) |
59 | | - { |
60 | | - if (resourceSource == null) |
61 | | - { |
62 | | - throw new ArgumentNullException(nameof(resourceSource)); |
63 | | - } |
64 | | - |
65 | | - var typeInfo = resourceSource.GetTypeInfo(); |
66 | | - var assembly = typeInfo.Assembly; |
67 | | - |
68 | | - // Re-root the base name if a resources path is set |
69 | | - string baseName; |
70 | | - if (assembly.FullName.StartsWith(_hostingEnvironment.ApplicationName)) |
71 | | - { |
72 | | - baseName = string.IsNullOrEmpty(_resourcesRelativePath) |
73 | | - ? typeInfo.FullName |
74 | | - : _hostingEnvironment.ApplicationName + "." + _resourcesRelativePath |
75 | | - + TrimPrefix(typeInfo.FullName, _hostingEnvironment.ApplicationName + "."); |
76 | | - } |
77 | | - else |
78 | | - { |
79 | | - var libName = TrimOnFirstComma(assembly.FullName); |
80 | | - baseName = libName + "." + TrimPrefix(typeInfo.FullName, libName + "."); |
81 | | - } |
82 | | - |
83 | | - return _localizerCache.GetOrAdd(baseName, _ => |
84 | | - new ResourceManagerStringLocalizer( |
85 | | - new ResourceManager(baseName, assembly), |
86 | | - assembly, |
87 | | - baseName, |
88 | | - _resourceNamesCache) |
89 | | - ); |
90 | | - |
91 | | - |
92 | | - } |
93 | | - |
94 | | - /// <summary> |
95 | | - /// Creates a <see cref="ResourceManagerStringLocalizer"/>. |
96 | | - /// </summary> |
97 | | - /// <param name="baseName">The base name of the resource to load strings from.</param> |
98 | | - /// <param name="location">The location to load resources from.</param> |
99 | | - /// <returns>The <see cref="ResourceManagerStringLocalizer"/>.</returns> |
100 | | - public IStringLocalizer Create(string baseName, string location) |
101 | | - { |
102 | | - if (baseName == null) |
103 | | - { |
104 | | - throw new ArgumentNullException(nameof(baseName)); |
105 | | - } |
106 | | - |
107 | | - location = location ?? _hostingEnvironment.ApplicationName; |
108 | | - |
109 | | - if (location.StartsWith(_hostingEnvironment.ApplicationName)) |
110 | | - { |
111 | | - baseName = location + "." + _resourcesRelativePath + TrimPrefix(baseName, location + "."); |
112 | | - } |
113 | | - else |
114 | | - { |
115 | | - baseName = location + "." + TrimPrefix(baseName, location + "."); |
116 | | - } |
117 | | - |
118 | | - return _localizerCache.GetOrAdd($"B={baseName},L={location}", _ => |
119 | | - { |
120 | | - var assembly = Assembly.Load(new AssemblyName(location)); |
121 | | - return new ResourceManagerStringLocalizer( |
122 | | - new ResourceManager(baseName, assembly), |
123 | | - assembly, |
124 | | - baseName, |
125 | | - _resourceNamesCache); |
126 | | - }); |
127 | | - } |
128 | | - |
129 | | - private static string TrimPrefix(string name, string prefix) |
130 | | - { |
131 | | - if (name.StartsWith(prefix, StringComparison.Ordinal)) |
132 | | - { |
133 | | - return name.Substring(prefix.Length); |
134 | | - } |
135 | | - |
136 | | - return name; |
137 | | - } |
138 | | - |
139 | | - private static string TrimOnFirstComma(string name) |
140 | | - { |
141 | | - |
142 | | - return name.Substring(0, name.IndexOf(",")); |
143 | | - |
144 | | - } |
145 | | - } |
146 | | -} |
| 1 | +//using System; |
| 2 | +//using System.Collections.Concurrent; |
| 3 | +//using System.IO; |
| 4 | +//using System.Reflection; |
| 5 | +//using System.Resources; |
| 6 | +//using Microsoft.AspNetCore.Hosting; |
| 7 | +//using Microsoft.Extensions.Options; |
| 8 | + |
| 9 | +////https://github.com/aspnet/Localization/issues/139 |
| 10 | + |
| 11 | +//namespace Microsoft.Extensions.Localization |
| 12 | +//{ |
| 13 | +// /// <summary> |
| 14 | +// /// An <see cref="IStringLocalizerFactory"/> that creates instances of <see cref="ResourceManagerStringLocalizer"/>. |
| 15 | +// /// </summary> |
| 16 | +// public class PatchedResourceManagerStringLocalizerFactory : IStringLocalizerFactory |
| 17 | +// { |
| 18 | +// private readonly IResourceNamesCache _resourceNamesCache = new ResourceNamesCache(); |
| 19 | +// private readonly ConcurrentDictionary<string, ResourceManagerStringLocalizer> _localizerCache = |
| 20 | +// new ConcurrentDictionary<string, ResourceManagerStringLocalizer>(); |
| 21 | +// private readonly IHostingEnvironment _hostingEnvironment; |
| 22 | +// private readonly string _resourcesRelativePath; |
| 23 | + |
| 24 | +// /// <summary> |
| 25 | +// /// Creates a new <see cref="ResourceManagerStringLocalizer"/>. |
| 26 | +// /// </summary> |
| 27 | +// /// <param name="hostingEnvironment">The <see cref="IHostingEnvironment"/>.</param> |
| 28 | +// /// <param name="localizationOptions">The <see cref="IOptions{LocalizationOptions}"/>.</param> |
| 29 | +// public PatchedResourceManagerStringLocalizerFactory( |
| 30 | +// IHostingEnvironment hostingEnvironment, |
| 31 | +// IOptions<LocalizationOptions> localizationOptions) |
| 32 | +// { |
| 33 | +// if (hostingEnvironment == null) |
| 34 | +// { |
| 35 | +// throw new ArgumentNullException(nameof(hostingEnvironment)); |
| 36 | +// } |
| 37 | + |
| 38 | +// if (localizationOptions == null) |
| 39 | +// { |
| 40 | +// throw new ArgumentNullException(nameof(localizationOptions)); |
| 41 | +// } |
| 42 | + |
| 43 | +// _hostingEnvironment = hostingEnvironment; |
| 44 | +// _resourcesRelativePath = localizationOptions.Value.ResourcesPath ?? string.Empty; |
| 45 | +// if (!string.IsNullOrEmpty(_resourcesRelativePath)) |
| 46 | +// { |
| 47 | +// _resourcesRelativePath = _resourcesRelativePath.Replace(Path.AltDirectorySeparatorChar, '.') |
| 48 | +// .Replace(Path.DirectorySeparatorChar, '.') + "."; |
| 49 | +// } |
| 50 | +// } |
| 51 | + |
| 52 | +// /// <summary> |
| 53 | +// /// Creates a <see cref="ResourceManagerStringLocalizer"/> using the <see cref="Assembly"/> and |
| 54 | +// /// <see cref="Type.FullName"/> of the specified <see cref="Type"/>. |
| 55 | +// /// </summary> |
| 56 | +// /// <param name="resourceSource">The <see cref="Type"/>.</param> |
| 57 | +// /// <returns>The <see cref="ResourceManagerStringLocalizer"/>.</returns> |
| 58 | +// public IStringLocalizer Create(Type resourceSource) |
| 59 | +// { |
| 60 | +// if (resourceSource == null) |
| 61 | +// { |
| 62 | +// throw new ArgumentNullException(nameof(resourceSource)); |
| 63 | +// } |
| 64 | + |
| 65 | +// var typeInfo = resourceSource.GetTypeInfo(); |
| 66 | +// var assembly = typeInfo.Assembly; |
| 67 | + |
| 68 | +// // Re-root the base name if a resources path is set |
| 69 | +// string baseName; |
| 70 | +// if (assembly.FullName.StartsWith(_hostingEnvironment.ApplicationName)) |
| 71 | +// { |
| 72 | +// baseName = string.IsNullOrEmpty(_resourcesRelativePath) |
| 73 | +// ? typeInfo.FullName |
| 74 | +// : _hostingEnvironment.ApplicationName + "." + _resourcesRelativePath |
| 75 | +// + TrimPrefix(typeInfo.FullName, _hostingEnvironment.ApplicationName + "."); |
| 76 | +// } |
| 77 | +// else |
| 78 | +// { |
| 79 | +// var libName = TrimOnFirstComma(assembly.FullName); |
| 80 | +// baseName = libName + "." + TrimPrefix(typeInfo.FullName, libName + "."); |
| 81 | +// } |
| 82 | + |
| 83 | +// return _localizerCache.GetOrAdd(baseName, _ => |
| 84 | +// new ResourceManagerStringLocalizer( |
| 85 | +// new ResourceManager(baseName, assembly), |
| 86 | +// assembly, |
| 87 | +// baseName, |
| 88 | +// _resourceNamesCache) |
| 89 | +// ); |
| 90 | + |
| 91 | + |
| 92 | +// } |
| 93 | + |
| 94 | +// /// <summary> |
| 95 | +// /// Creates a <see cref="ResourceManagerStringLocalizer"/>. |
| 96 | +// /// </summary> |
| 97 | +// /// <param name="baseName">The base name of the resource to load strings from.</param> |
| 98 | +// /// <param name="location">The location to load resources from.</param> |
| 99 | +// /// <returns>The <see cref="ResourceManagerStringLocalizer"/>.</returns> |
| 100 | +// public IStringLocalizer Create(string baseName, string location) |
| 101 | +// { |
| 102 | +// if (baseName == null) |
| 103 | +// { |
| 104 | +// throw new ArgumentNullException(nameof(baseName)); |
| 105 | +// } |
| 106 | + |
| 107 | +// location = location ?? _hostingEnvironment.ApplicationName; |
| 108 | + |
| 109 | +// if (location.StartsWith(_hostingEnvironment.ApplicationName)) |
| 110 | +// { |
| 111 | +// baseName = location + "." + _resourcesRelativePath + TrimPrefix(baseName, location + "."); |
| 112 | +// } |
| 113 | +// else |
| 114 | +// { |
| 115 | +// baseName = location + "." + TrimPrefix(baseName, location + "."); |
| 116 | +// } |
| 117 | + |
| 118 | +// return _localizerCache.GetOrAdd($"B={baseName},L={location}", _ => |
| 119 | +// { |
| 120 | +// var assembly = Assembly.Load(new AssemblyName(location)); |
| 121 | +// return new ResourceManagerStringLocalizer( |
| 122 | +// new ResourceManager(baseName, assembly), |
| 123 | +// assembly, |
| 124 | +// baseName, |
| 125 | +// _resourceNamesCache); |
| 126 | +// }); |
| 127 | +// } |
| 128 | + |
| 129 | +// private static string TrimPrefix(string name, string prefix) |
| 130 | +// { |
| 131 | +// if (name.StartsWith(prefix, StringComparison.Ordinal)) |
| 132 | +// { |
| 133 | +// return name.Substring(prefix.Length); |
| 134 | +// } |
| 135 | + |
| 136 | +// return name; |
| 137 | +// } |
| 138 | + |
| 139 | +// private static string TrimOnFirstComma(string name) |
| 140 | +// { |
| 141 | + |
| 142 | +// return name.Substring(0, name.IndexOf(",")); |
| 143 | + |
| 144 | +// } |
| 145 | +// } |
| 146 | +//} |
0 commit comments