diff --git a/samples/LocalizationSample.Web/Resources/LocalizationSample.Web.Controllers.HomeController.fr.resx b/samples/LocalizationSample.Web/Resources/Controllers.HomeController.fr.resx similarity index 100% rename from samples/LocalizationSample.Web/Resources/LocalizationSample.Web.Controllers.HomeController.fr.resx rename to samples/LocalizationSample.Web/Resources/Controllers.HomeController.fr.resx diff --git a/samples/LocalizationSample.Web/Resources/LocalizationSample.Web.Models.Product.fr.resx b/samples/LocalizationSample.Web/Resources/Models.Product.fr.resx similarity index 100% rename from samples/LocalizationSample.Web/Resources/LocalizationSample.Web.Models.Product.fr.resx rename to samples/LocalizationSample.Web/Resources/Models.Product.fr.resx diff --git a/samples/LocalizationSample.Web/Resources/LocalizationSample.Web.Models.User.fr.resx b/samples/LocalizationSample.Web/Resources/Models.User.fr.resx similarity index 100% rename from samples/LocalizationSample.Web/Resources/LocalizationSample.Web.Models.User.fr.resx rename to samples/LocalizationSample.Web/Resources/Models.User.fr.resx diff --git a/samples/LocalizationSample.Web/Views/Home/Locpage.cshtml b/samples/LocalizationSample.Web/Views/Home/Locpage.cshtml index de2a3a134d..90dddc4579 100644 --- a/samples/LocalizationSample.Web/Views/Home/Locpage.cshtml +++ b/samples/LocalizationSample.Web/Views/Home/Locpage.cshtml @@ -4,4 +4,4 @@ } @LocString["Hello there!!"] @ViewBag.Message -@LocString.Html("Hi", "John", @date , @date.DayOfWeek) +@LocString.GetHtml("Hi", "John", @date , @date.DayOfWeek) diff --git a/src/Microsoft.AspNet.Mvc.Localization/HtmlLocalizer.cs b/src/Microsoft.AspNet.Mvc.Localization/HtmlLocalizer.cs index ceaa04de02..1d0c70d4a3 100644 --- a/src/Microsoft.AspNet.Mvc.Localization/HtmlLocalizer.cs +++ b/src/Microsoft.AspNet.Mvc.Localization/HtmlLocalizer.cs @@ -9,12 +9,12 @@ using Microsoft.Extensions.Localization; namespace Microsoft.AspNet.Mvc.Localization { /// - /// An that uses the to provide localized HTML content. - /// This service just encodes the arguments but not the resource string. + /// An that uses the provided to do HTML-aware + /// localization of content. /// public class HtmlLocalizer : IHtmlLocalizer { - private IStringLocalizer _localizer; + private readonly IStringLocalizer _localizer; /// /// Creates a new . @@ -31,38 +31,60 @@ namespace Microsoft.AspNet.Mvc.Localization } /// - public virtual LocalizedString this[string key] + public virtual LocalizedHtmlString this[string name] { get { - if (key == null) + if (name == null) { - throw new ArgumentNullException(nameof(key)); + throw new ArgumentNullException(nameof(name)); } - return _localizer[key]; + return ToHtmlString(_localizer[name]); } } /// - public virtual LocalizedString this[string key, params object[] arguments] + public virtual LocalizedHtmlString this[string name, params object[] arguments] { get { - if (key == null) + if (name == null) { - throw new ArgumentNullException(nameof(key)); + throw new ArgumentNullException(nameof(name)); } - return _localizer[key, arguments]; + return ToHtmlString(_localizer[name], arguments); } } - /// - /// Creates a new for a specific . - /// - /// The to use. - /// A culture-specific . + /// + public virtual LocalizedString GetString(string name) + { + if (name == null) + { + throw new ArgumentNullException(nameof(name)); + } + + return _localizer[name]; + } + + /// + public virtual LocalizedString GetString(string name, params object[] arguments) + { + if (name == null) + { + throw new ArgumentNullException(nameof(name)); + } + + return _localizer[name, arguments]; + } + + /// + public virtual IEnumerable GetAllStrings(bool includeParentCultures) => + _localizer.GetAllStrings(includeParentCultures); + + /// public virtual IHtmlLocalizer WithCulture(CultureInfo culture) { if (culture == null) @@ -73,69 +95,6 @@ namespace Microsoft.AspNet.Mvc.Localization return new HtmlLocalizer(_localizer.WithCulture(culture)); } - /// - /// Creates a new for a specific . - /// - /// The to use. - /// A culture-specific . - IStringLocalizer IStringLocalizer.WithCulture(CultureInfo culture) - { - if (culture == null) - { - throw new ArgumentNullException(nameof(culture)); - } - - return new HtmlLocalizer(_localizer.WithCulture(culture)); - } - - /// - public virtual LocalizedString GetString(string key) - { - if (key == null) - { - throw new ArgumentNullException(nameof(key)); - } - - return _localizer.GetString(key); - } - - /// - public virtual LocalizedString GetString(string key, params object[] arguments) - { - if (key == null) - { - throw new ArgumentNullException(nameof(key)); - } - - return _localizer.GetString(key, arguments); - } - - /// - public virtual IEnumerable GetAllStrings(bool includeAncestorCultures) => - _localizer.GetAllStrings(includeAncestorCultures); - - /// - public virtual LocalizedHtmlString Html(string key) - { - if (key == null) - { - throw new ArgumentNullException(nameof(key)); - } - - return ToHtmlString(_localizer.GetString(key)); - } - - /// - public virtual LocalizedHtmlString Html(string key, params object[] arguments) - { - if (key == null) - { - throw new ArgumentNullException(nameof(key)); - } - - return ToHtmlString(_localizer.GetString(key), arguments); - } - /// /// Creates a new for a . /// diff --git a/src/Microsoft.AspNet.Mvc.Localization/HtmlLocalizerExtensions.cs b/src/Microsoft.AspNet.Mvc.Localization/HtmlLocalizerExtensions.cs new file mode 100644 index 0000000000..21e96a6777 --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Localization/HtmlLocalizerExtensions.cs @@ -0,0 +1,71 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using Microsoft.Extensions.Localization; + +namespace Microsoft.AspNet.Mvc.Localization +{ + /// + /// Extension methods for . + /// + public static class HtmlLocalizerExtensions + { + /// + /// Gets the resource for a specific name. + /// + /// The key to use. + /// The resource. + public static LocalizedHtmlString GetHtml(this IHtmlLocalizer htmlLocalizer, string name) + { + if (htmlLocalizer == null) + { + throw new ArgumentNullException(nameof(htmlLocalizer)); + } + + if (name == null) + { + throw new ArgumentNullException(nameof(name)); + } + + return htmlLocalizer[name]; + } + + /// + /// Gets the resource for a specific name. + /// + /// The key to use. + /// The values to format the string with. + /// The resource. + public static LocalizedHtmlString GetHtml(this IHtmlLocalizer htmlLocalizer, string name, params object[] arguments) + { + if (htmlLocalizer == null) + { + throw new ArgumentNullException(nameof(htmlLocalizer)); + } + + if (name == null) + { + throw new ArgumentNullException(nameof(name)); + } + + return htmlLocalizer[name, arguments]; + } + + /// + /// Gets all string resources including those for parent cultures. + /// + /// The . + /// The string resources. + public static IEnumerable GetAllStrings(this IHtmlLocalizer htmlLocalizer) + { + if (htmlLocalizer == null) + { + throw new ArgumentNullException(nameof(htmlLocalizer)); + } + + return htmlLocalizer.GetAllStrings(includeParentCultures: true); + } + } +} diff --git a/src/Microsoft.AspNet.Mvc.Localization/HtmlLocalizerFactory.cs b/src/Microsoft.AspNet.Mvc.Localization/HtmlLocalizerFactory.cs index 8ad004bb67..0471fe5145 100644 --- a/src/Microsoft.AspNet.Mvc.Localization/HtmlLocalizerFactory.cs +++ b/src/Microsoft.AspNet.Mvc.Localization/HtmlLocalizerFactory.cs @@ -7,14 +7,15 @@ using Microsoft.Extensions.Localization; namespace Microsoft.AspNet.Mvc.Localization { /// - /// An that creates instances of . + /// An that creates instances of using the + /// registered . /// public class HtmlLocalizerFactory : IHtmlLocalizerFactory { private readonly IStringLocalizerFactory _factory; /// - /// Creates a new . + /// Creates a new . /// /// The . public HtmlLocalizerFactory(IStringLocalizerFactory localizerFactory) @@ -28,10 +29,9 @@ namespace Microsoft.AspNet.Mvc.Localization } /// - /// Creates an using the and - /// of the specified . + /// Creates an using the specified . /// - /// The . + /// The to load resources for. /// The . public virtual IHtmlLocalizer Create(Type resourceSource) { @@ -44,7 +44,7 @@ namespace Microsoft.AspNet.Mvc.Localization } /// - /// Creates an . + /// Creates an using the specified base name and location. /// /// The base name of the resource to load strings from. /// The location to load resources from. diff --git a/src/Microsoft.AspNet.Mvc.Localization/HtmlLocalizerOfT.cs b/src/Microsoft.AspNet.Mvc.Localization/HtmlLocalizerOfT.cs index 4247dd8121..ccc6f0eb98 100644 --- a/src/Microsoft.AspNet.Mvc.Localization/HtmlLocalizerOfT.cs +++ b/src/Microsoft.AspNet.Mvc.Localization/HtmlLocalizerOfT.cs @@ -9,7 +9,8 @@ using Microsoft.Extensions.Localization; namespace Microsoft.AspNet.Mvc.Localization { /// - /// This is an implementation that provides localized HTML content. + /// An implementation that provides localized HTML content for the specified type + /// . /// /// The to scope the resource names. public class HtmlLocalizer : IHtmlLocalizer @@ -17,7 +18,7 @@ namespace Microsoft.AspNet.Mvc.Localization private readonly IHtmlLocalizer _localizer; /// - /// Creates a new . + /// Creates a new . /// /// The . public HtmlLocalizer(IHtmlLocalizerFactory factory) @@ -26,33 +27,59 @@ namespace Microsoft.AspNet.Mvc.Localization } /// - public virtual LocalizedString this[string key] + public virtual LocalizedHtmlString this[string name] { get { - if (key == null) + if (name == null) { - throw new ArgumentNullException(nameof(key)); + throw new ArgumentNullException(nameof(name)); } - return _localizer[key]; + return _localizer[name]; } } /// - public virtual LocalizedString this[string key, params object[] arguments] + public virtual LocalizedHtmlString this[string name, params object[] arguments] { get { - if (key == null) + if (name == null) { - throw new ArgumentNullException(nameof(key)); + throw new ArgumentNullException(nameof(name)); } - return _localizer[key, arguments]; + return _localizer[name, arguments]; } } + /// + public virtual LocalizedString GetString(string name) + { + if (name == null) + { + throw new ArgumentNullException(nameof(name)); + } + + return _localizer.GetString(name); + } + + /// + public virtual LocalizedString GetString(string name, params object[] arguments) + { + if (name == null) + { + throw new ArgumentNullException(nameof(name)); + } + + return _localizer.GetString(name, arguments); + } + + /// + public virtual IEnumerable GetAllStrings(bool includeParentCultures) => + _localizer.GetAllStrings(includeParentCultures); + /// public virtual IHtmlLocalizer WithCulture(CultureInfo culture) { @@ -63,64 +90,5 @@ namespace Microsoft.AspNet.Mvc.Localization return _localizer.WithCulture(culture); } - - /// - IStringLocalizer IStringLocalizer.WithCulture(CultureInfo culture) - { - if (culture == null) - { - throw new ArgumentNullException(nameof(culture)); - } - - return _localizer.WithCulture(culture); - } - - /// - public virtual LocalizedString GetString(string key) - { - if (key == null) - { - throw new ArgumentNullException(nameof(key)); - } - - return _localizer.GetString(key); - } - - /// - public virtual LocalizedString GetString(string key, params object[] arguments) - { - if (key == null) - { - throw new ArgumentNullException(nameof(key)); - } - - return _localizer.GetString(key, arguments); - } - - /// - public virtual LocalizedHtmlString Html(string key) - { - if (key == null) - { - throw new ArgumentNullException(nameof(key)); - } - - return _localizer.Html(key); - } - - /// - public virtual LocalizedHtmlString Html(string key, params object[] arguments) - { - if (key == null) - { - throw new ArgumentNullException(nameof(key)); - } - - return _localizer.Html(key, arguments); - } - - /// - public virtual IEnumerable GetAllStrings(bool includeAncestorCultures) => - _localizer.GetAllStrings(includeAncestorCultures); } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Localization/IHtmlLocalizer.cs b/src/Microsoft.AspNet.Mvc.Localization/IHtmlLocalizer.cs index b2c7436e49..45d9a7a0ca 100644 --- a/src/Microsoft.AspNet.Mvc.Localization/IHtmlLocalizer.cs +++ b/src/Microsoft.AspNet.Mvc.Localization/IHtmlLocalizer.cs @@ -1,37 +1,65 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System.Collections.Generic; using System.Globalization; using Microsoft.Extensions.Localization; namespace Microsoft.AspNet.Mvc.Localization { /// - /// This service does not HTML encode the resource string. It HTML encodes all arguments that are formatted in - /// the resource string. + /// Represents a type that that does HTML-aware localization of strings, by HTML encoding arguments that are + /// formatted in the resource string. /// - public interface IHtmlLocalizer : IStringLocalizer + public interface IHtmlLocalizer { /// - /// Creates a new for a specific . + /// Gets the string resource with the given name. + /// + /// The name of the string resource. + /// The string resource as a . + LocalizedHtmlString this[string name] { get; } + + /// + /// Gets the string resource with the given name and formatted with the supplied arguments. The arguments will + /// be HTML encoded. + /// + /// The name of the string resource. + /// The values to format the string with. + /// The formatted string resource as a . + LocalizedHtmlString this[string name, params object[] arguments] { get; } + + /// + /// Gets the string resource with the given name. + /// + /// The . + /// The name of the string resource. + /// The string resource as a . + LocalizedString GetString(string name); + + /// + /// Gets the string resource with the given name and formatted with the supplied arguments. + /// + /// The . + /// The name of the string resource. + /// The values to format the string with. + /// The formatted string resource as a . + LocalizedString GetString(string name, params object[] arguments); + + /// + /// Gets all string resources. + /// + /// + /// A indicating whether to include strings from parent cultures. + /// + /// The strings. + IEnumerable GetAllStrings(bool includeParentCultures); + + /// + /// Creates a new for a specific . /// /// The to use. /// A culture-specific . - new IHtmlLocalizer WithCulture(CultureInfo culture); - - /// - /// Gets the resource for a specific key. - /// - /// The key to use. - /// The resource. - LocalizedHtmlString Html(string key); - - /// - /// Gets the resource for a specific key. - /// - /// The key to use. - /// The values to format the string with. - /// The resource. - LocalizedHtmlString Html(string key, params object[] arguments); + IHtmlLocalizer WithCulture(CultureInfo culture); } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Localization/LocalizedHtmlString.cs b/src/Microsoft.AspNet.Mvc.Localization/LocalizedHtmlString.cs index f2be394544..5a9899170e 100644 --- a/src/Microsoft.AspNet.Mvc.Localization/LocalizedHtmlString.cs +++ b/src/Microsoft.AspNet.Mvc.Localization/LocalizedHtmlString.cs @@ -24,26 +24,36 @@ namespace Microsoft.AspNet.Mvc.Localization /// /// Creates an instance of . /// - /// The name of the string resource. + /// The name of the string resource. /// The string resource. - /// A flag that indicates if the resource is not found. - public LocalizedHtmlString(string key, string value, bool isResourceNotFound) - : this(key, value, isResourceNotFound, arguments: EmptyArguments) + public LocalizedHtmlString(string name, string value) + : this(name, value, isResourceNotFound: false, arguments: EmptyArguments) { } /// /// Creates an instance of . /// - /// The name of the string resource. + /// The name of the string resource. + /// The string resource. + /// A flag that indicates if the resource is not found. + public LocalizedHtmlString(string name, string value, bool isResourceNotFound) + : this(name, value, isResourceNotFound, arguments: EmptyArguments) + { + } + + /// + /// Creates an instance of . + /// + /// The name of the string resource. /// The string resource. /// A flag that indicates if the resource is not found. /// The values to format the with. - public LocalizedHtmlString(string key, string value, bool isResourceNotFound, params object[] arguments) + public LocalizedHtmlString(string name, string value, bool isResourceNotFound, params object[] arguments) { - if (key == null) + if (name == null) { - throw new ArgumentNullException(nameof(key)); + throw new ArgumentNullException(nameof(name)); } if (value == null) @@ -56,7 +66,7 @@ namespace Microsoft.AspNet.Mvc.Localization throw new ArgumentNullException(nameof(arguments)); } - Key = key; + Name = name; Value = value; IsResourceNotFound = isResourceNotFound; _arguments = arguments; @@ -65,7 +75,7 @@ namespace Microsoft.AspNet.Mvc.Localization /// /// The name of the string resource. /// - public string Key { get; } + public string Name { get; } /// /// The string resource. diff --git a/src/Microsoft.AspNet.Mvc.Localization/ViewLocalizer.cs b/src/Microsoft.AspNet.Mvc.Localization/ViewLocalizer.cs index a9dc3b8eda..c45b038d38 100644 --- a/src/Microsoft.AspNet.Mvc.Localization/ViewLocalizer.cs +++ b/src/Microsoft.AspNet.Mvc.Localization/ViewLocalizer.cs @@ -42,7 +42,7 @@ namespace Microsoft.AspNet.Mvc.Localization } /// - public virtual LocalizedString this[string key] + public virtual LocalizedHtmlString this[string key] { get { @@ -56,7 +56,7 @@ namespace Microsoft.AspNet.Mvc.Localization } /// - public virtual LocalizedString this[string key, params object[] arguments] + public virtual LocalizedHtmlString this[string key, params object[] arguments] { get { @@ -76,17 +76,16 @@ namespace Microsoft.AspNet.Mvc.Localization public LocalizedString GetString(string name, params object[] values) => _localizer.GetString(name, values); /// - public LocalizedHtmlString Html(string key) => _localizer.Html(key); + public IHtmlLocalizer WithCulture(CultureInfo culture) => _localizer.WithCulture(culture); /// - public LocalizedHtmlString Html(string key, params object[] arguments) => _localizer.Html(key, arguments); - - /// - public IStringLocalizer WithCulture(CultureInfo culture) => _localizer.WithCulture(culture); - - /// - IHtmlLocalizer IHtmlLocalizer.WithCulture(CultureInfo culture) => _localizer.WithCulture(culture); + public IEnumerable GetAllStrings(bool includeParentCultures) => + _localizer.GetAllStrings(includeParentCultures); + /// + /// Apply the specified . + /// + /// The . public void Contextualize(ViewContext viewContext) { if (viewContext == null) @@ -95,16 +94,12 @@ namespace Microsoft.AspNet.Mvc.Localization } var baseName = viewContext.View.Path.Replace('/', '.').Replace('\\', '.'); - if (baseName.StartsWith(".")) + if (baseName.StartsWith(".", StringComparison.OrdinalIgnoreCase)) { baseName = baseName.Substring(1); } _localizer = _localizerFactory.Create(baseName, _applicationName); } - - /// - public IEnumerable GetAllStrings(bool includeAncestorCultures) => - _localizer.GetAllStrings(includeAncestorCultures); } } \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Localization.Test/HtmlLocalizerOfTTest.cs b/test/Microsoft.AspNet.Mvc.Localization.Test/HtmlLocalizerOfTTest.cs index fe2d996019..357d85e7ed 100644 --- a/test/Microsoft.AspNet.Mvc.Localization.Test/HtmlLocalizerOfTTest.cs +++ b/test/Microsoft.AspNet.Mvc.Localization.Test/HtmlLocalizerOfTTest.cs @@ -14,10 +14,10 @@ namespace Microsoft.AspNet.Mvc.Localization.Test public class HtmlLocalizerOfTTest { [Fact] - public void HtmlLocalizerOfTTest_UseIndexer_ReturnsLocalizedString() + public void HtmlLocalizerOfTTest_UseIndexer_ReturnsLocalizedHtmlString() { // Arrange - var localizedString = new LocalizedString("Hello", "Bonjour"); + var localizedString = new LocalizedHtmlString("Hello", "Bonjour"); var htmlLocalizer = new Mock(); htmlLocalizer.Setup(h => h["Hello"]).Returns(localizedString); @@ -36,13 +36,13 @@ namespace Microsoft.AspNet.Mvc.Localization.Test } [Fact] - public void HtmlLocalizerOfTTest_UseIndexerWithArguments_ReturnsLocalizedString() + public void HtmlLocalizerOfTTest_UseIndexerWithArguments_ReturnsLocalizedHtmlString() { // Arrange var applicationEnvironment = new Mock(); applicationEnvironment.Setup(a => a.ApplicationName).Returns("TestApplication"); - var localizedString = new LocalizedString("Hello", "Bonjour test"); + var localizedString = new LocalizedHtmlString("Hello", "Bonjour test"); var htmlLocalizer = new Mock(); htmlLocalizer.Setup(h => h["Hello", "test"]).Returns(localizedString); diff --git a/test/Microsoft.AspNet.Mvc.Localization.Test/HtmlLocalizerTest.cs b/test/Microsoft.AspNet.Mvc.Localization.Test/HtmlLocalizerTest.cs index 2ff1bc919f..be2a8c53fe 100644 --- a/test/Microsoft.AspNet.Mvc.Localization.Test/HtmlLocalizerTest.cs +++ b/test/Microsoft.AspNet.Mvc.Localization.Test/HtmlLocalizerTest.cs @@ -17,7 +17,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test public class HtmlLocalizerTest { [Fact] - public void HtmlLocalizer_UseIndexer_ReturnsLocalizedString() + public void HtmlLocalizer_UseIndexer_ReturnsLocalizedHtmlString() { // Arrange var localizedString = new LocalizedString("Hello", "Bonjour"); @@ -27,28 +27,30 @@ namespace Microsoft.AspNet.Mvc.Localization.Test var htmlLocalizer = new HtmlLocalizer(stringLocalizer.Object); // Act - var actualLocalizedString = htmlLocalizer["Hello"]; + var actualLocalizedHtmlString = htmlLocalizer["Hello"]; // Assert - Assert.Equal(localizedString, actualLocalizedString); + Assert.Equal(localizedString.Name, actualLocalizedHtmlString.Name); + Assert.Equal(localizedString.Value, actualLocalizedHtmlString.Value); } [Fact] - public void HtmlLocalizer_UseIndexerWithArguments_ReturnsLocalizedString() + public void HtmlLocalizer_UseIndexerWithArguments_ReturnsLocalizedHtmlString() { // Arrange var localizedString = new LocalizedString("Hello", "Bonjour test"); var stringLocalizer = new Mock(); - stringLocalizer.Setup(s => s["Hello", "test"]).Returns(localizedString); + stringLocalizer.Setup(s => s["Hello"]).Returns(localizedString); var htmlLocalizer = new HtmlLocalizer(stringLocalizer.Object); // Act - var actualLocalizedString = htmlLocalizer["Hello", "test"]; + var actualLocalizedHtmlString = htmlLocalizer["Hello", "test"]; // Assert - Assert.Equal(localizedString, actualLocalizedString); + Assert.Equal(localizedString.Name, actualLocalizedHtmlString.Name); + Assert.Equal(localizedString.Value, actualLocalizedHtmlString.Value); } public static IEnumerable HtmlData @@ -108,7 +110,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test var htmlLocalizer = new HtmlLocalizer(stringLocalizer.Object); // Act - var localizedHtmlString = htmlLocalizer.Html("Hello", arguments); + var localizedHtmlString = htmlLocalizer.GetHtml("Hello", arguments); // Assert Assert.NotNull(localizedHtmlString); @@ -150,7 +152,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test stringLocalizer.Setup(s => s["Hello"]).Returns(localizedString); var htmlLocalizer = new HtmlLocalizer(stringLocalizer.Object); - var content = htmlLocalizer.Html("Hello", new object[] { }); + var content = htmlLocalizer.GetHtml("Hello", new object[] { }); // Act var exception = Assert.Throws( @@ -200,10 +202,10 @@ namespace Microsoft.AspNet.Mvc.Localization.Test var htmlLocalizer = new HtmlLocalizer(stringLocalizer); // Act - var actualLocalizedString = htmlLocalizer.Html("John"); + var actualLocalizedHtmlString = htmlLocalizer.GetHtml("John"); // Assert - Assert.Equal("Hello John", actualLocalizedString.Value); + Assert.Equal("Hello John", actualLocalizedHtmlString.Value); } [Fact] @@ -215,10 +217,10 @@ namespace Microsoft.AspNet.Mvc.Localization.Test var htmlLocalizer = new HtmlLocalizer(stringLocalizer); // Act - var actualLocalizedString = htmlLocalizer.WithCulture(new CultureInfo("fr"))["John"]; + var actualLocalizedHtmlString = htmlLocalizer.WithCulture(new CultureInfo("fr"))["John"]; // Assert - Assert.Equal("Bonjour John", actualLocalizedString.Value); + Assert.Equal("Bonjour John", actualLocalizedHtmlString.Value); } [Fact] @@ -230,7 +232,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test var htmlLocalizer = new HtmlLocalizer(stringLocalizer); // Act - var allLocalizedStrings = htmlLocalizer.GetAllStrings(includeAncestorCultures: false).ToList(); + var allLocalizedStrings = htmlLocalizer.GetAllStrings(includeParentCultures: false).ToList(); //Assert Assert.Equal(1, allLocalizedStrings.Count); @@ -238,7 +240,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test } [Fact] - public void HtmlLocalizer_GetAllStringsIncludeAncestorCulture_ReturnsAllLocalizedStrings() + public void HtmlLocalizer_GetAllStringsIncludeParentCulture_ReturnsAllLocalizedStrings() { // Arrange var stringLocalizer = new TestStringLocalizer(); diff --git a/test/Microsoft.AspNet.Mvc.Localization.Test/Internal/MvcLocalizationServiceCollectionExtensionsTest.cs b/test/Microsoft.AspNet.Mvc.Localization.Test/Internal/MvcLocalizationServiceCollectionExtensionsTest.cs index 1eef28a4af..9693ce73f1 100644 --- a/test/Microsoft.AspNet.Mvc.Localization.Test/Internal/MvcLocalizationServiceCollectionExtensionsTest.cs +++ b/test/Microsoft.AspNet.Mvc.Localization.Test/Internal/MvcLocalizationServiceCollectionExtensionsTest.cs @@ -219,7 +219,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Internal public class TestViewLocalizer : IViewLocalizer { - public LocalizedString this[string name] + public LocalizedHtmlString this[string name] { get { @@ -227,7 +227,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Internal } } - public LocalizedString this[string name, params object[] arguments] + public LocalizedHtmlString this[string name, params object[] arguments] { get { @@ -235,17 +235,17 @@ namespace Microsoft.AspNet.Mvc.Localization.Internal } } - public IEnumerable GetAllStrings(bool includeAncestorCultures) + public LocalizedString GetString(string name) { throw new NotImplementedException(); } - public LocalizedHtmlString Html(string key) + public LocalizedString GetString(string name, params object[] arguments) { throw new NotImplementedException(); } - public LocalizedHtmlString Html(string key, params object[] arguments) + public IEnumerable GetAllStrings(bool includeParentCultures) { throw new NotImplementedException(); } @@ -254,16 +254,11 @@ namespace Microsoft.AspNet.Mvc.Localization.Internal { throw new NotImplementedException(); } - - IStringLocalizer IStringLocalizer.WithCulture(CultureInfo culture) - { - throw new NotImplementedException(); - } } public class TestHtmlLocalizer : IHtmlLocalizer { - public LocalizedString this[string name] + public LocalizedHtmlString this[string name] { get { @@ -271,7 +266,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Internal } } - public LocalizedString this[string name, params object[] arguments] + public LocalizedHtmlString this[string name, params object[] arguments] { get { @@ -279,17 +274,17 @@ namespace Microsoft.AspNet.Mvc.Localization.Internal } } - public IEnumerable GetAllStrings(bool includeAncestorCultures) + public LocalizedString GetString(string name) { throw new NotImplementedException(); } - public LocalizedHtmlString Html(string key) + public LocalizedString GetString(string name, params object[] arguments) { throw new NotImplementedException(); } - public LocalizedHtmlString Html(string key, params object[] arguments) + public IEnumerable GetAllStrings(bool includeParentCultures) { throw new NotImplementedException(); } @@ -298,11 +293,6 @@ namespace Microsoft.AspNet.Mvc.Localization.Internal { throw new NotImplementedException(); } - - IStringLocalizer IStringLocalizer.WithCulture(CultureInfo culture) - { - throw new NotImplementedException(); - } } public class TestHtmlLocalizerFactory : IHtmlLocalizerFactory diff --git a/test/Microsoft.AspNet.Mvc.Localization.Test/TestStringLocalizer.cs b/test/Microsoft.AspNet.Mvc.Localization.Test/TestStringLocalizer.cs index 434e9186ee..d0809ba932 100644 --- a/test/Microsoft.AspNet.Mvc.Localization.Test/TestStringLocalizer.cs +++ b/test/Microsoft.AspNet.Mvc.Localization.Test/TestStringLocalizer.cs @@ -54,12 +54,12 @@ namespace Microsoft.AspNet.Mvc.Localization.Test } } - public IEnumerable GetAllStrings(bool includeAncestorCultures) + public IEnumerable GetAllStrings(bool includeParentCultures) { var allStrings = new List(); allStrings.Add(new LocalizedString("Hello", "World")); - if (includeAncestorCultures) + if (includeParentCultures) { allStrings.Add(new LocalizedString("Foo", "Bar")); } diff --git a/test/Microsoft.AspNet.Mvc.Localization.Test/ViewLocalizerTest.cs b/test/Microsoft.AspNet.Mvc.Localization.Test/ViewLocalizerTest.cs index b158df06dd..5b30ad9752 100644 --- a/test/Microsoft.AspNet.Mvc.Localization.Test/ViewLocalizerTest.cs +++ b/test/Microsoft.AspNet.Mvc.Localization.Test/ViewLocalizerTest.cs @@ -17,13 +17,13 @@ namespace Microsoft.AspNet.Mvc.Localization.Test public class ViewLocalizerTest { [Fact] - public void ViewLocalizer_UseIndexer_ReturnsLocalizedString() + public void ViewLocalizer_UseIndexer_ReturnsLocalizedHtmlString() { // Arrange var applicationEnvironment = new Mock(); applicationEnvironment.Setup(a => a.ApplicationName).Returns("TestApplication"); - var localizedString = new LocalizedString("Hello", "Bonjour"); + var localizedString = new LocalizedHtmlString("Hello", "Bonjour"); var htmlLocalizer = new Mock(); htmlLocalizer.Setup(h => h["Hello"]).Returns(localizedString); @@ -49,13 +49,13 @@ namespace Microsoft.AspNet.Mvc.Localization.Test } [Fact] - public void ViewLocalizer_UseIndexerWithArguments_ReturnsLocalizedString() + public void ViewLocalizer_UseIndexerWithArguments_ReturnsLocalizedHtmlString() { // Arrange var applicationEnvironment = new Mock(); applicationEnvironment.Setup(a => a.ApplicationName).Returns("TestApplication"); - var localizedString = new LocalizedString("Hello", "Bonjour test"); + var localizedString = new LocalizedHtmlString("Hello", "Bonjour test"); var htmlLocalizer = new Mock(); htmlLocalizer.Setup(h => h["Hello", "test"]).Returns(localizedString); @@ -81,7 +81,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test } [Fact] - public void ViewLocalizer_GetAllStrings_ReturnsLocalizedHtmlString() + public void ViewLocalizer_GetAllStrings_ReturnsLocalizedString() { // Arrange var stringLocalizer = new TestStringLocalizer(); @@ -98,7 +98,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test viewLocalizer.Contextualize(viewContext); // Act - var allLocalizedStrings = viewLocalizer.GetAllStrings(includeAncestorCultures: false).ToList(); + var allLocalizedStrings = viewLocalizer.GetAllStrings(includeParentCultures: false).ToList(); // Assert Assert.Equal(1, allLocalizedStrings.Count); @@ -106,7 +106,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test } [Fact] - public void ViewLocalizer_GetAllStringsIncludeAncestorCulture_ReturnsLocalizedHtmlString() + public void ViewLocalizer_GetAllStringsIncludeParentCulture_ReturnsLocalizedString() { // Arrange var stringLocalizer = new TestStringLocalizer(); @@ -123,7 +123,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test viewLocalizer.Contextualize(viewContext); // Act - var allLocalizedStrings = viewLocalizer.GetAllStrings().ToList(); + var allLocalizedStrings = viewLocalizer.GetAllStrings(includeParentCultures: true).ToList(); // Assert Assert.Equal(2, allLocalizedStrings.Count); @@ -197,7 +197,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test viewLocalizer.Contextualize(viewContext); // Act - var actualLocalizedString = viewLocalizer.Html("John"); + var actualLocalizedString = viewLocalizer.GetHtml("John"); // Assert Assert.Equal("Hello John", actualLocalizedString.Value); @@ -221,7 +221,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test viewLocalizer.Contextualize(viewContext); // Act - var actualLocalizedString = viewLocalizer.Html("John", "Doe"); + var actualLocalizedString = viewLocalizer.GetHtml("John", "Doe"); // Assert Assert.Equal("Hello John Doe", actualLocalizedString.Value); @@ -260,59 +260,49 @@ namespace Microsoft.AspNet.Mvc.Localization.Test _stringLocalizer = stringLocalizer; } - public LocalizedString this[string name] + public LocalizedHtmlString this[string name] { get { - return _stringLocalizer[name]; + var localizedString = _stringLocalizer.GetString(name); + return new LocalizedHtmlString( + localizedString.Name, + localizedString.Value, + isResourceNotFound: false); } } - public LocalizedString this[string name, params object[] arguments] + public LocalizedHtmlString this[string name, params object[] arguments] { get { - return _stringLocalizer[name, arguments]; + var localizedString = _stringLocalizer.GetString(name, arguments); + return new LocalizedHtmlString( + localizedString.Name, + localizedString.Value, + isResourceNotFound: false, + arguments: arguments); } } - public IEnumerable GetAllStrings(bool includeAncestorCultures) + public IEnumerable GetAllStrings(bool includeParentCultures) { - return _stringLocalizer.GetAllStrings(includeAncestorCultures); + return _stringLocalizer.GetAllStrings(includeParentCultures); } - public IStringLocalizer WithCulture(CultureInfo culture) + public IHtmlLocalizer WithCulture(CultureInfo culture) { return new TestHtmlLocalizer(new TestStringLocalizer(culture)); } - IHtmlLocalizer IHtmlLocalizer.WithCulture(CultureInfo culture) + public LocalizedString GetString(string name) { - return new TestHtmlLocalizer(new TestStringLocalizer(culture)); + return _stringLocalizer.GetString(name); } - public LocalizedHtmlString Html(string key) + public LocalizedString GetString(string name, params object[] arguments) { - var localizedString = _stringLocalizer.GetString(key); - return new LocalizedHtmlString( - localizedString.Name, - localizedString.Value, - isResourceNotFound: false); - } - - public LocalizedHtmlString Html(string key, params object[] arguments) - { - var localizedString = _stringLocalizer.GetString(key, arguments); - return new LocalizedHtmlString( - localizedString.Name, - localizedString.Value, - isResourceNotFound: false, - arguments: arguments); - } - - IEnumerable IStringLocalizer.GetAllStrings(bool includeAncestorCultures) - { - return _stringLocalizer.GetAllStrings(includeAncestorCultures); + return _stringLocalizer.GetString(name, arguments); } }