gehgassi_backend/gehGassi.Common/FixedStringLocalizerFactory.cs

87 lines
4.2 KiB
C#

using System;
using System.Collections.Concurrent;
using System.IO;
using System.Reflection;
using System.Resources;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace gehGassi.Common
{
/// <summary>
/// String-Localizer Factory die mit fixen Ressourcen in der Assembly "Localization" arbeitet.
/// Wenn nichts angegeben wird "Text" verwendet.
/// "Annotations" ist auch ein Sonderfall der definiert ist.
/// Soll eine andere Ressource in der Assembly "Localization" verwendet werden, dann muss z.B. IStringLocalizer<XXX> verwendet werden.
/// Dann muss die Ressource in "Localization" XXX heissen.
/// </summary>
public class FixedStringLocalizerFactory : IStringLocalizerFactory
{
private readonly ILogger _logger;
private readonly IResourceNamesCache _resourceNamesCache = (IResourceNamesCache)new ResourceNamesCache();
private readonly ConcurrentDictionary<string, ResourceManagerStringLocalizer> _localizerCache = new ConcurrentDictionary<string, ResourceManagerStringLocalizer>();
private readonly string _applicationName;
private readonly string _resourcesRelativePath;
/// <summary>
/// Erstellt eine Instanz
/// </summary>
/// <param name="hostingEnvironment">Informationen über das Hosting Environment</param>
/// <param name="localizationOptions">Optionen</param>
/// <param name="logger">Instanz eines ILogger</param>
public FixedStringLocalizerFactory(IHostEnvironment hostingEnvironment, IOptions<LocalizationOptions> localizationOptions, ILogger<FixedStringLocalizerFactory> logger)
{
if (hostingEnvironment == null)
throw new ArgumentNullException("hostingEnvironment");
if (localizationOptions == null)
throw new ArgumentNullException("localizationOptions");
_logger = logger;
this._applicationName = hostingEnvironment.ApplicationName;
this._resourcesRelativePath = localizationOptions.Value.ResourcesPath ?? string.Empty;
if (string.IsNullOrEmpty(this._resourcesRelativePath))
return;
this._resourcesRelativePath = this._resourcesRelativePath.Replace(Path.AltDirectorySeparatorChar, '.').Replace(Path.DirectorySeparatorChar, '.') + ".";
}
/// <summary>
/// Erstellt einen StringLocalizer vom gesuchten Typ
/// </summary>
/// <param name="resourceSource">Typ. Name des Typs entspricht Name der Ressource-Datei</param>
/// <returns>StringLocalizer</returns>
public IStringLocalizer Create(Type resourceSource)
{
if ((object)resourceSource == null)
throw new ArgumentNullException("resourceSource");
var baseName = resourceSource.Name;
var location = _applicationName;
return Create(baseName, location);
}
/// <summary>
/// Erstellt einen StringLocalizer
/// </summary>
/// <param name="baseName">Basis-Name. Wenn nicht Annotations, dann wird hier "Text" verwendet</param>
/// <param name="location">Wird nicht verwendet. Ist immer "Localization"</param>
/// <returns>StringLocalizer</returns>
public IStringLocalizer Create(string baseName, string location)
{
if (baseName == null)
throw new ArgumentNullException("baseName");
if (baseName.ToUpperInvariant() != "ANNOTATIONS" && baseName.ToUpperInvariant() != "IDENTITYERROR" && baseName.ToUpperInvariant() != "CLIENT")
baseName = "Text";
location = "gehGassi.Common";
return (IStringLocalizer)this._localizerCache.GetOrAdd(string.Format("B={0},L={1}", (object)baseName, (object)location), (Func<string, ResourceManagerStringLocalizer>)(_ =>
{
Assembly assembly = Assembly.Load(new AssemblyName(location));
baseName = $"{location}.{baseName}";
return new ResourceManagerStringLocalizer(new ResourceManager(baseName, assembly), assembly, baseName, this._resourceNamesCache, _logger);
}));
}
}
}