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));
        }
        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));
        }
예제 #3
0
        public void TestLGResourceGroup()
        {
            var resourceExplorer = new ResourceExplorer().LoadProject(GetProjectFolder(), monitorChanges: false);

            // use LG file as entrance
            var lgResourceGroup = LGResourceLoader.GroupByLocale(resourceExplorer);

            Assert.Contains(string.Empty, lgResourceGroup.Keys.ToList());
            var resourceNames = lgResourceGroup[string.Empty].Select(u => u.Id);

            Assert.Equal(8, resourceNames.Count());
            Assert.Subset(new HashSet <string>()
            {
                "a.lg", "b.lg", "c.lg", "inject.lg", "NormalStructuredLG.lg", "root.lg", "subDialog.lg", "test.lg"
            }, new HashSet <string>(resourceNames));

            Assert.Contains("en-us", lgResourceGroup.Keys.ToList());
            resourceNames = lgResourceGroup["en-us"].Select(u => u.Id);
            Assert.Equal(8, resourceNames.Count());
            Assert.Subset(new HashSet <string>()
            {
                "a.en-US.lg", "b.en-us.lg", "c.en.lg", "inject.lg", "NormalStructuredLG.lg", "root.lg", "subDialog.lg", "test.en-US.lg"
            }, new HashSet <string>(resourceNames));

            Assert.Contains("en", lgResourceGroup.Keys.ToList());
            resourceNames = lgResourceGroup["en"].Select(u => u.Id);
            Assert.Equal(8, resourceNames.Count());
            Assert.Subset(new HashSet <string>()
            {
                "a.lg", "b.lg", "c.en.lg", "inject.lg", "NormalStructuredLG.lg", "root.lg", "subDialog.lg", "test.en.lg"
            }, new HashSet <string>(resourceNames));
        }
        public async Task TestMultiLangImport()
        {
            var resourceExplorer = new ResourceExplorer().LoadProject(GetProjectFolder(), monitorChanges: false);

            // use LG file as entrance
            var lgResourceGroup = LGResourceLoader.GroupByLocale(resourceExplorer);

            var resource  = resourceExplorer.GetResource("a.en-US.lg") as FileResource;
            var generator = new TemplateEngineLanguageGenerator(resource.FullName, lgResourceGroup);
            var result    = await generator.GenerateAsync(GetDialogContext(), "${templatea()}", null);

            Assert.AreEqual("from a.en-us.lg", result);

            // import b.en-us.lg
            result = await generator.GenerateAsync(GetDialogContext(), "${templateb()}", null);

            Assert.AreEqual("from b.en-us.lg", result);

            // fallback to c.en.lg
            result = await generator.GenerateAsync(GetDialogContext(), "${templatec()}", null);

            Assert.AreEqual("from c.en.lg", result);

            // there is no 'greeting' template in b.en-us.lg, no more fallback to b.lg
            var ex = await Assert.ThrowsExceptionAsync <Exception>(async() => await generator.GenerateAsync(GetDialogContext(), "${greeting()}", null));

            Assert.IsTrue(ex.Message.Contains("greeting does not have an evaluator"));

            resource  = resourceExplorer.GetResource("a.lg") as FileResource;
            generator = new TemplateEngineLanguageGenerator(resource.FullName, lgResourceGroup);

            result = await generator.GenerateAsync(GetDialogContext(), "${templatea()}", null);

            Assert.AreEqual("from a.lg", result);

            result = await generator.GenerateAsync(GetDialogContext(), "${templateb()}", null);

            Assert.AreEqual("from b.lg", result);

            // ignore the "en" in c.en.lg, just load c.lg
            result = await generator.GenerateAsync(GetDialogContext(), "${templatec()}", null);

            Assert.AreEqual("from c.lg", result);

            result = await generator.GenerateAsync(GetDialogContext(), "${greeting()}", null);

            Assert.AreEqual("hi", result);
        }
        private async Task <ITurnContext> GetTurnContext(string lgFile)
        {
            var context = new TurnContext(new TestAdapter(), new Activity());
            var lgText  = await resourceExplorer.GetResource(lgFile).ReadTextAsync();

            context.TurnState.Add <ILanguageGenerator>(new TemplateEngineLanguageGenerator(lgText, "test", LGResourceLoader.GroupByLocale(resourceExplorer)));
            return(context);
        }