Skip to content
This repository was archived by the owner on Nov 29, 2018. It is now read-only.

Commit e51bea8

Browse files
committed
Initial attempt to cache walk of the culture tree when building resource name list:
- #15
1 parent b86af22 commit e51bea8

File tree

1 file changed

+34
-15
lines changed

1 file changed

+34
-15
lines changed

src/Microsoft.Framework.Localization/ResourceManagerStringLocalizer.cs

+34-15
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public class ResourceManagerStringLocalizer : IStringLocalizer
2020
{
2121
private readonly ConcurrentDictionary<MissingManifestCacheKey, object> _missingManifestCache =
2222
new ConcurrentDictionary<MissingManifestCacheKey, object>();
23+
private readonly ConcurrentDictionary<string, IList<string>> _cache = new ConcurrentDictionary<string, IList<string>>();
24+
2325

2426
/// <summary>
2527
/// Creates a new <see cref="ResourceManagerStringLocalizer"/>.
@@ -155,21 +157,7 @@ private IEnumerable<string> GetResourceNamesFromCultureHierarchy(CultureInfo sta
155157
{
156158
try
157159
{
158-
var resourceStreamName = ResourceBaseName;
159-
if (!string.IsNullOrEmpty(currentCulture.Name))
160-
{
161-
resourceStreamName += "." + currentCulture.Name;
162-
}
163-
resourceStreamName += ".resources";
164-
using (var cultureResourceStream = ResourceAssembly.GetManifestResourceStream(resourceStreamName))
165-
using (var resources = new ResourceReader(cultureResourceStream))
166-
{
167-
foreach (DictionaryEntry entry in resources)
168-
{
169-
var resourceName = (string)entry.Key;
170-
resourceNames.Add(resourceName);
171-
}
172-
}
160+
GetCultureThings(currentCulture, resourceNames);
173161
}
174162
catch (MissingManifestResourceException) { }
175163

@@ -185,6 +173,37 @@ private IEnumerable<string> GetResourceNamesFromCultureHierarchy(CultureInfo sta
185173
return resourceNames;
186174
}
187175

176+
private void GetCultureThings(CultureInfo currentCulture, HashSet<string> resourceNames)
177+
{
178+
var resourceStreamName = ResourceBaseName;
179+
if (!string.IsNullOrEmpty(currentCulture.Name))
180+
{
181+
resourceStreamName += "." + currentCulture.Name;
182+
}
183+
resourceStreamName += ".resources";
184+
185+
var rNames = _cache.GetOrAdd(resourceStreamName, key =>
186+
{
187+
var names = new List<string>();
188+
using (var cultureResourceStream = ResourceAssembly.GetManifestResourceStream(key))
189+
using (var resources = new ResourceReader(cultureResourceStream))
190+
{
191+
foreach (DictionaryEntry entry in resources)
192+
{
193+
var resourceName = (string)entry.Key;
194+
names.Add(resourceName);
195+
}
196+
}
197+
198+
return names;
199+
});
200+
201+
foreach (var resourceName in rNames)
202+
{
203+
resourceNames.Add(resourceName);
204+
}
205+
}
206+
188207
private class MissingManifestCacheKey : IEquatable<MissingManifestCacheKey>
189208
{
190209
private readonly int _hashCode;

0 commit comments

Comments
 (0)