public void ViewLocalizer_GetAllStringsIncludeParentCulture_ReturnsLocalizedString()
        {
            // Arrange
            var stringLocalizer    = new TestStringLocalizer();
            var htmlLocalizer      = new HtmlLocalizer(stringLocalizer);
            var hostingEnvironment = new Mock <IHostingEnvironment>();

            hostingEnvironment.Setup(a => a.ApplicationName).Returns("TestApplication");
            var viewLocalizer = new ViewLocalizer(new TestHtmlLocalizerFactory(), hostingEnvironment.Object);

            var view = new Mock <IView>();

            view.Setup(v => v.Path).Returns("example");
            var viewContext = new ViewContext();

            viewContext.View = view.Object;

            viewLocalizer.Contextualize(viewContext);

            // Act
            var allLocalizedStrings = viewLocalizer.GetAllStrings(includeParentCultures: true).ToList();

            // Assert
            Assert.Equal(2, allLocalizedStrings.Count);
            Assert.Equal("World", allLocalizedStrings[0].Value);
            Assert.Equal("Bar", allLocalizedStrings[1].Value);
        }
        public void ViewLocalizer_HtmlWithArguments_ReturnsLocalizedHtmlString()
        {
            // Arrange
            var stringLocalizer    = new TestStringLocalizer();
            var htmlLocalizer      = new HtmlLocalizer(stringLocalizer);
            var hostingEnvironment = new Mock <IHostingEnvironment>();

            hostingEnvironment.Setup(a => a.ApplicationName).Returns("TestApplication");
            var viewLocalizer = new ViewLocalizer(new TestHtmlLocalizerFactory(), hostingEnvironment.Object);

            var view = new Mock <IView>();

            view.Setup(v => v.Path).Returns("example");
            var viewContext = new ViewContext();

            viewContext.View = view.Object;

            viewLocalizer.Contextualize(viewContext);

            // Act
            var actualLocalizedString = viewLocalizer.GetHtml("John", "Doe");

            // Assert
            Assert.Equal("Hello John Doe", actualLocalizedString.Value);
        }
        public void ViewLocalizer_WithCulture_ReturnsLocalizedHtmlString()
        {
            // Arrange
            var stringLocalizer    = new TestStringLocalizer();
            var htmlLocalizer      = new HtmlLocalizer(stringLocalizer);
            var hostingEnvironment = new Mock <IHostingEnvironment>();

            hostingEnvironment.Setup(a => a.ApplicationName).Returns("TestApplication");
            var viewLocalizer = new ViewLocalizer(new TestHtmlLocalizerFactory(), hostingEnvironment.Object);

            var view = new Mock <IView>();

            view.Setup(v => v.Path).Returns("example");
            var viewContext = new ViewContext();

            viewContext.View = view.Object;

            viewLocalizer.Contextualize(viewContext);

            // Act
            var actualLocalizedString = viewLocalizer.WithCulture(new CultureInfo("fr"))["John"];

            // Assert
            Assert.Equal("Bonjour John", actualLocalizedString.Value);
        }
Exemple #4
0
        public void HtmlLocalizer_HtmlWithArguments_ReturnsLocalizedHtml(
            string format,
            object[] arguments,
            string expectedText)
        {
            // Arrange
            var localizedString = new LocalizedString("Hello", format);

            var stringLocalizer = new Mock <IStringLocalizer>();

            stringLocalizer.Setup(s => s["Hello"]).Returns(localizedString);

            var htmlLocalizer = new HtmlLocalizer(stringLocalizer.Object);

            // Act
            var localizedHtmlString = htmlLocalizer.GetHtml("Hello", arguments);

            // Assert
            Assert.NotNull(localizedHtmlString);
            Assert.Equal(format, localizedHtmlString.Value);
            using (var writer = new StringWriter())
            {
                localizedHtmlString.WriteTo(writer, new HtmlTestEncoder());
                Assert.Equal(expectedText, writer.ToString());
            }
        }
