Make the ResourceManagerStringLocalizerFactory cache localizer instances
- #156
This commit is contained in:
parent
36bc26fb55
commit
96462b9528
|
|
@ -3,7 +3,6 @@
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Concurrent;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
|
@ -18,9 +17,7 @@ namespace Microsoft.Extensions.Localization
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class ResourceManagerStringLocalizer : IStringLocalizer
|
public class ResourceManagerStringLocalizer : IStringLocalizer
|
||||||
{
|
{
|
||||||
private readonly ConcurrentDictionary<string, object> _missingManifestCache =
|
private readonly Dictionary<string, object> _missingManifestCache = new Dictionary<string, object>();
|
||||||
new ConcurrentDictionary<string, object>();
|
|
||||||
|
|
||||||
private readonly IResourceNamesCache _resourceNamesCache;
|
private readonly IResourceNamesCache _resourceNamesCache;
|
||||||
private readonly ResourceManager _resourceManager;
|
private readonly ResourceManager _resourceManager;
|
||||||
private readonly AssemblyWrapper _resourceAssemblyWrapper;
|
private readonly AssemblyWrapper _resourceAssemblyWrapper;
|
||||||
|
|
@ -188,7 +185,7 @@ namespace Microsoft.Extensions.Localization
|
||||||
}
|
}
|
||||||
catch (MissingManifestResourceException)
|
catch (MissingManifestResourceException)
|
||||||
{
|
{
|
||||||
_missingManifestCache.TryAdd(cacheKey, null);
|
_missingManifestCache.Add(cacheKey, null);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,11 +2,12 @@
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Resources;
|
using System.Resources;
|
||||||
using Microsoft.Extensions.PlatformAbstractions;
|
|
||||||
using Microsoft.Extensions.OptionsModel;
|
using Microsoft.Extensions.OptionsModel;
|
||||||
|
using Microsoft.Extensions.PlatformAbstractions;
|
||||||
|
|
||||||
namespace Microsoft.Extensions.Localization
|
namespace Microsoft.Extensions.Localization
|
||||||
{
|
{
|
||||||
|
|
@ -16,7 +17,8 @@ namespace Microsoft.Extensions.Localization
|
||||||
public class ResourceManagerStringLocalizerFactory : IStringLocalizerFactory
|
public class ResourceManagerStringLocalizerFactory : IStringLocalizerFactory
|
||||||
{
|
{
|
||||||
private readonly IResourceNamesCache _resourceNamesCache = new ResourceNamesCache();
|
private readonly IResourceNamesCache _resourceNamesCache = new ResourceNamesCache();
|
||||||
|
private readonly ConcurrentDictionary<string, ResourceManagerStringLocalizer> _localizerCache =
|
||||||
|
new ConcurrentDictionary<string, ResourceManagerStringLocalizer>();
|
||||||
private readonly IApplicationEnvironment _applicationEnvironment;
|
private readonly IApplicationEnvironment _applicationEnvironment;
|
||||||
|
|
||||||
private readonly string _resourcesRelativePath;
|
private readonly string _resourcesRelativePath;
|
||||||
|
|
@ -67,11 +69,13 @@ namespace Microsoft.Extensions.Localization
|
||||||
|
|
||||||
var baseName = _applicationEnvironment.ApplicationName + "." + _resourcesRelativePath + typeInfo.FullName;
|
var baseName = _applicationEnvironment.ApplicationName + "." + _resourcesRelativePath + typeInfo.FullName;
|
||||||
|
|
||||||
return new ResourceManagerStringLocalizer(
|
return _localizerCache.GetOrAdd(baseName, _ =>
|
||||||
new ResourceManager(baseName, assembly),
|
new ResourceManagerStringLocalizer(
|
||||||
assembly,
|
new ResourceManager(baseName, assembly),
|
||||||
baseName,
|
assembly,
|
||||||
_resourceNamesCache);
|
baseName,
|
||||||
|
_resourceNamesCache)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -91,11 +95,13 @@ namespace Microsoft.Extensions.Localization
|
||||||
var assembly = Assembly.Load(new AssemblyName(rootPath));
|
var assembly = Assembly.Load(new AssemblyName(rootPath));
|
||||||
baseName = rootPath + "." + _resourcesRelativePath + baseName;
|
baseName = rootPath + "." + _resourcesRelativePath + baseName;
|
||||||
|
|
||||||
return new ResourceManagerStringLocalizer(
|
return _localizerCache.GetOrAdd(baseName, _ =>
|
||||||
new ResourceManager(baseName, assembly),
|
new ResourceManagerStringLocalizer(
|
||||||
assembly,
|
new ResourceManager(baseName, assembly),
|
||||||
baseName,
|
assembly,
|
||||||
_resourceNamesCache);
|
baseName,
|
||||||
|
_resourceNamesCache)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue