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
{
///
/// 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 verwendet werden.
/// Dann muss die Ressource in "Localization" XXX heissen.
///
public class FixedStringLocalizerFactory : IStringLocalizerFactory
{
private readonly ILogger _logger;
private readonly IResourceNamesCache _resourceNamesCache = (IResourceNamesCache)new ResourceNamesCache();
private readonly ConcurrentDictionary _localizerCache = new ConcurrentDictionary();
private readonly string _applicationName;
private readonly string _resourcesRelativePath;
///
/// Erstellt eine Instanz
///
/// Informationen über das Hosting Environment
/// Optionen
/// Instanz eines ILogger
public FixedStringLocalizerFactory(IHostEnvironment hostingEnvironment, IOptions localizationOptions, ILogger 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, '.') + ".";
}
///
/// Erstellt einen StringLocalizer vom gesuchten Typ
///
/// Typ. Name des Typs entspricht Name der Ressource-Datei
/// StringLocalizer
public IStringLocalizer Create(Type resourceSource)
{
if ((object)resourceSource == null)
throw new ArgumentNullException("resourceSource");
var baseName = resourceSource.Name;
var location = _applicationName;
return Create(baseName, location);
}
///
/// Erstellt einen StringLocalizer
///
/// Basis-Name. Wenn nicht Annotations, dann wird hier "Text" verwendet
/// Wird nicht verwendet. Ist immer "Localization"
/// StringLocalizer
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)(_ =>
{
Assembly assembly = Assembly.Load(new AssemblyName(location));
baseName = $"{location}.{baseName}";
return new ResourceManagerStringLocalizer(new ResourceManager(baseName, assembly), assembly, baseName, this._resourceNamesCache, _logger);
}));
}
}
}