コード例 #1
0
        public static TextManager ApplyDefaults(Assembly appAssembly, TextManager manager, string overridesPath = null)
        {
            overridesPath = overridesPath ?? "~/App_Config/LocalizationEntries.xml";

            manager.StringEncoder = (s) =>
                                    HttpUtility.HtmlEncode(s ?? "")
                                    .Replace("\r\n", "\n")   // Windows to UNIX
                                    .Replace("\r", "<br />") //Mac
                                    .Replace("\n", "<br />") //Other
            ;

            manager.MissingTextHandler = (ns, key, lang, fallback) => !string.IsNullOrWhiteSpace(fallback)
                ? fallback
                : "(" + (string.IsNullOrEmpty(ns) || ns == manager.DefaultNamespace ? "" : ns + ".") + key + ")";

            string defaultNamespace = manager.GetNamespace(appAssembly);

            manager.DefaultNamespace = defaultNamespace;

            var overridePath = HostingEnvironment.MapPath(overridesPath);

            if (File.Exists(overridePath))
            {
                manager.Texts.Sources.Add(
                    new PrioritizedTextSource(XmlTextSource.Monitoring(appAssembly, manager, overridePath), 1));
            }

            manager.PrepareTextSources(defaultNamespace);

            return(manager);
        }
コード例 #2
0
        public override ITextSource GetSource(Assembly asm, TextManager textManager, string targetNamespace)
        {
            var source = XmlTextSource.ForAssembly(asm, ResourceName);

            if (source != null)
            {
                source.DefaultNamespace = targetNamespace;
            }
            else
            {
                throw new LocalizedFileNotFoundException(ResourceName, "Exceptions.ResourceNotFoundException",
                                                         defaultMessage: "The resource {0} could not be opened");
            }
            return(source);
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: paulsuart/rebelcmsxu5
        static void Main(string[] args)
        {
            var extractOptions = new ExtractOptions();
            var compareOptions = new CompareOptions();
            var parser = new CommandLineParser();

            if( args.Length > 0 )
            {
                if (args[0] == "extract")
                {
                    if (parser.ParseArguments(args, extractOptions))
                    {
                        extractOptions.RootDirectory = extractOptions.RootDirectory ??
                            Environment.CurrentDirectory;

                        Console.Out.WriteLine("");
                        Console.Out.WriteLine("Extracting entries from:");
                        Console.Out.WriteLine("\"" + extractOptions.RootDirectory + "\"");
                        Console.Out.WriteLine("Extensions: " + string.Join(", ", extractOptions.Extensions));
                        Console.Out.WriteLine("Exclude patterns: " + string.Join(", ", extractOptions.ExcludePatterns));
                        Console.Out.WriteLine();
                        Console.Out.WriteLine("Saving texts to: " + extractOptions.TargetFile);

                        var extractor = new CStyleLanguageTextExtractor();
                        extractor.MarkerLanguage = extractOptions.DefaultLanguage;

                        extractor.SourceFiles = new SourceFileList(
                            extractOptions.RootDirectory,
                            extractOptions.Extensions,
                            extractOptions.ExcludePatterns).GetFiles();

                        var target = new XmlTextSource();
                        target.Put(extractor.Get().Select(x =>
                            new LocalizedTextState { Text = x, Status = LocalizedTextStatus.New }), TextMergeOptions.KeepUnused);

                        target.Document.Save(extractOptions.TargetFile);

                        return;
                    }
                }
                else if (args[0] == "compare")
                {
                    if (parser.ParseArguments(args, compareOptions))
                    {

                        Console.WriteLine();

                        var texts = new TextSourceAggregator();
                        using (texts.BeginUpdate())
                        {
                            foreach (var file in compareOptions.Files)
                            {
                                texts.Sources.Add(new PrioritizedTextSource(new XmlTextSource(XDocument.Load(file)), 1));
                            }
                        }
                        

                        var comparer = new LanguageComparer(texts);
                        var result = comparer.Compare(
                            compareOptions.SourceLanguage, 
                            compareOptions.TargetLanguage);
                        
                        Console.WriteLine(result.Success ?
                            "OK - The text(s) defines the same keys for the languages" :
                            "The two languages differ");
                        Console.Out.WriteLine();                        
                        foreach (var text in result.MissingTexts)
                        {
                            Console.Out.WriteLine("Missing\t" + text.Key);                            
                        }
                        foreach (var text in result.UnmatchedTexts)
                        {
                            Console.Out.WriteLine("Unmatched\t" + text.Key);                            
                        }                        
                        return;
                    }
                }
            }

            Console.WriteLine();
            Console.WriteLine("Usage: ");
            Console.WriteLine();
            Console.WriteLine("Localization.exe extract");
            WriteHelp(extractOptions);
            Console.WriteLine();
            Console.WriteLine("Localization.exe compare");
            WriteHelp(compareOptions);
            Console.WriteLine();
        }
コード例 #4
0
        static void Main(string[] args)
        {
            var extractOptions = new ExtractOptions();
            var compareOptions = new CompareOptions();
            var parser         = new CommandLineParser();

            if (args.Length > 0)
            {
                if (args[0] == "extract")
                {
                    if (parser.ParseArguments(args, extractOptions))
                    {
                        extractOptions.RootDirectory = extractOptions.RootDirectory ??
                                                       Environment.CurrentDirectory;

                        Console.Out.WriteLine("");
                        Console.Out.WriteLine("Extracting entries from:");
                        Console.Out.WriteLine("\"" + extractOptions.RootDirectory + "\"");
                        Console.Out.WriteLine("Extensions: " + string.Join(", ", extractOptions.Extensions));
                        Console.Out.WriteLine("Exclude patterns: " + string.Join(", ", extractOptions.ExcludePatterns));
                        Console.Out.WriteLine();
                        Console.Out.WriteLine("Saving texts to: " + extractOptions.TargetFile);

                        var extractor = new CStyleLanguageTextExtractor();
                        extractor.MarkerLanguage = extractOptions.DefaultLanguage;

                        extractor.SourceFiles = new SourceFileList(
                            extractOptions.RootDirectory,
                            extractOptions.Extensions,
                            extractOptions.ExcludePatterns).GetFiles();

                        var target = new XmlTextSource();
                        target.Put(extractor.Get().Select(x =>
                                                          new LocalizedTextState {
                            Text = x, Status = LocalizedTextStatus.New
                        }), TextMergeOptions.KeepUnused);

                        target.Document.Save(extractOptions.TargetFile);

                        return;
                    }
                }
                else if (args[0] == "compare")
                {
                    if (parser.ParseArguments(args, compareOptions))
                    {
                        Console.WriteLine();

                        var texts = new TextSourceAggregator();
                        using (texts.BeginUpdate())
                        {
                            foreach (var file in compareOptions.Files)
                            {
                                texts.Sources.Add(new PrioritizedTextSource(new XmlTextSource(XDocument.Load(file)), 1));
                            }
                        }


                        var comparer = new LanguageComparer(texts);
                        var result   = comparer.Compare(
                            compareOptions.SourceLanguage,
                            compareOptions.TargetLanguage);

                        Console.WriteLine(result.Success ?
                                          "OK - The text(s) defines the same keys for the languages" :
                                          "The two languages differ");
                        Console.Out.WriteLine();
                        foreach (var text in result.MissingTexts)
                        {
                            Console.Out.WriteLine("Missing\t" + text.Key);
                        }
                        foreach (var text in result.UnmatchedTexts)
                        {
                            Console.Out.WriteLine("Unmatched\t" + text.Key);
                        }
                        return;
                    }
                }
            }

            Console.WriteLine();
            Console.WriteLine("Usage: ");
            Console.WriteLine();
            Console.WriteLine("Localization.exe extract");
            WriteHelp(extractOptions);
            Console.WriteLine();
            Console.WriteLine("Localization.exe compare");
            WriteHelp(compareOptions);
            Console.WriteLine();
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: RebelCMS/rebelcmsxu5
        static void Main(string[] args)
        {
            //TestSwitchCaseCondition("@Test", 1);


            TestSwitchCaseCondition("% 10 = 2 or (1 and < 1)", 1);
            TestSwitchCaseCondition("% 10 = 2 or (1 and < 1)", 12);
            TestSwitchCaseCondition("% 10 = 2 or 5, 6, 7, 8, 9", 7);
            TestSwitchCaseCondition("% 10 = 2 or 5, 6, 7, 8, 9", 15);


            TestSwitchCaseCondition(" +1 % 10 = 1", 10);

            TestSwitchCaseCondition(" + 1 % 10 = 1", 10);

            TestSwitchCaseCondition("%10=1", 1);
            TestSwitchCaseCondition("%10=1", 10);
            TestSwitchCaseCondition("%10=1", 25);
            TestSwitchCaseCondition("%10=1", 11);



            var dialect = new DefaultDialect();
            var manager = new DummyManager();


            var test = "Hej {P1: Format spec} <F1:   {Moo:tahr}> {Abe:kat} #Fisso{1:  Foo 10 #Nested{Boo:Hawrh!} | 10} #Moo{{ab}} #Foo{|?Gok}";

            var p = new DefaultExpressionParser();
            var e = p.Parse(test, null);

            e.Accept(new Printer());

            var eval = new PatternEvaluator(e);

            Console.Out.WriteLine();
            Console.Out.WriteLine(e.ToString());

            Console.Out.WriteLine("Custom expression part");
            var customTest = "Hej {Name}";

            e = p.Parse(customTest, null);
            e.Parts.Add(new TestPart());
            e.Accept(new PatternDecorator(manager, dialect));

            eval = new PatternEvaluator(e);


            e.Accept(new Printer());
            Console.Out.WriteLine();
            Console.Out.WriteLine(eval.Evaluate(
                                      CreateContext(null, manager.CurrentLanguage, new Dictionary <string, object> {
                { "Name", "John" }
            })));
            Console.Out.WriteLine();
            Console.Out.WriteLine(e.ToString());

            var ptest = @"The values are {Name} and <Bold: <Bold:{Test}> <Bold:{Test}>><Bold: {Number:N2} (roman: {Number:roman})>. #Number{1: Singularis: 1 ({#}) | Pluralis\: n ({#})}.
            
                Boolean Expression: #Number{2 or 5: Yes | No}

                ModuloTest: #Number{% 2 = 0: Yes | No}
            
                {Name} has {Name.Length:N2} characters

                Let's enumerate 
                Enum test 1: #EnumTest1{0:{#}|, {#}|-1:"" and {#}""}
                Enum test 2: #EnumTest2{0:{#}|, {#}|-1:"" and {#}""}";

            Console.Out.WriteLine();

            var evaluator = dialect.GetEvaluator(ptest, manager);

            Console.Out.WriteLine(evaluator.Evaluate(
                                      CreateContext(null, manager.CurrentLanguage, new Dictionary <string, object> {
                { "Name", "John Doe" },
                { "Number", 5 },
                { "Bold", "<b>{#}</b>" },
                { "EnumTest1", new string[] { "A", "B", "C", "D" } },
                { "EnumTest2", new string[] { "A" } },
                { "EnumTest3", new string[] { "A", "B" } },
                { "Test", true }
            })));


            var texts = new List <LocalizedText>
            {
                new LocalizedText {
                    Key = "LookupCondition", Pattern = "% 10 <= 2", Language = "en-US", PatternDialect = "Text"
                },

                new LocalizedText {
                    Key = "Plural1", Pattern = "1", Language = "en-US", PatternDialect = "Text"
                },
                new LocalizedText {
                    Key = "Plural2", Pattern = "<20", Language = "en-US", PatternDialect = "Text"
                },

                new LocalizedText {
                    Key = "ShortPlural", Pattern = "The number is #Plural(Number){Not plural|Plural|More plural}", Language = "en-US"
                },

                new LocalizedText {
                    Key = "PluralFun", Pattern = "The number is #Number{@LookupCondition: Plural|Not plural}", Language = "en-US"
                },

                new LocalizedText {
                    Key = "Arithmetic", Pattern = "Test #Number{% 10 = 1: Module | + 1 * 2 > 10: Combined}", Language = "en-US"
                },

                new LocalizedText {
                    Key = "Text one", Pattern = "Test one", Language = "en-US"
                },
                new LocalizedText {
                    Key = "Text two", Pattern = "Hello {UserName}", Language = "en-US"
                },
                new LocalizedText {
                    Key = "Text two", Pattern = "Hej {UserName}", Language = "da-DK"
                },
                new LocalizedText {
                    Key = "Enum", Pattern = @"#0{0: {#Index:roman}: {#}|, {#Index:roman}\: {#}|-1: "" {1} {#Index:roman}\: {#}""}", Language = "en-US"
                },

                new LocalizedText {
                    Key = "Reffed", Pattern = "Test {0} String literal {1}", Language = "en-US"
                },
                new LocalizedText {
                    Key = "Ref test 1", Pattern = "Ref 1: {@Reffed} and Another namespace {@Another__Reffed}", Language = "en-US"
                },
                new LocalizedText {
                    Key = "Ref test 2", Pattern = @"Ref 2: {@Reffed(UserName, ""Moo"")}", Language = "en-US"
                },
                //This one makes sense, and shows why pattern references are neat
                new LocalizedText {
                    Key = "Ref test 3", Pattern = @"You have selected {@Enum(List, 'and')}", Language = "en-US"
                },

                new LocalizedText {
                    Key = "Ref test 3", Namespace = "Another", Pattern = "Test one {@Reffed} and {@__Reffed}", Language = "en-US"
                },

                new LocalizedText {
                    Key = "Reffed", Namespace = "Another", Pattern = "I'm another", Language = "en-US"
                },

                new LocalizedText {
                    Key = "FormattedSwitch", Pattern = "Two decimals #Count:N2{1: One - ({#}) | More - {#}}", Language = "en-US"
                },
            };

            var xml = new XmlTextSource();

            xml.Put(texts.Select(x => new LocalizedTextState {
                Text = x, Status = LocalizedTextStatus.Unchanged
            }), TextMergeOptions.KeepUnused);

            xml.Document.Save("Texts.xml");


            xml          = new XmlTextSource();
            xml.Document = XDocument.Load("Texts.xml");


            manager.Texts.Sources.Add(new PrioritizedTextSource(xml));


            Console.Out.WriteLine(manager.Get("LookupCondition"));
            Console.Out.WriteLine(manager.Get("PluralFun", new { Number = 1 }));

            Console.Out.WriteLine("Shorthand:");
            Console.Out.WriteLine(manager.Get("ShortPlural", new { Number = 30 }));

            Console.Out.WriteLine(manager.Get("Text two", new { UserName = "******" }));

            Console.Out.WriteLine("Debug:");
            Console.Out.WriteLine(manager.Get("Text two", new { UserName = "******" }, debug: true));
            Console.Out.WriteLine();

            Console.Out.WriteLine(manager.Get("FormattedSwitch", new { Count = 7 }));

            Console.Out.WriteLine(manager.Get("Ref test 1", new { UserName = "******" }));
            Console.Out.WriteLine(manager.Get("Ref test 2", new { UserName = "******" }));

            Console.Out.WriteLine(manager.Get("Ref test 3", new { List = new[] { "Item 1", "Item 2", "Item 7", "Item 17" } }));
            Console.Out.WriteLine(manager.Get("Ref test 3", new { UserName = "******" }, ns: "Another"));

            string a = "Key";   /* @L10n @da-DK Foo @en-US Bar */
            string b = "Key 2"; // @L10n @da-DK Foo 2

            string.Format("Key 3" /* @L10n @da-DK Foo 3 */);



            string g = "Key"; /* @L10n @da-DK */


            string m = "Key" /* @L10n @en-US Goo */;

            //var extractor = new CStyleLanguageTextExtractor();
            //extractor.SourceFiles =
            //    new SourceFileList(@"C:\Users\niels.kuhnel\Stuff\Rebel\Rebel 5\I18n\i18n\Sandboxes\Localization\Sandbox.Localization.Tryout",
            //        new[] { "cs" }, new[] { "obj" }).GetFiles();

            //xml.Document = null;
            //xml.Put(extractor.Get().Select(x => new LocalizedTextState { Text = x }), TextMergeOptions.KeepUnused);
            //xml.Document.Save("Extracted.xml");


            Console.Out.WriteLine("Arithmetic");
            Console.Out.WriteLine(manager.Get("Arithmetic", new { Number = 11 }));

            Console.In.Read();
        }