• Lexical
Show / Hide Table of Contents
  • Lexical.FileSystem
    • Introduction
    • Abstractions
      • IFileSystem
        • IFileSystem
        • IFileSystemBrowse
        • IFileSystemCreateDirectory
        • IFileSystemDelete
        • IFileSystemFileAttribute
        • IFileSystemMount
        • IFileSystemMove
        • IFileSystemObserve
        • IFileSystemOpen
      • IEvent
      • IEntry
      • IOption
      • IToken
    • FileSystem
    • VirtualFileSystem
    • MemoryFileSystem
    • EmbeddedFileSystem
    • HttpFileSystem
    • Decoration
    • IFileProvider
    • Utility
      • DisposeList
      • FileScanner
      • VisitTree
      • File Operation
  • Lexical.FileProvider
    • Introduction
    • Package
    • Package.Abstractions
    • Root
    • Zip
    • Dll
    • SharpCompress
    • SharpZipLib
    • FileScanner
    • Utils
  • Lexical.Localization
    • Introduction
    • Tutorial
    • Asset
      • IAsset
      • IStringAsset
    • Line
      • ILine
      • ILineFactory
      • ILineRoot
      • ILineFormat
      • ILineLogger
      • LineComparer
    • File
      • ILineReader
      • ILineWriter
      • Ini
      • Json
      • Xml
      • Resx
      • Resources
    • Miscellaneous
      • Plurality
      • ICulturePolicy
      • IStringFormat
      • Dependency Injection
    • Practices
      • Class Library
      • Class Library DI
      • Class Library DI opt.
  • Lexical.Utilities
    • Introduction
    • UnicodeString
    • FileScanner
    • Permutation
    • Tuples
    • StructList

Introduction

StringLocalizerFactoryAsset adapts IStringLocalizerFactory implementations to IAsset.

// Create IStringLocalizerFactory
LoggerFactory loggerFactory = new LoggerFactory();
loggerFactory.AddConsole(LogLevel.Trace);
IOptions<LocalizationOptions> options = Options.Create(new LocalizationOptions { ResourcesPath = "" });
IStringLocalizerFactory stringLocalizerFactory = new ResourceManagerStringLocalizerFactory(options, loggerFactory);

// Adapt IStringLocalizerFactory to IAsset
IAsset asset = new StringLocalizerFactoryAsset(stringLocalizerFactory);

// Create root
ILineRoot root = new LineRoot(asset, new CulturePolicy());

// There are .resx files in "Resources/ConsoleApp1.MyController" with keys "Success" and "Error"
ILine key = root
    .Assembly(Assembly.GetExecutingAssembly())
    .BaseName("ConsoleApp1.MyController")
    //.Type(typeof(ConsoleApp1.MyController1))
    .Key("Success")
    .Culture("sv");

// Retrieve string from IStringLocalizerFactory
string str = key.ToString();

There is an extension method .ToAsset() that makes the same conversion.

// Adapt IStringLocalizerFactory to IAsset
asset = stringLocalizerFactory.ToAsset();

StringLocalizerAsset adapts IStringLocalizer implementations to IAsset.

// Adapt IStringLocalizer to IAsset
//asset = new StringLocalizerAsset(stringLocalizer);

StringLocalizerRoot adapts IAsset implementations back to IStringLocalizerFactory. Read more.

// Create asset
var source = new Dictionary<string, string> { { "fi:ConsoleApp1.MyController:Success", "Onnistui" } };
IAsset asset = new StringAsset(source, LineParameterPrinter.Default);

// Create root
ILine root = new StringLocalizerRoot(asset, new CulturePolicy());

// Type cast to IStringLocalizerFactory
IStringLocalizerFactory stringLocalizerFactory = root as IStringLocalizerFactory;

// Get IStringLocalizer
IStringLocalizer stringLocalizer = stringLocalizerFactory.Create(typeof(ConsoleApp1.MyController));

// Assign culture
stringLocalizer = stringLocalizer.WithCulture(CultureInfo.GetCultureInfo("fi"));

// Get string
string str = stringLocalizer["Success"];

And to IStringLocalizer.

// Create asset
var source = new Dictionary<string, string> { { "fi:ConsoleApp1.MyController:Success", "Onnistui" } };
IAsset asset = new StringAsset(source, LineParameterPrinter.Default);

// Create root
ILine root = new StringLocalizerRoot(asset, new CulturePolicy());

// Set key
ILine key = root.Culture("fi").Type(typeof(ConsoleApp1.MyController));

// Type cast key to IStringLocalizer
IStringLocalizer stringLocalizer = key as IStringLocalizer;

// Get string
string str = stringLocalizer["Success"];

Links

  • Microsoft.Extensions.Localization.Abstractions (NuGet)
    • IStringLocalizer
    • IStringLocalizer<T>
    • IStringLocalizerFactory
  • Microsoft.Extensions.Localization (NuGet)
  • Lexical.Localization (NuGet)
    • StringLocalizerAsset
    • StringLocalizerRoot
    • StringLocalizerFactoryAsset
    • ResourceManagerStringLocalizerAsset
    • ResourceManagerStringLocalizerAssetSource
Back to top Copyright © 2015-2020 Toni Kalajainen