Localization(ILineRoot linkedTo) : base(null, linkedTo) { // Add library's internal assets here Builder.AddSources(new AssetSources()); // Apply changes Builder.Build(); }
public static void Main(string[] args) { // Create a loader IAsset asset = IniLinesReader.Default.FileAsset("HelloWorld.ini"); // Add asset to global singleton instance LineRoot.Builder.AddAsset(asset); LineRoot.Builder.Build(); // Take reference of the root ILineRoot root = LineRoot.Global; // Create key ILine key = root.Type <Program>().Key("Hello").Format("Hello World!"); // Print with current culture Console.WriteLine(key); // Print with other cultures CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("de"); Console.WriteLine(key); CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("fi"); Console.WriteLine(key); CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("sv"); Console.WriteLine(key); Console.ReadKey(); }
public static void Main(string[] args) { #region Snippet_1 // Create service collection IServiceCollection serviceCollection = new ServiceCollection(); // Configure to use CultureInfo.CurrentUICulture serviceCollection.AddSingleton <ICulturePolicy>(new CulturePolicy().SetToCurrentThreadUICulture().AsReadonly()); // Add localization services: ILineRoot, ILine<T>, IAssetBuilder, ICulturePolicy serviceCollection.AddLexicalLocalization( addStringLocalizerService: false, addCulturePolicyService: false, useGlobalInstance: false, addCache: false); #endregion Snippet_1 #region Snippet_2 // Create localization source var lines = new List <ILine> { LineAppender.Default.Culture("en").Type("ConsoleApp1.MyController").Key("Hello").Format("Hello World!") }; // Create asset source IAssetSource assetSource = new StringAsset(lines).ToSource(); // Add asset source serviceCollection.AddSingleton <IAssetSource>(assetSource); #endregion Snippet_2 #region Snippet_3 // Build service provider using (var serviceProvider = serviceCollection.BuildServiceProvider()) { // Service can provide the asset IAsset asset = serviceProvider.GetService <IAsset>(); // Service can provide root ILineRoot root = serviceProvider.GetService <ILineRoot>(); // Service can provide type key ILine typeKey = serviceProvider.GetService <ILine <ConsoleApp1.MyController> >(); // Get "Hello World!" string str = typeKey.Key("Hello").Culture("en").ToString(); } #endregion Snippet_3 }
public MyController(ILineRoot localizationRoot) { this.localization = localizationRoot.Assembly("docs").Type <MyController>(); }
public MyController2(ILineRoot root) { this.localization = root.Type(GetType()); }
/// <summary> /// Adds the following Lexical.Localization services: /// <see cref="ILineRoot"/> /// <see cref="ILine{T}"/> /// <see cref="IAssetBuilder"/> /// /// If <paramref name="addCulturePolicyService"/> is true a <see cref="ICulturePolicy"/> is added, /// otherwise <see cref="ICulturePolicy"/> must be added to the service collection. /// /// Further services are needed: /// <see cref="IAssetSource"/> one or more. /// /// If <paramref name="addStringLocalizerService"/> is true, the following services are added: /// <see cref="IStringLocalizerFactory"/> /// <see cref="IStringLocalizer{T}"/> /// /// If <paramref name="useGlobalInstance"/> is true, then uses global <see cref="LineRoot"/>. /// /// /// After this call, the <paramref name="serviceCollection"/> still needs to be populated with /// instances of <see cref="IAssetSource"/>, such as: /// <see cref="ResourceStringDictionary"/> /// <see cref="StringAsset"/> /// /// </summary> /// <param name="serviceCollection"></param> /// <param name="addStringLocalizerService"></param> /// <param name="addCulturePolicyService">Add instance of <see cref="CulturePolicy"/></param> /// <param name="useGlobalInstance"></param> /// <param name="addCache"></param> /// <param name="addLogger">Add adapter for <see cref="ILogger"/></param> /// <returns></returns> public static IServiceCollection AddLexicalLocalization( this IServiceCollection serviceCollection, bool addStringLocalizerService = true, bool addCulturePolicyService = true, bool useGlobalInstance = false, bool addCache = false, bool addLogger = true ) { // ILineRoot if (useGlobalInstance) { // Use StringLocalizerRoot as ILineRoot serviceCollection.TryAdd(ServiceDescriptor.Singleton <ILineRoot>( s => { IAsset asset = s.GetService <IAsset>(); // DO NOT REMOVE ICulturePolicy culturePolicy = s.GetService <ICulturePolicy>(); if (culturePolicy != null) { StringLocalizerRoot.Global.CulturePolicy = culturePolicy; } return(StringLocalizerRoot.Global); } )); } else { serviceCollection.TryAdd(ServiceDescriptor.Singleton <ILineRoot, StringLocalizerRoot>()); } // ILineFactory serviceCollection.TryAdd(ServiceDescriptor.Singleton <ILineFactory>(StringLocalizerAppender.NonResolving)); // ILineAsset serviceCollection.TryAdd(ServiceDescriptor.Singleton <ILineAsset>(s => s.GetService <ILineRoot>() as ILineAsset)); // ICulturePolicy if (addCulturePolicyService) { if (useGlobalInstance) { serviceCollection.TryAdd(ServiceDescriptor.Singleton <ICulturePolicy>(StringLocalizerRoot.Global.CulturePolicy)); } else { serviceCollection.TryAdd(ServiceDescriptor.Singleton <ICulturePolicy>( s => new CulturePolicy().SetToCurrentThreadCulture() )); } } // ILineResolver serviceCollection.TryAdd(ServiceDescriptor.Singleton <IStringResolver>(StringResolver.Default)); // ILogger<ILine> if (addLogger) { serviceCollection.AddLoggerAdapter(); } // IAssetBuilder if (useGlobalInstance) { serviceCollection.TryAdd(ServiceDescriptor.Singleton <IAssetBuilder>(s => { IEnumerable <IAssetSource> assetSources = s.GetServices <IAssetSource>(); IAssetBuilder builder = StringLocalizerRoot.Builder; builder.AddSources(assetSources); return(builder); })); } else { serviceCollection.TryAdd(ServiceDescriptor.Singleton <IAssetBuilder>(s => { // Get IAssetSource services IEnumerable <IAssetSource> assetSources = s.GetServices <IAssetSource>(); // Get IEnumerable<ILibraryAssetSources> services IEnumerable <IAssetSources> libraryAssetSourcesLists = s.GetServices <IAssetSources>(); // Concatenate if (libraryAssetSourcesLists != null) { foreach (IEnumerable <IAssetSource> assetSources_ in libraryAssetSourcesLists) { assetSources = assetSources == null ? assetSources_ : assetSources.Concat(assetSources_); } } // Take distinct if (assetSources != null) { assetSources = assetSources.Distinct(); } // Is it still empty if (assetSources == null) { assetSources = new IAssetSource[0]; } // Create builder AssetBuilder.OneBuildInstance builder = new AssetBuilder.OneBuildInstance(assetSources); return(builder); })); } // IAsset serviceCollection.TryAdd(ServiceDescriptor.Singleton <IAsset>(s => s.GetService <IAssetBuilder>().Build())); // ILine<> serviceCollection.TryAdd(ServiceDescriptor.Singleton(typeof(ILine <>), typeof(StringLocalizerType <>))); // IStringLocalizer<> // IStringLocalizerFactory if (addStringLocalizerService) { // Service request for IStringLocalizer<> instances serviceCollection.TryAdd(ServiceDescriptor.Singleton(typeof(IStringLocalizer <>), typeof(StringLocalizerType <>))); // Service reqeust for IStringLocalizerFactory serviceCollection.TryAdd(ServiceDescriptor.Singleton <IStringLocalizerFactory>(s => { ILineRoot localizationRoot = s.GetService <ILineRoot>(); // Use the StringLocalizerKey or StringLocalizerRoot implementation from th service. if (localizationRoot is IStringLocalizerFactory casted) { return(casted); } // Create new root that implements IStringLocalizerFactory and acquires asset and policy with delegate return(new StringLocalizerRoot.LinkedTo(StringLocalizerAppender.NonResolving, localizationRoot)); })); } // Add cache if (addCache) { // Add cache serviceCollection.AddSingleton <IAssetSource>(new AssetCacheSource(o => o.AddResourceCache().AddStringsCache().AddCulturesCache())); } return(serviceCollection); }