Exemplo n.º 1
0
        public static void Main(string[] args)
        {
            // Create localizer
            IAssetBuilder       builder   = new AssetBuilder.OneBuildInstance();
            IAsset              asset     = builder.Build();
            StringLocalizerRoot localizer = new StringLocalizerRoot(asset, new CulturePolicy());

            // Install TutorialLibrary's IAssetSources
            Assembly library = typeof(MyClass).Assembly;

            builder.AddLibraryAssetSources(library).Build();

            #region Snippet
            // Install additional localization that was not available in the TutorialLibrary.
            IAssetSource assetSource = XmlLinesReader.Default.FileAssetSource("TutorialLibrary2-fi.xml");
            builder.AddSource(assetSource).Build();
            #endregion Snippet

            // Create class
            ILine <MyClass> classLocalizer = localizer.Type <MyClass>();
            MyClassB        myClass        = new MyClassB(classLocalizer);

            // Use the culture that was provided with the class library (AssetSources)
            CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("de");
            Console.WriteLine(myClass.Do());

            // Use the culture that was supplied above
            CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("fi");
            Console.WriteLine(myClass.Do());
        }
Exemplo n.º 2
0
        public static void Main(string[] args)
        {
            #region Snippet
            // Create localizer
            IAssetBuilder       builder   = new AssetBuilder.OneBuildInstance();
            IAsset              asset     = builder.Build();
            StringLocalizerRoot localizer = new StringLocalizerRoot(asset, new CulturePolicy());

            // Install TutorialLibrary's IAssetSources
            Assembly library = typeof(MyClass).Assembly;
            builder.AddLibraryAssetSources(library).Build();

            // Create class
            IStringLocalizer <MyClass> classLocalizer = localizer.Type <MyClass>().AsStringLocalizer <MyClass>();
            MyClass myClass = new MyClass(classLocalizer);

            // Use the culture that was provided with the class library (AssetSources)
            CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("de");
            Console.WriteLine(myClass.Do());
            #endregion Snippet
        }
Exemplo n.º 3
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);
            }
        }
Exemplo n.º 4
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
            }
        }