Esempio n. 1
0
        public static void Main(string[] args)
        {
            // 1. Line
            {
                #region Snippet_1a
                ILine key = LineRoot.Global.PluralRules("Unicode.CLDR35").Key("Key").Format("Hello, {0}");
                #endregion Snippet_1a
            }

            {
                #region Snippet_1b
                ILineRoot root       = new LineRoot();
                ILine     hint1      = root.PluralRules("Unicode.CLDR35");
                ILine     section1   = hint1.Section("Section2");
                ILine     section1_1 = hint1.Section("Section1.1");
                ILine     key1_1_1   = section1_1.Key("Key1");
                ILine     key1_1_2   = section1_1.Key("Key2");
                ILine     value1_1_1 = key1_1_1.Format("Hello, {0}");
                // ...
                #endregion Snippet_1b
            }

            // 2. Parts
            {
                #region Snippet_2a
                IAsset resourceAsset = new ResourceDictionary(new Dictionary <ILine, byte[]>());
                ILine  line          = LineRoot.Global.Asset(resourceAsset);
                #endregion Snippet_2a
            }
            {
                #region Snippet_2b
                ILine line = LineRoot.Global.Logger(Console.Out, LineStatusSeverity.Ok);
                #endregion Snippet_2b
            }
            {
                #region Snippet_2c
                ICulturePolicy culturePolicy = new CulturePolicy().SetToCurrentCulture();
                ILine          line          = LineRoot.Global.CulturePolicy(culturePolicy);
                #endregion Snippet_2c
            }
            {
                #region Snippet_2d
                #endregion Snippet_2d
            }
            {
                #region Snippet_2e
                #endregion Snippet_2e
            }
            {
                #region Snippet_2f
                #endregion Snippet_2f
            }
            {
                #region Snippet_2g
                #endregion Snippet_2g
            }
            {
                #region Snippet_2h
                #endregion Snippet_2h
            }

            // 3. Hints
            {
                #region Snippet_3_
                IAsset    asset = LineReaderMap.Default.FileAsset("ILine\\Hints.ini");
                ILineRoot root  = new LineRoot(asset);
                Console.WriteLine(root.Key("Error").Value(DateTime.Now));
                Console.WriteLine(root.Key("Ok"));
                #endregion Snippet_3_
            }
            {
                #region Snippet_3a
                ILine line1 = LineRoot.Global.StringFormat(TextFormat.Default).String("Text");
                ILine line2 = LineRoot.Global.StringFormat("Lexical.Localization.StringFormat.TextFormat,Lexical.Localization")
                              .String("Text");
                #endregion Snippet_3a
                Console.WriteLine(line2);
            }
            {
                #region Snippet_3b
                IPluralRules pluralRules = PluralRulesResolver.Default.Resolve("Unicode.CLDR35");
                ILine        line1       = LineRoot.Global.PluralRules(pluralRules);
                ILine        line2       = LineRoot.Global.PluralRules("Unicode.CLDR35");
                ILine        line3       = LineRoot.Global.PluralRules("[Category=cardinal,Case=one]n=1[Category=cardinal,Case=other]true");
                #endregion Snippet_3b
                Console.WriteLine(line2);
            }
            {
                #region Snippet_3c
                IFormatProvider customFormat = new CustomFormat();
                ILine           line1        = LineRoot.Global.FormatProvider(customFormat).Format("{0:DATE}").Value(DateTime.Now);
                ILine           line2        = LineRoot.Global.FormatProvider("docs.CustomFormat,docs").Format("{0:DATE}").Value(DateTime.Now);
                #endregion Snippet_3c
                Console.WriteLine(line1);
                Console.WriteLine(line2);
            }
            {
                #region Snippet_3e
                ResolveSource[] resolveSequence =
                    new ResolveSource[] { ResolveSource.Inline, ResolveSource.Asset, ResolveSource.Line };

                IStringResolver stringResolver = new StringResolver(Resolvers.Default, resolveSequence);
                ILine           line           = LineRoot.Global.StringResolver(stringResolver);
                #endregion Snippet_3e
                Console.WriteLine(line);
            }
            {
                #region Snippet_3f
                ILine line = LineRoot.Global.StringResolver("Lexical.Localization.StringFormat.StringResolver");
                #endregion Snippet_3f
                Console.WriteLine(line);
            }
            {
                #region Snippet_3g
                ResolveSource[] resolveSequence =
                    new ResolveSource[] { ResolveSource.Inline, ResolveSource.Asset, ResolveSource.Line };

                IResourceResolver resourceResolver = new ResourceResolver(Resolvers.Default, resolveSequence);
                ILine             line             = LineRoot.Global.ResourceResolver(resourceResolver);
                #endregion Snippet_3g
                Console.WriteLine(line);
            }
            {
                #region Snippet_3h
                ILine line = LineRoot.Global.ResourceResolver("Lexical.Localization.Resource.ResourceResolver");
                #endregion Snippet_3h
                Console.WriteLine(line);
            }

            // 4. Keys
            {
                #region Snippet_4a
                #endregion Snippet_4a
            }
            {
                #region Snippet_4b
                #endregion Snippet_4b
            }
            {
                #region Snippet_4c
                #endregion Snippet_4c
            }
            {
                #region Snippet_4d
                #endregion Snippet_4d
            }
            {
                #region Snippet_4e
                #endregion Snippet_4e
            }
            {
                #region Snippet_4f
                #endregion Snippet_4f
            }
            {
                #region Snippet_4g
                #endregion Snippet_4g
            }
            {
                #region Snippet_4h
                #endregion Snippet_4h
            }

            // 5. Non-canonicals
            {
                #region Snippet_5a
                Assembly asm   = typeof(ILine_Examples).Assembly;
                ILine    line1 = LineRoot.Global.Assembly(asm);
                ILine    line2 = LineRoot.Global.Assembly("docs");
                #endregion Snippet_5a
            }
            {
                #region Snippet_5b
                CultureInfo culture = CultureInfo.GetCultureInfo("en");
                ILine       line1   = LineRoot.Global.Culture(culture);
                ILine       line2   = LineRoot.Global.Culture("en");
                #endregion Snippet_5b
            }
            {
                #region Snippet_5c
                ILine line1 = LineRoot.Global.Type <ILine_Examples>();
                ILine line2 = LineRoot.Global.Type(typeof(ILine_Examples));
                #endregion Snippet_5c
            }
            {
                #region Snippet_5d
                #endregion Snippet_5d
            }
            {
                #region Snippet_5e
                #endregion Snippet_5e
            }
            {
                #region Snippet_5f
                #endregion Snippet_5f
            }
            {
                #region Snippet_5g
                #endregion Snippet_5g
            }
            {
                #region Snippet_5h
                #endregion Snippet_5h
            }

            // 6. Canonicals
            {
                #region Snippet_6a
                ILine line = LineRoot.Global.Key("Ok");
                #endregion Snippet_6a
            }
            {
                #region Snippet_6b
                ILine line = LineRoot.Global.Section("Resources").Key("Ok");
                #endregion Snippet_6b
            }
            {
                #region Snippet_6c
                ILine line = LineRoot.Global.Location(@"c:\dir");
                #endregion Snippet_6c
            }
            {
                #region Snippet_6d
                ILine line = LineRoot.Global.BaseName("docs.Resources");
                #endregion Snippet_6d
            }
            {
                #region Snippet_6e
                #endregion Snippet_6e
            }
            {
                #region Snippet_6f
                #endregion Snippet_6f
            }
            {
                #region Snippet_6g
                #endregion Snippet_6g
            }
            {
                #region Snippet_6h
                #endregion Snippet_6h
            }

            // 7. Strings
            {
                #region Snippet_7a
                IString str  = CSharpFormat.Default.Parse("ErrorCode = 0x{0:X8}");
                ILine   line = LineRoot.Global.Key("Error").String(str);
                #endregion Snippet_7a
            }
            {
                #region Snippet_7b
                ILine line = LineRoot.Global.Key("Error").StringFormat(CSharpFormat.Default).String("ErrorCode = 0x{0:X8}");
                #endregion Snippet_7b
            }
            {
                #region Snippet_7c
                ILine line = LineRoot.Global.Key("Error").Format("ErrorCode = 0x{0:X8}");
                #endregion Snippet_7c
            }
            {
                #region Snippet_7c2
                int   code = 0x100;
                ILine line = LineRoot.Global.Key("Error").Format($"ErrorCode = 0x{code:X8}");
                #endregion Snippet_7c2
                Console.WriteLine(line);
            }
            {
                #region Snippet_7d
                ILine line = LineRoot.Global.Key("Hello").Text("Hello World");
                #endregion Snippet_7d
            }
            {
                #region Snippet_7e
                ILine line = LineRoot.Global.Section("Section").Key("Success")
                             .Format("Success")                        // Add inlining to the root culture ""
                             .Inline("Culture:en", "Success")          // Add inlining to culture "en"
                             .Inline("Culture:fi", "Onnistui")         // Add inlining to culture "fi"
                             .Inline("Culture:sv", "Det funkar");      // Add inlining to culture "sv"
                #endregion Snippet_7e
            }
            {
                #region Snippet_7f
                ILine line = LineRoot.Global.Section("Section").Key("Success")
                             .Format("Success")                         // Add inlining to the root culture ""
                             .en("Success")                             // Add inlining to culture "en"
                             .fi("Onnistui")                            // Add inlining to culture "fi"
                             .sv("Det funkar");                         // Add inlining to culture "sv"
                #endregion Snippet_7f
            }

            // Enumerations
            {
                #region Snippet_7l
                ILine carFeature = LineRoot.Global.Assembly("docs").Type <CarFeature>().Format("{0}");
                #endregion Snippet_7l
            }
            {
                #region Snippet_7l2
                ILine carFeature = LineRoot.Global.Assembly("docs").Type <CarFeature>().InlineEnum <CarFeature>().Format("{0}");
                #endregion Snippet_7l2
            }
            {
                #region Snippet_7l3
                IAssetSource assetSource = LineReaderMap.Default.FileAssetSource(@"ILine\CarFeature.ini");
                LineRoot.Builder.AddSource(assetSource).Build();
                #endregion Snippet_7l3
            }
            {
                #region Snippet_7m
                ILine carFeature = LineRoot.Global.Assembly("docs").Type <CarFeature>()
                                   .InlineEnum <CarFeature>()
                                   .InlineEnum(CarFeature.Electric, "fi", "Sähkö")
                                   .InlineEnum(CarFeature.Petrol, "fi", "Bensiini")
                                   .InlineEnum(CarFeature.NaturalGas, "fi", "Maakaasu")
                                   .InlineEnum(CarFeature.TwoDoors, "fi", "Kaksiovinen")
                                   .InlineEnum(CarFeature.FourDoors, "fi", "Neliovinen")
                                   .InlineEnum(CarFeature.FiveDoors, "fi", "Viisiovinen")
                                   .InlineEnum(CarFeature.Red, "fi", "Punainen")
                                   .InlineEnum(CarFeature.Black, "fi", "Musta")
                                   .InlineEnum(CarFeature.White, "fi", "Valkoinen")
                                   .Format("{0}");
                #endregion Snippet_7m

                CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("");
                #region Snippet_7m2
                Console.WriteLine(carFeature.Key(CarFeature.Petrol));
                Console.WriteLine(carFeature.Key(CarFeature.Petrol).Culture("fi"));
                Console.WriteLine(carFeature.Key(CarFeature.Petrol).Culture("sv"));
                #endregion Snippet_7m2

                #region Snippet_7m3
                CarFeature features = CarFeature.Petrol | CarFeature.FiveDoors | CarFeature.Black;
                Console.WriteLine(carFeature.Value(features));
                #endregion Snippet_7m3
                #region Snippet_7m5
                Console.WriteLine(carFeature.Formulate($"{CarFeature.Petrol | CarFeature.Black:|}").Culture("fi"));
                Console.WriteLine(carFeature.Formulate($"{CarFeature.Petrol | CarFeature.Black: |}").Culture("fi"));
                #endregion Snippet_7m5
            }
            {
                #region Snippet_7m6
                ILine carFeature = LineRoot.Global.Assembly("docs").Type <CarFeature>().InlineEnum <CarFeature>()
                                   .InlineEnum(CarFeature.Electric, "de", "Elektroauto")
                                   .InlineEnum(CarFeature.Petrol, "de", "Benzinwagen")
                                   .InlineEnum(CarFeature.NaturalGas, "de", "Erdgasauto")
                                   .InlineEnum(CarFeature.TwoDoors, "de", "Zweitürig")
                                   .InlineEnum(CarFeature.FourDoors, "de", "Viertürig")
                                   .InlineEnum(CarFeature.FiveDoors, "de", "Fünftürige")
                                   .InlineEnum(CarFeature.Red, "de", "Rot")
                                   .InlineEnum(CarFeature.Black, "de", "Schwartz")
                                   .InlineEnum(CarFeature.White, "de", "Weiß")
                                   .Format("{0}");

                ILine message = LineRoot.Global.Assembly("docs").Type("MyClass").Key("Msg")
                                .Format("Your car has following features: {0}")
                                .de("Ihr Auto hat folgende Eigenschaften: {0}");

                Console.WriteLine(message.Value(CarFeature.Petrol | CarFeature.Red | CarFeature.TwoDoors).Culture("fi"));
                Console.WriteLine(message.Value(CarFeature.Petrol | CarFeature.Red | CarFeature.TwoDoors).Culture("sv"));
                Console.WriteLine(message.Value(CarFeature.Petrol | CarFeature.Red | CarFeature.TwoDoors).Culture("de"));
                #endregion Snippet_7m6
            }
            {
                #region Snippet_7m7
                ILine carFeature = LineRoot.Global.Assembly("docs").Type <CarFeature>().InlineEnum <CarFeature>()
                                   .InlineEnum(CarFeature.Electric, "de", "Elektroauto")
                                   .InlineEnum(CarFeature.Petrol, "de", "Benzinwagen")
                                   .InlineEnum(CarFeature.NaturalGas, "de", "Erdgasauto")
                                   .InlineEnum(CarFeature.TwoDoors, "de", "Zweitürig")
                                   .InlineEnum(CarFeature.FourDoors, "de", "Viertürig")
                                   .InlineEnum(CarFeature.FiveDoors, "de", "Fünftürige")
                                   .InlineEnum(CarFeature.Red, "de", "Rot")
                                   .InlineEnum(CarFeature.Black, "de", "Schwartz")
                                   .InlineEnum(CarFeature.White, "de", "Weiß")
                                   .Format("{0}");

                ILine message = LineRoot.Global.Assembly("docs").Type("MyClass").Key("Msg")
                                .Format("Your car has following features: {0}")
                                .de("Ihr Auto hat folgende Eigenschaften: {0}");

                CarFeature features = CarFeature.Petrol | CarFeature.Red | CarFeature.TwoDoors;

                // Inlined enum strings don't work as Enum (unless tool is used)
                Console.WriteLine(message.Value(features).Culture("de"));
                // But works when ILine reference is used.
                Console.WriteLine(message.Value(carFeature.Value(features)).Culture("de"));
                #endregion Snippet_7m7
            }
            {
                ILine      carFeature = LineRoot.Global.Assembly("docs").Type <CarFeature>().InlineEnum <CarFeature>();
                CarFeature features   = CarFeature.Petrol | CarFeature.FiveDoors | CarFeature.Black;
                Console.WriteLine(carFeature.Formulate($"{features:g}").Culture("fi"));
                Console.WriteLine(carFeature.Formulate($"{features:G}").Culture("fi"));
                Console.WriteLine(carFeature.Formulate($"{features:f}").Culture("fi"));
                Console.WriteLine(carFeature.Formulate($"{features:F}").Culture("fi"));
                Console.WriteLine(carFeature.Formulate($"{features:d}").Culture("fi"));
                Console.WriteLine(carFeature.Formulate($"{features:D}").Culture("fi"));
                Console.WriteLine(carFeature.Formulate($"{features:x}").Culture("fi"));
                Console.WriteLine(carFeature.Formulate($"{features:X}").Culture("fi"));
            }

            // 8. Values
            {
                #region Snippet_8a
                ILine      line   = LineRoot.Global.Key("Error").Format("ErrorCode={0}").Value(0x100);
                LineString result = line.ResolveString();
                #endregion Snippet_8a
            }
            {
                #region Snippet_8b
                #endregion Snippet_8b
            }
            {
                #region Snippet_8c
                #endregion Snippet_8c
            }
            {
                #region Snippet_8d
                #endregion Snippet_8d
            }
            {
                #region Snippet_8e
                #endregion Snippet_8e
            }
            {
                #region Snippet_8f
                #endregion Snippet_8f
            }
            {
                #region Snippet_8g
                #endregion Snippet_8g
            }
            {
                #region Snippet_8h
                #endregion Snippet_8h
            }

            // 10. Resources
            {
                #region Snippet_10a
                ILine             line   = LineRoot.Global.Key("Error").Resource(new byte[] { 1, 2, 3 });
                LineResourceBytes result = line.ResolveBytes();
                #endregion Snippet_10a
            }
            {
                #region Snippet_10b
                ILine line = LineRoot.Global.Key("Error").Resource(new byte[] { 1, 2, 3 });
                using (LineResourceStream result = line.ResolveStream())
                {
                }
                #endregion Snippet_10b
            }
            {
                #region Snippet_10c
                #endregion Snippet_10c
            }
            {
                #region Snippet_10d
                #endregion Snippet_10d
            }
            {
                #region Snippet_10e
                #endregion Snippet_10e
            }
            {
                #region Snippet_10f
                #endregion Snippet_10f
            }
            {
                #region Snippet_10g
                #endregion Snippet_10g
            }
            {
                #region Snippet_10h
                #endregion Snippet_10h
            }
        }
Esempio n. 2
0
        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
            }
        }
Esempio n. 3
0
        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
            }
        }
Esempio n. 4
0
        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);
            }
        }
Esempio n. 5
0
        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
            }
        }