Exemple #5
0
        public void HtmlLocalizerOfTTest_UseIndexerWithArguments_ReturnsLocalizedString()
        {
            // Arrange
            var applicationEnvironment = new Mock <IApplicationEnvironment>();

            applicationEnvironment.Setup(a => a.ApplicationName).Returns("TestApplication");

            var localizedString = new LocalizedString("Hello", "Bonjour test");

            var htmlLocalizer = new Mock <IHtmlLocalizer>();

            htmlLocalizer.Setup(h => h["Hello", "test"]).Returns(localizedString);

            var htmlLocalizerFactory = new Mock <IHtmlLocalizerFactory>();

            htmlLocalizerFactory.Setup(h => h.Create(typeof(TestClass)))
            .Returns(htmlLocalizer.Object);

            var htmlLocalizerOfT = new HtmlLocalizer <TestClass>(htmlLocalizerFactory.Object);

            // Act
            var actualLocalizedString = htmlLocalizerOfT["Hello", "test"];

            // Assert
            Assert.Equal(localizedString, actualLocalizedString);
        }
Exemple #6
0
        public void HtmlLocalizer_GetStringWithArguments_ReturnsLocalizedString()
        {
            // Arrange
            var stringLocalizer = new TestStringLocalizer();

            var htmlLocalizer = new HtmlLocalizer(stringLocalizer);

            // Act
            var actualLocalizedString = htmlLocalizer.GetString("John", "Doe");

            // Assert
            Assert.Equal("Hello John Doe", actualLocalizedString.Value);
        }
Exemple #7
0
        public void HtmlLocalizer_Html_ReturnsLocalizedHtmlString()
        {
            // Arrange
            var stringLocalizer = new TestStringLocalizer();

            var htmlLocalizer = new HtmlLocalizer(stringLocalizer);

            // Act
            var actualLocalizedHtmlString = htmlLocalizer.GetHtml("John");

            // Assert
            Assert.Equal("Hello John", actualLocalizedHtmlString.Value);
        }
Exemple #8
0
        public void HtmlLocalizer_WithCulture_ReturnsLocalizedHtmlString()
        {
            // Arrange
            var stringLocalizer = new TestStringLocalizer(new CultureInfo("fr"));

            var htmlLocalizer = new HtmlLocalizer(stringLocalizer);

            // Act
            var actualLocalizedHtmlString = htmlLocalizer["John"];

            // Assert
            Assert.Equal("Bonjour John", actualLocalizedHtmlString.Value);
        }
Exemple #9
0
        public void HtmlLocalizer_GetAllStrings_ReturnsAllLocalizedStrings()
        {
            // Arrange
            var stringLocalizer = new TestStringLocalizer();

            var htmlLocalizer = new HtmlLocalizer(stringLocalizer);

            // Act
            var allLocalizedStrings = htmlLocalizer.GetAllStrings(includeParentCultures: false).ToList();

            //Assert
            Assert.Single(allLocalizedStrings);
            Assert.Equal("World", allLocalizedStrings.First().Value);
        }
        public void HtmlLocalizer_WithCulture_ReturnsLocalizedHtmlString()
        {
            // Arrange
            var stringLocalizer = new TestStringLocalizer();

            var htmlLocalizer = new HtmlLocalizer(stringLocalizer);

            // Act
#pragma warning disable CS0618 // Type or member is obsolete
            var actualLocalizedHtmlString = htmlLocalizer.WithCulture(new CultureInfo("fr"))["John"];
#pragma warning restore CS0618 // Type or member is obsolete

            // Assert
            Assert.Equal("Bonjour John", actualLocalizedHtmlString.Value);
        }
Exemple #11
0
        public void HtmlLocalizer_GetAllStringsIncludeParentCulture_ReturnsAllLocalizedStrings()
        {
            // Arrange
            var stringLocalizer = new TestStringLocalizer();

            var htmlLocalizer = new HtmlLocalizer(stringLocalizer);

            // Act
            var allLocalizedStrings = htmlLocalizer.GetAllStrings().ToList();

            //Assert
            Assert.Equal(2, allLocalizedStrings.Count);
            Assert.Equal("World", allLocalizedStrings[0].Value);
            Assert.Equal("Bar", allLocalizedStrings[1].Value);
        }
