public async Task TestMultiLanguageGenerator()
        {
            var resourceExplorer = new ResourceExplorer().LoadProject(GetProjectFolder(), monitorChanges: false);

            var lg = new MultiLanguageGenerator();
            var multilanguageresources = LGResourceLoader.GroupByLocale(resourceExplorer);

            lg.LanguageGenerators[string.Empty] = new TemplateEngineLanguageGenerator(resourceExplorer.GetResource("test.lg").ReadTextAsync().Result, "test.lg", multilanguageresources);
            lg.LanguageGenerators["de"]         = new TemplateEngineLanguageGenerator(resourceExplorer.GetResource("test.de.lg").ReadTextAsync().Result, "test.de.lg", multilanguageresources);
            lg.LanguageGenerators["en"]         = new TemplateEngineLanguageGenerator(resourceExplorer.GetResource("test.en.lg").ReadTextAsync().Result, "test.en.lg", multilanguageresources);
            lg.LanguageGenerators["en-US"]      = new TemplateEngineLanguageGenerator(resourceExplorer.GetResource("test.en-US.lg").ReadTextAsync().Result, "test.en-US.lg", multilanguageresources);
            lg.LanguageGenerators["en-GB"]      = new TemplateEngineLanguageGenerator(resourceExplorer.GetResource("test.en-GB.lg").ReadTextAsync().Result, "test.en-GB.lg", multilanguageresources);
            lg.LanguageGenerators["fr"]         = new TemplateEngineLanguageGenerator(resourceExplorer.GetResource("test.fr.lg").ReadTextAsync().Result, "test.fr.lg", multilanguageresources);

            // test targeted in each language
            Assert.AreEqual("english-us", await lg.GenerateAsync(GetDialogContext(locale: "en-us"), "${test()}", null));
            Assert.AreEqual("english-gb", await lg.GenerateAsync(GetDialogContext(locale: "en-gb"), "${test()}", null));
            Assert.AreEqual("english", await lg.GenerateAsync(GetDialogContext(locale: "en"), "${test()}", null));
            Assert.AreEqual("default", await lg.GenerateAsync(GetDialogContext(locale: string.Empty), "${test()}", null));
            Assert.AreEqual("default", await lg.GenerateAsync(GetDialogContext(locale: "foo"), "${test()}", null));

            // test fallback for en-us -> en -> default
            //Assert.AreEqual("default2", await lg.Generate(GetTurnContext(locale: "en-us"), "${test2()}", null));
            Assert.AreEqual("default2", await lg.GenerateAsync(GetDialogContext(locale: "en-gb"), "${test2()}", null));
            Assert.AreEqual("default2", await lg.GenerateAsync(GetDialogContext(locale: "en"), "${test2()}", null));
            Assert.AreEqual("default2", await lg.GenerateAsync(GetDialogContext(locale: string.Empty), "${test2()}", null));
            Assert.AreEqual("default2", await lg.GenerateAsync(GetDialogContext(locale: "foo"), "${test2()}", null));
        }
Ejemplo n.º 2
0
        public async Task TestMultiLanguageGenerator()
        {
            var resourceExplorer = new ResourceExplorer().LoadProject(GetProjectFolder(), monitorChanges: false);

            // Configure last locale fallback to be non-overlapping with the explicitly set languages so we can
            // verify that when zero locale information is provided we default to it
            Thread.CurrentThread.CurrentCulture = new CultureInfo("es-ar");

            var lg = new MultiLanguageGenerator();
            var multilanguageresources = LGResourceLoader.GroupByLocale(resourceExplorer);

            lg.LanguageGenerators[string.Empty] = new TemplateEngineLanguageGenerator(resourceExplorer.GetResource("test.lg"), multilanguageresources);
            lg.LanguageGenerators["de"]         = new TemplateEngineLanguageGenerator(resourceExplorer.GetResource("test.de.lg"), multilanguageresources);
            lg.LanguageGenerators["en"]         = new TemplateEngineLanguageGenerator(resourceExplorer.GetResource("test.en.lg"), multilanguageresources);
            lg.LanguageGenerators["en-US"]      = new TemplateEngineLanguageGenerator(resourceExplorer.GetResource("test.en-US.lg"), multilanguageresources);
            lg.LanguageGenerators["en-GB"]      = new TemplateEngineLanguageGenerator(resourceExplorer.GetResource("test.en-GB.lg"), multilanguageresources);
            lg.LanguageGenerators["fr"]         = new TemplateEngineLanguageGenerator(resourceExplorer.GetResource("test.fr.lg"), multilanguageresources);

            // test targeted in each language
            Assert.Equal("english-us", await lg.GenerateAsync(GetDialogContext(locale: "en-us"), "${test()}", null));
            Assert.Equal("english-gb", await lg.GenerateAsync(GetDialogContext(locale: "en-gb"), "${test()}", null));
            Assert.Equal("english", await lg.GenerateAsync(GetDialogContext(locale: "en"), "${test()}", null));
            Assert.Equal("default", await lg.GenerateAsync(GetDialogContext(locale: string.Empty), "${test()}", null));
            Assert.Equal("default", await lg.GenerateAsync(GetDialogContext(locale: "foo"), "${test()}", null));

            // test fallback for en-us -> en -> default
            //Assert.Equal("default2", await lg.Generate(GetTurnContext(locale: "en-us"), "${test2()}", null));
            Assert.Equal("default2", await lg.GenerateAsync(GetDialogContext(locale: "en-gb"), "${test2()}", null));
            Assert.Equal("default2", await lg.GenerateAsync(GetDialogContext(locale: "en"), "${test2()}", null));
            Assert.Equal("default2", await lg.GenerateAsync(GetDialogContext(locale: string.Empty), "${test2()}", null));
            Assert.Equal("default2", await lg.GenerateAsync(GetDialogContext(locale: "foo"), "${test2()}", null));
        }