private string[] GetKeyAndDescriptionList(StringAsset stringAsset) { if (stringAsset == null) { return(emptyStringAsset); } if (cachedPopupList.ContainsKey(stringAsset)) { if (cachedPopupList[stringAsset].Length == stringAsset.database.Count) { return(cachedPopupList[stringAsset]); } else { cachedPopupList.Remove(stringAsset); } } string[] list = new string[stringAsset.database.Count]; for (var i = 0; i < list.Length; i++) { list[i] = stringAsset.database[i].value.Default; } cachedPopupList.Add(stringAsset, list); return(list); }
private int GetCurrentSelectKeyIndex(StringAsset stringAsset, string finderGuid) { if (stringAsset == null) { return(0); } return(stringAsset.database.FindIndex(d => d.guid.CompareTo(finderGuid) == 0)); }
public static void Main(string[] args) { // Create asset var source = new Dictionary <string, string> { { "Culture:en:Key:hello", "Hello World!" } }; IAsset asset = new StringAsset(source, LineFormat.Parameters); #region Snippet // Create cache decorator IAssetCache asset_cached = new AssetCache(asset).AddResourceCache().AddStringsCache().AddCulturesCache(); #endregion Snippet // Assign the cached asset LineRoot.Global.Asset = asset_cached; }
public static void Main(string[] args) { // Create asset var source = new Dictionary <string, string> { { "Culture:en:Key:hello", "Hello World!" } }; IAsset asset = new StringAsset(source, LineFormat.Parameters); #region Snippet // Decorate with cache IAssetCache asset_cached = asset.CreateCache(); #endregion Snippet // Assign the asset with cache decoration LineRoot.Global.Asset = asset_cached; }
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 static void Main(string[] args) { #region Snippet // Create individual assets IAsset asset_1 = new StringAsset(new Dictionary <string, string> { { "Culture:en:Key:hello", "Hello World!" } }, LineFormat.Parameters); IAsset asset_2 = new ResourceStringDictionary(new Dictionary <string, byte[]> { { "Culture:en:Key:Hello.Icon", new byte[] { 1, 2, 3 } } }, LineFormat.Parameters); // Create composition asset IAssetComposition asset_composition = new AssetComposition(asset_1, asset_2); // Assign the composition to root ILineRoot root = new LineRoot(asset_composition, new CulturePolicy()); #endregion Snippet }
public static void Main(string[] args) { #region Snippet // Create asset var source = new Dictionary<string, string> { { "Culture:en:Key:hello", "Hello World!" } }; IAsset asset = new StringAsset(source, LineFormat.Parameters); // Cache it asset = asset.CreateCache(); // Issue a request which will be cached. ILine key = new LineRoot().Key("hello"); IString value = asset.GetLine( key.Culture("en") ).GetString(); Console.WriteLine(value); // Clear cache asset.Reload(); #endregion Snippet }
public static void Main(string[] args) { // Create asset Dictionary <string, string> strs = new Dictionary <string, string>(); strs["Culture:fi:Type:TutorialLibrary.MyController3:Key:OK"] = "Toiminto onnistui"; IAsset asset = new StringAsset() .Add(strs, LineFormat.Parameters) .Load(); // Create asset root IStringLocalizerFactory root = new StringLocalizerRoot(asset, new CulturePolicy()); // Call Controller CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("fi"); MyController3 controller3 = new MyController3(root); Console.WriteLine(controller3.Do()); }
public static void Main(string[] args) { { #region Snippet_1 // Language string source Dictionary <string, string> src = new Dictionary <string, string> { { "en:hello", "Hello World!" } }; // Create Asset IAsset asset = new StringAsset(src, LineParameterPrinter.Default); #endregion Snippet_1 #region Snippet_2 // Create key ILine key = new LineRoot().Key("hello").Culture("en"); // Resolve string - Call to StringAssetExtensions.GetString() IString str = asset.GetLine(key).GetString(); #endregion Snippet_2 } }
public static void Main(string[] args) { #region Snippet // 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, // IStringLocalizer<T>, IStringLocalizerFactory serviceCollection.AddLexicalLocalization( addStringLocalizerService: true, // <- string localizer addCulturePolicyService: false, useGlobalInstance: false, addCache: false); // 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); // Build service provider using (var serviceProvider = serviceCollection.BuildServiceProvider()) { // Get string localizer for class "ConsoleApp1.MyController". IStringLocalizer stringLocalizer = serviceProvider.GetService <IStringLocalizer <ConsoleApp1.MyController> >(); // Narrow scope down to "en" culture IStringLocalizer stringLocalizerScoped = stringLocalizer.WithCulture(CultureInfo.GetCultureInfo("en")); // Get "Hello World!" string str = stringLocalizerScoped.GetString("Hello"); } #endregion Snippet }
/// <summary> /// 要素取得. /// </summary> /// <param name='key'> /// Key. /// </param> public static string Get( string key ) { // 実体が無ければ新規生成. if( instance == null ) { instance = Resources.Load( "StringAsset/StringAsset" ) as StringAsset; findDictionary = new Dictionary<string, string>(); for( int i=0,imax=instance.key.Count; i<imax; i++ ) { findDictionary.Add( instance.key[i], instance.asset[i] ); } } string result; if( !findDictionary.TryGetValue( key, out result ) ) { // 要素がない場合はエラー. Debug.LogError( "The specified key did not exist." ); } return result; }
public static void Main(string[] args) { #region Snippet // Create asset var source = new Dictionary <string, string> { { "Culture:en:Key:hello", "Hello World!" } }; IAsset asset = new StringAsset(source, LineFormat.Parameters); // Create cache IAssetCache asset_cached = new AssetCache(asset); // Adds feature to cache IResourceAsset specific requests asset_cached.Add(new AssetCachePartResources(asset_cached.Source, asset_cached.Options)); // Adds feature to cache IStringAsset specific requests asset_cached.Add(new AssetCachePartStrings(asset_cached.Source, asset_cached.Options)); // Adds feature to cache IAssetCultureEnumerable specific requests asset_cached.Add(new AssetCachePartCultures(asset_cached.Source, asset_cached.Options)); // Assign the cached asset LineRoot.Global.Asset = asset_cached; #endregion Snippet }
public static void Main(string[] args) { { #region Snippet_0a // Create localization source var source = new Dictionary <string, string> { { "en/MyController/Hello", "Hello World!" } }; // Create key name policy ILineFormat policy = new LineParameterPrinter() .ParameterInfo(ParameterInfos.Default.Comparables(), prefixSeparator: "/") // Sorts parameters .DefaultRule(true, prefixSeparator: "/"); // Default separator // Create asset IAsset asset = new StringAsset(source, policy); // Create key ILine key = new LineRoot(asset).Section("MyController").Key("Hello"); // Retrieve string string str = key.Culture("en").ResolveString(); #endregion Snippet_0a #region Snippet_0b // Test if key converted correctly to expected identity "en/Section/Key" string id = policy.Print(key.Culture("en")); #endregion Snippet_0b } { #region Snippet_1 // Let's create an example key ILine key = new LineRoot() .Location("Patches") .Type("MyController") .Section("Errors") .Key("InvalidState") .Culture("en"); #endregion Snippet_1 { #region Snippet_2 // "en:Patches:MyController:Errors:InvalidState" string str1 = LineParameterPrinter.Default.Print(key); // "en.Patches.MyController.Errors.InvalidState" string str2 = LineParameterPrinter.Dot_Dot_Dot.Print(key); // "Patches:MyController:Errors:InvalidState" string str3 = LineParameterPrinter.None_Colon_Colon.Print(key); // "en:Patches.MyController.Errors.InvalidState" string str4 = LineParameterPrinter.Colon_Dot_Dot.Print(key); #endregion Snippet_2 } { #region Snippet_3 // Create a custom policy ILineFormat myPolicy = new LineParameterPrinter() // Enable non-canonical "Culture" parameter with "/" separator .Rule("Culture", true, "", "/") // Disable other non-canonical parts .NonCanonicalRule(false) // Enable canonical all parts with "/" separator .CanonicalRule(true, "/", "") // Set "Key" parameter's prefix to "/" and postfix to ".txt". .Rule("Key", true, "/", ".txt"); // "en/Patches/MyController/Errors/InvalidState.txt" string str = myPolicy.Print(key); #endregion Snippet_3 } { #region Snippet_4 // Create similiar policy with LinePattern ILineFormat myPolicy = new LinePattern("{culture/}{location/}{type/}{section/}[Key].txt"); // "en/Patches/MyController/Errors/InvalidState.txt" string str = myPolicy.Print(key); #endregion Snippet_4 } { #region Snippet_4a // Create name pattern ILineFormat myPolicy = new LinePattern("Patches/{Section}[-key]{-culture}.png"); #endregion Snippet_4a // "Patches/icons-ok-de.png" string str = myPolicy.Print(key); } { #region Snippet_4b // Create name pattern ILineFormat myPolicy = new LinePattern("{location_0/}{location_1/}{location_n/}{Section}{-key}{-culture}.png"); // Create key ILine key2 = new LineRoot().Location("Patches").Location("20181130").Section("icons").Key("ok").Culture("de"); // Converts to "Patches/20181130/icons-ok-de.png" string str = myPolicy.Print(key2); #endregion Snippet_4b } { #region Snippet_4c // Create name pattern with regular expression detail ILinePattern myPolicy = new LinePattern("{location<[^/]+>/}{Section}{-key}{-culture}.png"); // Use its regular expression Match match = myPolicy.Regex.Match("patches/icons-ok-de.png"); #endregion Snippet_4c } } }
public static void Main(string[] args) { { #region Snippet_0 // Create policy ICulturePolicyAssignable culturePolicy = new CulturePolicy(); #endregion Snippet_0 #region Snippet_1 // Create localization source var source = new Dictionary <string, string> { { "MyController:hello", "Hello World!" }, { "en:MyController:hello", "Hello World!" }, { "de:MyController:hello", "Hallo Welt!" } }; // Create asset with culture policy IAsset asset = new StringAsset(source, LineParameterPrinter.Default); // Create root and assign culturePolicy ILineRoot root = new LineRoot(asset, culturePolicy); #endregion Snippet_1 } { #region Snippet_2a // Set active culture and set fallback culture ICulturePolicy cultureArray_ = new CulturePolicy().SetCultures( CultureInfo.GetCultureInfo("en-US"), CultureInfo.GetCultureInfo("en"), CultureInfo.GetCultureInfo("") ); #endregion Snippet_2a } { #region Snippet_2b // Create policy from array of cultures ICulturePolicy culturePolicy = new CulturePolicy().SetCultures("en-US", "en", ""); #endregion Snippet_2b } { #region Snippet_2c // Create policy from culture, adds fallback cultures "en" and "". ICulturePolicy culturePolicy = new CulturePolicy().SetCultureWithFallbackCultures("en-US"); #endregion Snippet_2c } { #region Snippet_3a // Set to use CultureInfo.CurrentCulture ICulturePolicy culturePolicy = new CulturePolicy().SetToCurrentCulture(); // Change current culture CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en"); #endregion Snippet_3a } { #region Snippet_3b // Set to use CultureInfo.CurrentCulture ICulturePolicy culturePolicy = new CulturePolicy().SetToCurrentUICulture(); // Change current culture CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo("en"); #endregion Snippet_3b } { #region Snippet_4a // Set to use CultureInfo.CurrentCulture ICulturePolicy culturePolicy = new CulturePolicy().SetToCurrentThreadCulture(); // Change current thread's culture Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en"); #endregion Snippet_4a } { #region Snippet_4b // Set to use CultureInfo.CurrentCulture ICulturePolicy culturePolicy = new CulturePolicy().SetToCurrentThreadUICulture(); // Change current thread's culture Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en"); #endregion Snippet_4b } { #region Snippet_5 // Assign delegate ICulturePolicy culturePolicy = new CulturePolicy().SetFunc(() => CultureInfo.GetCultureInfo("fi")); #endregion Snippet_5 } { #region Snippet_6 // Assign delegate ICulturePolicy source = new CulturePolicy().SetToCurrentUICulture(); ICulturePolicy culturePolicy = new CulturePolicy().SetSourceFunc(() => source); #endregion Snippet_6 } { #region Snippet_7 // Freeze current culture ICulturePolicy culturePolicy = new CulturePolicy() .SetToCurrentCulture() .ToSnapshot() .AsReadonly(); #endregion Snippet_7 } { #region Snippet_8a // Default CulturePolicy Console.WriteLine(LineRoot.Global.Format("It is now {0:d} at {0:t}").Value(DateTime.Now)); #endregion Snippet_8a #region Snippet_8b // Format uses explicit CultureInfo "fi" Console.WriteLine(LineRoot.Global.Format("It is now {0:d} at {0:t}").Value(DateTime.Now).Culture("fi")); // Format uses explicit CultureInfo "sv" Console.WriteLine(LineRoot.Global.Format("It is now {0:d} at {0:t}").Value(DateTime.Now).Culture("sv")); // Format uses explicit CultureInfo "en" Console.WriteLine(LineRoot.Global.Format("It is now {0:d} at {0:t}").Value(DateTime.Now).Culture("en")); #endregion Snippet_8b #region Snippet_8c // C#'s String.Format uses Thread.CurrentCulture Console.WriteLine(String.Format("It is now {0:d} at {0:t}", DateTime.Now)); #endregion Snippet_8c } }
public static void RemoveCachedDictionary(StringAsset stringAsset) { cachedPopupList.Remove(stringAsset); }
public static void Main(string[] args) { { #region Snippet_0a // Create localization source var source = new Dictionary <string, string> { { "en/MyController/Hello", "Hello World!" } }; // Create key name policy ILineFormat policy = new LineParameterPrinter() .ParameterInfo(ParameterInfos.Default.Comparables(), prefixSeparator: "/") // Sorts parameters .DefaultRule(true, prefixSeparator: "/"); // Default separator // Create asset IAsset asset = new StringAsset(source, policy); // Create key ILine key = new LineRoot(asset).Section("MyController").Key("Hello"); // Retrieve string string str = key.Culture("en").ResolveString(); #endregion Snippet_0a #region Snippet_0b // Test if key converted correctly to expected identity "en/Section/Key" string id = policy.Print(key.Culture("en")); #endregion Snippet_0b } { #region Snippet_1 // Let's create an example key ILine key = LineAppender.NonResolving .Location("Patches") .Section("Controllers") .Type("MyController") .Section("Errors") .Key("InvalidState") .Culture("en"); #endregion Snippet_1 { #region Snippet_2 // "en:Patches:Controllers:MyController:Errors:InvalidState" string str1 = LineParameterPrinter.Default.Print(key); // "en.Patches.Controllers.MyController.Errors.InvalidState" string str2 = LineParameterPrinter.Dot_Dot_Dot.Print(key); // "Patches:Controllers:MyController:Errors:InvalidState" string str3 = LineParameterPrinter.None_Colon_Colon.Print(key); // "en:Patches.Controllers.MyController.Errors.InvalidState" string str4 = LineParameterPrinter.Colon_Dot_Dot.Print(key); // "en:Patches:Controllers:MyController:Errors.InvalidState" string str5 = LineParameterPrinter.Colon_Colon_Dot.Print(key); #endregion Snippet_2 } { #region Snippet_3 // Create a custom policy ILineFormat myPolicy = new LineParameterPrinter() // Enable non-canonical "Culture" parameter with "/" separator .Rule("Culture", true, postfixSeparator: "/", order: ParameterInfos.Default.GetValue("Culture").Order) // Disable other non-canonical parts .NonCanonicalRule(false) // Enable canonical all parts with "/" separator .CanonicalRule(true, prefixSeparator: "/") // Set "Key" parameter's prefix to "/" .Rule("Key", true, prefixSeparator: "/", order: ParameterInfos.Default.GetValue("Key").Order); // "en/Patches/MyController/Errors/InvalidState" string str = myPolicy.Print(key); #endregion Snippet_3 } } }
public static void Main(string[] args) { { #region Snippet_1a // Create localization source var source = new Dictionary <string, string> { { "Culture:en:Type:MyController:Key:hello", "Hello World!" } }; // Create asset IAsset asset = new StringAsset(source, LineFormat.Parameters); // Create culture policy ICulturePolicy culturePolicy = new CulturePolicy(); // Create root ILineRoot root = new LineRoot(asset, culturePolicy); #endregion Snippet_1a #region Snippet_1b // Construct key ILine key = root.Type("MyController").Key("Hello"); #endregion Snippet_1b #region Snippet_1c // Set active culture for this root (root.FindCulturePolicy() as ICulturePolicyAssignable).SetCultures("en", ""); // Provide string string str = key.ToString(); #endregion Snippet_1c } { // Create localization source var source = new Dictionary <string, string> { { "Culture:en:Section:Section:Key:Key", "Hello World!" } }; // Create asset IAsset asset = new StringAsset(source, LineFormat.Parameters); #region Snippet_5x // Create reference ILine key = LineAppender.NonResolving.Section("Section").Key("Key"); // Retreieve with reference IString str = asset.GetLine(key).GetString(); #endregion Snippet_5x } { #region Snippet_2a // Create key from global root ILine key = LineRoot.Global.Type("MyController").Key("Hello"); #endregion Snippet_2a #region Snippet_2b // Create localization source var source = new Dictionary <string, string> { { "Culture:en:Type:MyController:Key:hello", "Hello World!" } }; // Create asset IAsset asset = new StringAsset(source, LineFormat.Parameters); // Assets are added to global static builder. It must be (re-)built after adding. LineRoot.Builder.AddAsset(asset).Build(); #endregion Snippet_2b #region Snippet_2c // If ran in multi-threaded initialization, lock to LineRoot.Builder. lock (LineRoot.Builder) LineRoot.Builder.AddAsset(asset).Build(); #endregion Snippet_2c #region Snippet_2d // StringLocalizerRoot is root for IStringLocalizer interoperability IStringLocalizerFactory stringLocalizerFactory = StringLocalizerRoot.Global; #endregion Snippet_2d #region Snippet_2e // LineRoot and StringLocalizerRoot are interchangeable. They share the same asset(s). LineRoot.Builder.AddAsset(asset).Build(); IStringLocalizer <MyController> stringLocalizer = StringLocalizerRoot.Global.Type <MyController>().AsStringLocalizer <MyController>(); #endregion Snippet_2e #region Snippet_2f // Dynamic instance is acquired with LineRoot.GlobalDynamic dynamic key_ = LineRoot.GlobalDynamic.Section("Section").Key("Key"); #endregion Snippet_2f } }
public static void Main(string[] args) { { List <ILine> lines = new List <ILine> { LineAppender.Default.Type("ILineFactory_Examples").Key("Hello").Text("Hello World"), }; IAsset asset = new StringAsset().Add(lines); #region Snippet_0 ILine key = LineAppender.NonResolving.Type("ILineFactory_Examples").Key("Hello"); IString str = asset.GetLine(key).GetString(); #endregion Snippet_0 } { #region Snippet_1 ILine localization = LineAppender.Default .PluralRules("[Category=cardinal,Case=one]n=1[Category=cardinal,Case=other]true") .Assembly("docs") .Culture("en") .Type <ILineFactory_Examples>(); List <ILine> lines = new List <ILine> { localization.Key("OK").Text("Successful"), localization.Key("Error").Format("Failed (ErrorCode={0})") }; IAsset asset = new StringAsset().Add(lines); #endregion Snippet_1 } { #region Snippet_2 IStringLocalizer localizer = LineRoot.Global.Type("Hello").SetAppender(StringLocalizerAppender.Default).Key("Ok") as IStringLocalizer; #endregion Snippet_2 } { #region Snippet_3a ILine line = LineRoot.Global.AddAppender(new MyAppender()).Key("Ok"); #endregion Snippet_3a } { #region Snippet_4 #endregion Snippet_4 } { #region Snippet_5 #endregion Snippet_5 } { #region Snippet_6 #endregion Snippet_6 } { #region Snippet_7 #endregion Snippet_7 } { #region Snippet_8 #endregion Snippet_8 } }
public static void Main(string[] args) { { #region Snippet_1a // Create localization source var source = new Dictionary <string, string> { { "MyController:hello", "Hello World!" }, { "en:MyController:hello", "Hello World!" }, { "de:MyController:hello", "Hallo Welt!" } }; // Create asset with string source IAsset asset = new StringAsset().Add(source, "{Culture:}[Type:][Key]").Load(); #endregion Snippet_1a ILine key = new LineRoot(asset).Type("MyController").Key("hello"); Console.WriteLine(key); Console.WriteLine(key.Culture("en")); Console.WriteLine(key.Culture("de")); } { #region Snippet_1b // Create localization source var source = new List <ILine> { { LineFormat.Parameters.Parse("Type:MyController:Key:hello").Format("Hello World!") }, { LineFormat.Parameters.Parse("Culture:en:Type:MyController:Key:hello").Format("Hello World!") }, { LineFormat.Parameters.Parse("Culture:de:Type:MyController:Key:hello").Format("Hallo Welt!") } }; // Create asset with string source IAsset asset = new StringAsset().Add(source).Load(); #endregion Snippet_1b #region Snippet_2b ILine key = new LineRoot(asset).Type("MyController").Key("hello"); Console.WriteLine(key); Console.WriteLine(key.Culture("en")); Console.WriteLine(key.Culture("de")); #endregion Snippet_2b } { #region Snippet_1c // Create localization source var source = new Dictionary <ILine, string> { { new LineRoot().Type("MyController").Key("hello"), "Hello World!" }, { new LineRoot().Type("MyController").Key("hello").Culture("en"), "Hello World!" }, { new LineRoot().Type("MyController").Key("hello").Culture("de"), "Hallo Welt!" } }; // Create asset with string source IAsset asset = new StringAsset().Add(source).Load(); #endregion Snippet_1c #region Snippet_2c ILine key = new LineRoot(asset).Type("MyController").Key("hello"); Console.WriteLine(key); Console.WriteLine(key.Culture("en")); Console.WriteLine(key.Culture("de")); #endregion Snippet_2c } { var source = new Dictionary <string, string> { { "MyController:hello", "Hello World!" }, { "en:MyController:hello", "Hello World!" }, { "de:MyController:hello", "Hallo Welt!" } }; // Create asset with string source IAsset asset = new StringAsset().Add(source, "{Culture:}[Type:][Key]").Load(); #region Snippet_3a // Extract all keys foreach (var _key in asset.GetStringLines(null)) { Console.WriteLine(_key); } #endregion Snippet_3a } { #region Snippet_3b var source = new List <ILine> { LineAppender.Default.Type("MyController").Key("hello").Format("Hello World!"), LineAppender.Default.Type("MyController").Key("hello").Culture("en").Format("Hello World!"), LineAppender.Default.Type("MyController").Key("hello").Culture("de").Format("Hallo Welt!") }; // Keys can be filtered ILine filterKey = LineAppender.Default.Culture("de"); IAsset asset = new StringAsset().Add(source, "{Culture:}[Type:][Key]").Load(); foreach (var _key in asset.GetLines(filterKey)) { Console.WriteLine(_key.Print(LineFormat.ParametersInclString)); } #endregion Snippet_3b } }
public static void Main(string[] args) { { #region Snippet_1a ILine line = LineRoot.Global.Type("MyClass").Key("hello").Format("Hello, {0}."); #endregion Snippet_1a #region Snippet_1b Console.WriteLine(line.Value("Corellia Melody")); #endregion Snippet_1b #region Snippet_1c Console.WriteLine(LineRoot.Global.Format("It is now {0:d} at {0:t}").Value(DateTime.Now)); Console.WriteLine(String.Format("It is now {0:d} at {0:t}", DateTime.Now)); #endregion Snippet_1c #region Snippet_1c1 DateTime time = DateTime.Now; Console.WriteLine(LineRoot.Global.Key("Time").Formulate($"It is now {time:d} at {time:t}")); #endregion Snippet_1c1 #region Snippet_1c2 Console.WriteLine(LineRoot.Global.Format("It is now {0:d} at {0:t}").Culture("sv").Value(DateTime.Now)); Console.WriteLine(LineRoot.Global.Format("It is now {0:d} at {0:t}").Culture("de").Value(DateTime.Now)); #endregion Snippet_1c2 #region Snippet_1d LineString resolved_string = line.Value("Corellia Melody").ResolveString(); Console.WriteLine(resolved_string.Status); #endregion Snippet_1d #region Snippet_1e Permissions permissions = Permissions.Add | Permissions.Modify | Permissions.Remove; Console.WriteLine(LineRoot.Global.Key("Permission").Formulate($"User permissions {permissions}")); #endregion Snippet_1e } { #region Snippet_2 ILine line = LineRoot.Global .Key("hello") .Format("Hello, {0}.") .Inline("Culture:fi", "Hei, {0}") .Inline("Culture:de", "Hallo, {0}"); Console.WriteLine(line.Value("Corellia Melody")); #endregion Snippet_2 } { #region Snippet_3 ILine line = LineRoot.Global.PluralRules("Unicode.CLDR35") .Type("MyClass").Key("Cats") .Format("{cardinal:0} cat(s)") .Inline("N:zero", "no cats") .Inline("N:one", "a cat") .Inline("N:other", "{0} cats"); for (int cats = 0; cats <= 2; cats++) { Console.WriteLine(line.Value(cats)); } #endregion Snippet_3 } { #region Snippet_4 ILine line = LineRoot.Global.PluralRules("Unicode.CLDR35") .Type("MyClass").Key("Cats") .Format("{0} cat(s)") .Inline("Culture:en", "{cardinal:0} cat(s)") .Inline("Culture:en:N:zero", "no cats") .Inline("Culture:en:N:one", "a cat") .Inline("Culture:en:N:other", "{0} cats") .Inline("Culture:fi", "{cardinal:0} kissa(a)") .Inline("Culture:fi:N:zero", "ei kissoja") .Inline("Culture:fi:N:one", "yksi kissa") .Inline("Culture:fi:N:other", "{0} kissaa"); // Print with plurality and to culture "en" for (int cats = 0; cats <= 2; cats++) { Console.WriteLine(line.Culture("en").Value(cats)); } #endregion Snippet_4 } { #region Snippet_5a IAsset asset = LineReaderMap.Default.FileAsset("PluralityExample0a.xml"); LineRoot.Builder.AddAsset(asset).Build(); ILine line = LineRoot.Global.Key("Cats").Format("{0} cat(s)"); // Print with plurality for (int cats = 0; cats <= 2; cats++) { Console.WriteLine(line.Culture("fi").Value(cats)); } #endregion Snippet_5a } { #region Snippet_5b IAsset asset = LineReaderMap.Default.FileAsset("PluralityExample0a.xml"); ILineRoot root = new LineRoot(asset, new CulturePolicy()); ILine line = root.Key("Cats").Format("{0} cat(s)"); #endregion Snippet_5b // Print with plurality for (int cats = 0; cats <= 2; cats++) { Console.WriteLine(line.Culture("fi").Value(cats)); } } { #region Snippet_6 List <ILine> lines = new List <ILine> { LineAppender.Default.Type("MyClass").Key("Hello").Format("Hello, {0}"), LineAppender.Default.Type("MyClass").Key("Hello").Culture("fi").Format("Hei, {0}"), LineAppender.Default.Type("MyClass").Key("Hello").Culture("de").Format("Hallo, {0}") }; IAsset asset = new StringAsset().Add(lines).Load(); ILineRoot root = new LineRoot(asset, new CulturePolicy()); ILine line = root.Type("MyClass").Key("Hello"); Console.WriteLine(line.Value("Corellia Melody")); #endregion Snippet_6 } { #region Snippet_7 ILine root = LineRoot.Global.Logger(Console.Out, LineStatusSeverity.Ok); ILine line = root.Type("MyClass").Key("hello").Format("Hello, {0}."); Console.WriteLine(line.Value("Corellia Melody")); #endregion Snippet_7 } { #region Snippet_8 ILine line1a = LineRoot.Global.Key("Cats").Format("{0} cat(s)"); IString string1 = CSharpFormat.Default.Parse("{0} cat(s)"); ILine line1b = LineRoot.Global.Key("Cats").String(string1); ILine line2a = LineRoot.Global.Key("Ok").Text("{in braces}"); IString string2 = TextFormat.Default.Parse("{in braces}"); ILine line2b = LineRoot.Global.Key("Ok").String(string2); #endregion Snippet_8 } { #region Snippet_9 ICulturePolicy culturePolicy = new CulturePolicy().SetToCurrentThreadUICulture(); ILine root = new LineRoot(null, culturePolicy: culturePolicy); #endregion Snippet_9 } { #region Snippet_10 ILine line = StringLocalizerRoot.Global.Type("MyClass").Key("hello").Format("Hello, {0}."); IStringLocalizer localizer = line.AsStringLocalizer(); IStringLocalizerFactory localizerFactory = line.AsStringLocalizerFactory(); #endregion Snippet_10 } { #region Snippet_11 ILineRoot root = new StringLocalizerRoot(null, new CulturePolicy()); ILine line = root.Type("MyClass").Key("hello").Format("Hello, {0}."); IStringLocalizer localizer = line.AsStringLocalizer(); IStringLocalizerFactory localizerFactory = line.AsStringLocalizerFactory(); #endregion Snippet_11 } { #region Snippet_12 ILine line = LineRoot.Global.Type("MyClass").Key("hello").Format("Hello, {0}."); IStringLocalizer localizer = line.AsStringLocalizer(); IStringLocalizerFactory localizerFactory = line.AsStringLocalizerFactory(); #endregion Snippet_12 } { #region Snippet_13 #endregion Snippet_13 } { #region Snippet_14 #endregion Snippet_14 } { #region Snippet_15 #endregion Snippet_15 } }
public static void Main(string[] args) { #region Snippet_1 // Create localization source var source = new List <ILine> { LineFormat.Parameters.Parse("Culture:en:Type:MyController:Key:hello").Format("Hello World!") }; // Create asset IAsset asset = new StringAsset(source); // Create culture policy ICulturePolicy culturePolicy = new CulturePolicy(); // Create root ILineRoot root = new StringLocalizerRoot(asset, culturePolicy); #endregion Snippet_1 { #region Snippet_2 // Assign as IStringLocalizer, use "MyController" as root. IStringLocalizer stringLocalizer = root.Section("MyController").AsStringLocalizer(); #endregion Snippet_2 } { #region Snippet_3 // Assign as IStringLocalizerFactory IStringLocalizerFactory stringLocalizerFactory = root as IStringLocalizerFactory; // Adapt to IStringLocalizer IStringLocalizer <MyController> stringLocalizer2 = stringLocalizerFactory.Create(typeof(MyController)) as IStringLocalizer <MyController>; #endregion Snippet_3 } { #region Snippet_4a // Assign to IStringLocalizer for the class MyController IStringLocalizer <MyController> stringLocalizer = root.Type(typeof(MyController)).AsStringLocalizer <MyController>(); #endregion Snippet_4a } { #region Snippet_4b // Assign as IStringLocalizerFactory IStringLocalizerFactory stringLocalizerFactory = root as IStringLocalizerFactory; // Create IStringLocalizer for the class MyController IStringLocalizer <MyController> stringLocalizer = stringLocalizerFactory.Create(typeof(MyController)) as IStringLocalizer <MyController>; #endregion Snippet_4b } { #region Snippet_5a // Create IStringLocalizer and assign culture IStringLocalizer stringLocalizer = root.Culture("en").Type <MyController>().AsStringLocalizer(); #endregion Snippet_5a } { #region Snippet_5b // Assign as IStringLocalizerFactory IStringLocalizerFactory stringLocalizerFactory = root as IStringLocalizerFactory; // Create IStringLocalizer and assign culture IStringLocalizer stringLocalizer = stringLocalizerFactory.Create(typeof(MyController)) .WithCulture(CultureInfo.GetCultureInfo("en")); #endregion Snippet_5b stringLocalizer = (IStringLocalizer)root.Culture("en").Type <MyController>(); // Change language stringLocalizer = stringLocalizer.WithCulture(CultureInfo.GetCultureInfo("fi")); // Keep language stringLocalizer = stringLocalizer.WithCulture(CultureInfo.GetCultureInfo("fi")); // Change language stringLocalizer = stringLocalizer.WithCulture(CultureInfo.GetCultureInfo("sv")); // Remove language stringLocalizer = stringLocalizer.WithCulture(null); } }