Exemple #12
0
        public void HtmlLocalizer_UseIndexer_ReturnsLocalizedString()
        {
            // Arrange
            var localizedString = new LocalizedString("Hello", "Bonjour");
            var stringLocalizer = new Mock<IStringLocalizer>();
            stringLocalizer.Setup(s => s["Hello"]).Returns(localizedString);

            var htmlLocalizer = new HtmlLocalizer(stringLocalizer.Object, new CommonTestEncoder());

            // Act
            var actualLocalizedString = htmlLocalizer["Hello"];

            // Assert
            Assert.Equal(localizedString, actualLocalizedString);
        }
        public void HtmlLocalizer_UseIndexer_ReturnsLocalizedHtmlString()
        {
            // Arrange
            var localizedString = new LocalizedString("Hello", "Bonjour");
            var stringLocalizer = new Mock<IStringLocalizer>();
            stringLocalizer.Setup(s => s["Hello"]).Returns(localizedString);

            var htmlLocalizer = new HtmlLocalizer(stringLocalizer.Object);

            // Act
            var actualLocalizedHtmlString = htmlLocalizer["Hello"];

            // Assert
            Assert.Equal(localizedString.Name, actualLocalizedHtmlString.Name);
            Assert.Equal(localizedString.Value, actualLocalizedHtmlString.Value);
        }
Exemple #14
0
        public void HtmlLocalizer_UseIndexer_ReturnsLocalizedString()
        {
            // Arrange
            var localizedString = new LocalizedString("Hello", "Bonjour");
            var stringLocalizer = new Mock <IStringLocalizer>();

            stringLocalizer.Setup(s => s["Hello"]).Returns(localizedString);

            var htmlLocalizer = new HtmlLocalizer(stringLocalizer.Object, new CommonTestEncoder());

            // Act
            var actualLocalizedString = htmlLocalizer["Hello"];

            // Assert
            Assert.Equal(localizedString, actualLocalizedString);
        }
Exemple #15
0
        public void HtmlLocalizer_UseIndexer_ReturnsLocalizedHtmlString()
        {
            // Arrange
            var localizedString = new LocalizedString("Hello", "Bonjour");
            var stringLocalizer = new Mock <IStringLocalizer>();

            stringLocalizer.Setup(s => s["Hello"]).Returns(localizedString);

            var htmlLocalizer = new HtmlLocalizer(stringLocalizer.Object);

            // Act
            var actualLocalizedHtmlString = htmlLocalizer["Hello"];

            // Assert
            Assert.Equal(localizedString.Name, actualLocalizedHtmlString.Name);
            Assert.Equal(localizedString.Value, actualLocalizedHtmlString.Value);
        }
Exemple #16
0
        public void HtmlLocalizer_HtmlWithInvalidResourcestring_ThrowsException(string format)
        {
            // Arrange
            var localizedString = new LocalizedString("Hello", format);

            var stringLocalizer = new Mock <IStringLocalizer>();

            stringLocalizer.Setup(s => s["Hello"]).Returns(localizedString);

            var htmlLocalizer = new HtmlLocalizer(stringLocalizer.Object, new CommonTestEncoder());

            // Act
            var exception = Assert.Throws <FormatException>(() => htmlLocalizer.Html("Hello", new object[] { }));

            // Assert
            Assert.NotNull(exception);
            Assert.Equal("Input string was not in a correct format.", exception.Message);
        }
        public void HtmlLocalizerOfTTest_UseIndexer_ReturnsLocalizedString()
        {
            // Arrange
            var localizedString = new LocalizedString("Hello", "Bonjour");

            var htmlLocalizer = new Mock<IHtmlLocalizer>();
            htmlLocalizer.Setup(h => h["Hello"]).Returns(localizedString);

            var htmlLocalizerFactory = new Mock<IHtmlLocalizerFactory>();
            htmlLocalizerFactory.Setup(h => h.Create(typeof(TestClass)))
                .Returns(htmlLocalizer.Object);

            var htmlLocalizerOfT = new HtmlLocalizer<TestClass>(htmlLocalizerFactory.Object);

            // Act
            var actualLocalizedString = htmlLocalizerOfT["Hello"];

            // Assert
            Assert.Equal(localizedString, actualLocalizedString);
        }
