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"];