Exemple #18
0
        public void HtmlLocalizerOfTTest_UseIndexer_ReturnsLocalizedString()
        {
            // Arrange
            var localizedString = new LocalizedString("Hello", "Bonjour");

            var htmlLocalizer = new Mock <IHtmlLocalizer>();

            htmlLocalizer.Setup(h => h["Hello"]).Returns(localizedString);

            var htmlLocalizerFactory = new Mock <IHtmlLocalizerFactory>();

            htmlLocalizerFactory.Setup(h => h.Create(typeof(TestClass)))
            .Returns(htmlLocalizer.Object);

            var htmlLocalizerOfT = new HtmlLocalizer <TestClass>(htmlLocalizerFactory.Object);

            // Act
            var actualLocalizedString = htmlLocalizerOfT["Hello"];

            // Assert
            Assert.Equal(localizedString, actualLocalizedString);
        }
        public void HtmlLocalizerOfTTest_UseIndexerWithArguments_ReturnsLocalizedString()
        {
            // Arrange
            var applicationEnvironment = new Mock<IApplicationEnvironment>();
            applicationEnvironment.Setup(a => a.ApplicationName).Returns("TestApplication");

            var localizedString = new LocalizedString("Hello", "Bonjour test");

            var htmlLocalizer = new Mock<IHtmlLocalizer>();
            htmlLocalizer.Setup(h => h["Hello", "test"]).Returns(localizedString);

            var htmlLocalizerFactory = new Mock<IHtmlLocalizerFactory>();
            htmlLocalizerFactory.Setup(h => h.Create(typeof(TestClass)))
                .Returns(htmlLocalizer.Object);

            var htmlLocalizerOfT = new HtmlLocalizer<TestClass>(htmlLocalizerFactory.Object);

            // Act
            var actualLocalizedString = htmlLocalizerOfT["Hello", "test"];

            // Assert
            Assert.Equal(localizedString, actualLocalizedString);
        }
        public void ViewLocalizer_HtmlWithArguments_ReturnsLocalizedHtmlString()
        {
            // Arrange
            var stringLocalizer = new TestStringLocalizer();
            var htmlLocalizer = new HtmlLocalizer(stringLocalizer);
            var applicationEnvironment = new Mock<IApplicationEnvironment>();
            applicationEnvironment.Setup(a => a.ApplicationName).Returns("TestApplication");
            var viewLocalizer = new ViewLocalizer(new TestHtmlLocalizerFactory(), applicationEnvironment.Object);

            var view = new Mock<IView>();
            view.Setup(v => v.Path).Returns("example");
            var viewContext = new ViewContext();
            viewContext.View = view.Object;

            viewLocalizer.Contextualize(viewContext);

            // Act
            var actualLocalizedString = viewLocalizer.GetHtml("John", "Doe");

            // Assert
            Assert.Equal("Hello John Doe", actualLocalizedString.Value);
        }
Exemple #21
0
        public void HtmlLocalizer_HtmlWithArguments_ReturnsLocalizedHtml(
            string format,
            object[] arguments,
            string expectedText)
        {
            // Arrange
            var localizedString = new LocalizedString("Hello", format);

            var stringLocalizer = new Mock<IStringLocalizer>();
            stringLocalizer.Setup(s => s["Hello"]).Returns(localizedString);

            var htmlLocalizer = new HtmlLocalizer(stringLocalizer.Object, new CommonTestEncoder());

            // Act
            var localizedHtmlString = htmlLocalizer.Html("Hello", arguments);

            // Assert
            Assert.NotNull(localizedHtmlString);
            Assert.Equal(expectedText, localizedHtmlString.Value);
        }
Exemple #22
0
        public void HtmlLocalizer_HtmlWithInvalidResourcestring_ThrowsException(string format)
        {
            // Arrange
            var localizedString = new LocalizedString("Hello", format);

            var stringLocalizer = new Mock<IStringLocalizer>();
            stringLocalizer.Setup(s => s["Hello"]).Returns(localizedString);

            var htmlLocalizer = new HtmlLocalizer(stringLocalizer.Object, new CommonTestEncoder());

            // Act
            var exception = Assert.Throws<FormatException>(() => htmlLocalizer.Html("Hello", new object[] { }));

            // Assert
            Assert.NotNull(exception);
            Assert.Equal("Input string was not in a correct format.", exception.Message);
        }
 /// <summary>
 /// Gets localized string for given key name and specified culture information.
 /// </summary>
 protected string L(string name, CultureInfo culture) => HtmlLocalizer.WithCulture(culture).GetString(name);
        public void ViewLocalizer_GetAllStringsIncludeParentCulture_ReturnsLocalizedString()
        {
            // Arrange
            var stringLocalizer = new TestStringLocalizer();
            var htmlLocalizer = new HtmlLocalizer(stringLocalizer);
            var applicationEnvironment = new Mock<IApplicationEnvironment>();
            applicationEnvironment.Setup(a => a.ApplicationName).Returns("TestApplication");
            var viewLocalizer = new ViewLocalizer(new TestHtmlLocalizerFactory(), applicationEnvironment.Object);

            var view = new Mock<IView>();
            view.Setup(v => v.Path).Returns("example");
            var viewContext = new ViewContext();
            viewContext.View = view.Object;

            viewLocalizer.Contextualize(viewContext);

            // Act
            var allLocalizedStrings = viewLocalizer.GetAllStrings(includeParentCultures: true).ToList();

            // Assert
            Assert.Equal(2, allLocalizedStrings.Count);
            Assert.Equal("World", allLocalizedStrings[0].Value);
            Assert.Equal("Bar", allLocalizedStrings[1].Value);
        }
 /// <summary>
 /// Gets localized string for given key name and current language with formatting strings.
 /// </summary>
 protected string L(string name, CultureInfo culture, params object[] args) =>
 HtmlLocalizer.WithCulture(culture).GetString(name, args);
 /// <summary>
 /// Gets localized string for given key name and current language.
 /// </summary>
 protected string L(string name) => HtmlLocalizer.GetString(name);
        public void ViewLocalizer_WithCulture_ReturnsLocalizedHtmlString()
        {
            // Arrange
            var stringLocalizer = new TestStringLocalizer();
            var htmlLocalizer = new HtmlLocalizer(stringLocalizer);
            var applicationEnvironment = new Mock<IApplicationEnvironment>();
            applicationEnvironment.Setup(a => a.ApplicationName).Returns("TestApplication");
            var viewLocalizer = new ViewLocalizer(new TestHtmlLocalizerFactory(), applicationEnvironment.Object);

            var view = new Mock<IView>();
            view.Setup(v => v.Path).Returns("example");
            var viewContext = new ViewContext();
            viewContext.View = view.Object;

            viewLocalizer.Contextualize(viewContext);

            // Act
            var actualLocalizedString = viewLocalizer.WithCulture(new CultureInfo("fr"))["John"];

            // Assert
            Assert.Equal("Bonjour John", actualLocalizedString.Value);
        }
 /// <summary>
 /// Gets localized string for given key name and current language with formatting strings.
 /// </summary>
 protected string L(string name, params object[] args) => HtmlLocalizer.GetString(name, args);
Exemple #29
0
        public void HtmlLocalizer_GetStringWithArguments_ReturnsLocalizedString()
        {
            // Arrange
            var stringLocalizer = new TestStringLocalizer();

            var htmlLocalizer = new HtmlLocalizer(stringLocalizer);

            // Act
            var actualLocalizedString = htmlLocalizer.GetString("John", "Doe");

            // Assert
            Assert.Equal("Hello John Doe", actualLocalizedString.Value);
        }
Exemple #30
0
        public void HtmlLocalizer_Html_ReturnsLocalizedHtmlString()
        {
            // Arrange
            var stringLocalizer = new TestStringLocalizer();

            var htmlLocalizer = new HtmlLocalizer(stringLocalizer);

            // Act
            var actualLocalizedHtmlString = htmlLocalizer.GetHtml("John");

            // Assert
            Assert.Equal("Hello John", actualLocalizedHtmlString.Value);
        }
Exemple #31
0
        public void HtmlLocalizer_WithCulture_ReturnsLocalizedHtmlString()
        {
            // Arrange
            var stringLocalizer = new TestStringLocalizer();

            var htmlLocalizer = new HtmlLocalizer(stringLocalizer);

            // Act
            var actualLocalizedHtmlString = htmlLocalizer.WithCulture(new CultureInfo("fr"))["John"];

            // Assert
            Assert.Equal("Bonjour John", actualLocalizedHtmlString.Value);
        }
Exemple #32
0
        public void HtmlLocalizer_GetAllStringsIncludeParentCulture_ReturnsAllLocalizedStrings()
        {
            // Arrange
            var stringLocalizer = new TestStringLocalizer();

            var htmlLocalizer = new HtmlLocalizer(stringLocalizer);

            // Act
            var allLocalizedStrings = htmlLocalizer.GetAllStrings().ToList();

            //Assert
            Assert.Equal(2, allLocalizedStrings.Count);
            Assert.Equal("World", allLocalizedStrings[0].Value);
            Assert.Equal("Bar", allLocalizedStrings[1].Value);
        }