Improve usability of IHtmlLocalizer & associated API:

- IHtmlLocalizer no longer derives from IStringLocalizer
- IHtmlLocalizer indexer now returns LocalizedHtmlString
- IHtmlLocalizer has GetString methods now that act the same as IStringLocalizer.GetString
- Made LocalizedHtmlString a struct to match LocalizedString
- Updated samples in response to aspnet/Localization#167
- Rename "ancestor" to "parent" for loc API
- Fixes some doc comments
- Fixed tests
- #3716
This commit is contained in:
damianedwards 2015-12-17 13:40:01 -08:00 committed by DamianEdwards
parent 0720d23a19
commit 0c3e7b5a75
16 changed files with 294 additions and 281 deletions

View File

@ -4,4 +4,4 @@
}
@LocString["Hello there!!"]
@ViewBag.Message
@LocString.Html("Hi", "John", @date , @date.DayOfWeek)
@LocString.GetHtml("Hi", "John", @date , @date.DayOfWeek)

View File

@ -9,12 +9,12 @@ using Microsoft.Extensions.Localization;
namespace Microsoft.AspNet.Mvc.Localization
{
/// <summary>
/// An <see cref="IHtmlLocalizer"/> that uses the <see cref="IStringLocalizer"/> to provide localized HTML content.
/// This service just encodes the arguments but not the resource string.
/// An <see cref="IHtmlLocalizer"/> that uses the provided <see cref="IStringLocalizer"/> to do HTML-aware
/// localization of content.
/// </summary>
public class HtmlLocalizer : IHtmlLocalizer
{
private IStringLocalizer _localizer;
private readonly IStringLocalizer _localizer;
/// <summary>
/// Creates a new <see cref="HtmlLocalizer"/>.
@ -31,38 +31,60 @@ namespace Microsoft.AspNet.Mvc.Localization
}
/// <inheritdoc />
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]);
}
}
/// <inheritdoc />
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);
}
}
/// <summary>
/// Creates a new <see cref="IHtmlLocalizer"/> for a specific <see cref="CultureInfo"/>.
/// </summary>
/// <param name="culture">The <see cref="CultureInfo"/> to use.</param>
/// <returns>A culture-specific <see cref="IHtmlLocalizer"/>.</returns>
/// <inheritdoc />
public virtual LocalizedString GetString(string name)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
return _localizer[name];
}
/// <inheritdoc />
public virtual LocalizedString GetString(string name, params object[] arguments)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
return _localizer[name, arguments];
}
/// <inheritdoc />
public virtual IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures) =>
_localizer.GetAllStrings(includeParentCultures);
/// <inheritdoc />
public virtual IHtmlLocalizer WithCulture(CultureInfo culture)
{
if (culture == null)
@ -73,69 +95,6 @@ namespace Microsoft.AspNet.Mvc.Localization
return new HtmlLocalizer(_localizer.WithCulture(culture));
}
/// <summary>
/// Creates a new <see cref="IStringLocalizer"/> for a specific <see cref="CultureInfo"/>.
/// </summary>
/// <param name="culture">The <see cref="CultureInfo"/> to use.</param>
/// <returns>A culture-specific <see cref="IStringLocalizer"/>.</returns>
IStringLocalizer IStringLocalizer.WithCulture(CultureInfo culture)
{
if (culture == null)
{
throw new ArgumentNullException(nameof(culture));
}
return new HtmlLocalizer(_localizer.WithCulture(culture));
}
/// <inheritdoc />
public virtual LocalizedString GetString(string key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
return _localizer.GetString(key);
}
/// <inheritdoc />
public virtual LocalizedString GetString(string key, params object[] arguments)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
return _localizer.GetString(key, arguments);
}
/// <inheritdoc />
public virtual IEnumerable<LocalizedString> GetAllStrings(bool includeAncestorCultures) =>
_localizer.GetAllStrings(includeAncestorCultures);
/// <inheritdoc />
public virtual LocalizedHtmlString Html(string key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
return ToHtmlString(_localizer.GetString(key));
}
/// <inheritdoc />
public virtual LocalizedHtmlString Html(string key, params object[] arguments)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
return ToHtmlString(_localizer.GetString(key), arguments);
}
/// <summary>
/// Creates a new <see cref="LocalizedHtmlString"/> for a <see cref="LocalizedString"/>.
/// </summary>

View File

@ -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
{
/// <summary>
/// Extension methods for <see cref="IHtmlLocalizer"/>.
/// </summary>
public static class HtmlLocalizerExtensions
{
/// <summary>
/// Gets the <see cref="LocalizedHtmlString"/> resource for a specific name.
/// </summary>
/// <param name="key">The key to use.</param>
/// <returns>The <see cref="LocalizedHtmlString"/> resource.</returns>
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];
}
/// <summary>
/// Gets the <see cref="LocalizedHtmlString"/> resource for a specific name.
/// </summary>
/// <param name="key">The key to use.</param>
/// <param name="arguments">The values to format the string with.</param>
/// <returns>The <see cref="LocalizedHtmlString"/> resource.</returns>
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];
}
/// <summary>
/// Gets all string resources including those for parent cultures.
/// </summary>
/// <param name="htmlLocalizer">The <see cref="IHtmlLocalizer"/>.</param>
/// <returns>The string resources.</returns>
public static IEnumerable<LocalizedString> GetAllStrings(this IHtmlLocalizer htmlLocalizer)
{
if (htmlLocalizer == null)
{
throw new ArgumentNullException(nameof(htmlLocalizer));
}
return htmlLocalizer.GetAllStrings(includeParentCultures: true);
}
}
}

View File

@ -7,14 +7,15 @@ using Microsoft.Extensions.Localization;
namespace Microsoft.AspNet.Mvc.Localization
{
/// <summary>
/// An <see cref="IHtmlLocalizerFactory"/> that creates instances of <see cref="HtmlLocalizer"/>.
/// An <see cref="IHtmlLocalizerFactory"/> that creates instances of <see cref="HtmlLocalizer"/> using the
/// registered <see cref="IStringLocalizerFactory"/>.
/// </summary>
public class HtmlLocalizerFactory : IHtmlLocalizerFactory
{
private readonly IStringLocalizerFactory _factory;
/// <summary>
/// Creates a new <see cref="HtmlLocalizer"/>.
/// Creates a new <see cref="HtmlLocalizerFactory"/>.
/// </summary>
/// <param name="localizerFactory">The <see cref="IStringLocalizerFactory"/>.</param>
public HtmlLocalizerFactory(IStringLocalizerFactory localizerFactory)
@ -28,10 +29,9 @@ namespace Microsoft.AspNet.Mvc.Localization
}
/// <summary>
/// Creates an <see cref="HtmlLocalizer"/> using the <see cref="System.Reflection.Assembly"/> and
/// <see cref="Type.FullName"/> of the specified <see cref="Type"/>.
/// Creates an <see cref="HtmlLocalizer"/> using the specified <see cref="Type"/>.
/// </summary>
/// <param name="resourceSource">The <see cref="Type"/>.</param>
/// <param name="resourceSource">The <see cref="Type"/> to load resources for.</param>
/// <returns>The <see cref="HtmlLocalizer"/>.</returns>
public virtual IHtmlLocalizer Create(Type resourceSource)
{
@ -44,7 +44,7 @@ namespace Microsoft.AspNet.Mvc.Localization
}
/// <summary>
/// Creates an <see cref="HtmlLocalizer"/>.
/// Creates an <see cref="HtmlLocalizer"/> using the specified base name and location.
/// </summary>
/// <param name="baseName">The base name of the resource to load strings from.</param>
/// <param name="location">The location to load resources from.</param>

View File

@ -9,7 +9,8 @@ using Microsoft.Extensions.Localization;
namespace Microsoft.AspNet.Mvc.Localization
{
/// <summary>
/// This is an <see cref="IHtmlLocalizer"/> implementation that provides localized HTML content.
/// An <see cref="IHtmlLocalizer"/> implementation that provides localized HTML content for the specified type
/// <typeparamref name="TResource"/>.
/// </summary>
/// <typeparam name="TResource">The <see cref="Type"/> to scope the resource names.</typeparam>
public class HtmlLocalizer<TResource> : IHtmlLocalizer<TResource>
@ -17,7 +18,7 @@ namespace Microsoft.AspNet.Mvc.Localization
private readonly IHtmlLocalizer _localizer;
/// <summary>
/// Creates a new <see cref="HtmlLocalizer"/>.
/// Creates a new <see cref="HtmlLocalizer{TResource}"/>.
/// </summary>
/// <param name="factory">The <see cref="IHtmlLocalizerFactory"/>.</param>
public HtmlLocalizer(IHtmlLocalizerFactory factory)
@ -26,33 +27,59 @@ namespace Microsoft.AspNet.Mvc.Localization
}
/// <inheritdoc />
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];
}
}
/// <inheritdoc />
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];
}
}
/// <inheritdoc />
public virtual LocalizedString GetString(string name)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
return _localizer.GetString(name);
}
/// <inheritdoc />
public virtual LocalizedString GetString(string name, params object[] arguments)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
return _localizer.GetString(name, arguments);
}
/// <inheritdoc />
public virtual IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures) =>
_localizer.GetAllStrings(includeParentCultures);
/// <inheritdoc />
public virtual IHtmlLocalizer WithCulture(CultureInfo culture)
{
@ -63,64 +90,5 @@ namespace Microsoft.AspNet.Mvc.Localization
return _localizer.WithCulture(culture);
}
/// <inheritdoc />
IStringLocalizer IStringLocalizer.WithCulture(CultureInfo culture)
{
if (culture == null)
{
throw new ArgumentNullException(nameof(culture));
}
return _localizer.WithCulture(culture);
}
/// <inheritdoc />
public virtual LocalizedString GetString(string key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
return _localizer.GetString(key);
}
/// <inheritdoc />
public virtual LocalizedString GetString(string key, params object[] arguments)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
return _localizer.GetString(key, arguments);
}
/// <inheritdoc />
public virtual LocalizedHtmlString Html(string key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
return _localizer.Html(key);
}
/// <inheritdoc />
public virtual LocalizedHtmlString Html(string key, params object[] arguments)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
return _localizer.Html(key, arguments);
}
/// <inheritdoc />
public virtual IEnumerable<LocalizedString> GetAllStrings(bool includeAncestorCultures) =>
_localizer.GetAllStrings(includeAncestorCultures);
}
}

View File

@ -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
{
/// <summary>
/// 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.
/// </summary>
public interface IHtmlLocalizer : IStringLocalizer
public interface IHtmlLocalizer
{
/// <summary>
/// Creates a new <see cref="HtmlLocalizer"/> for a specific <see cref="CultureInfo"/>.
/// Gets the string resource with the given name.
/// </summary>
/// <param name="name">The name of the string resource.</param>
/// <returns>The string resource as a <see cref="LocalizedHtmlString"/>.</returns>
LocalizedHtmlString this[string name] { get; }
/// <summary>
/// Gets the string resource with the given name and formatted with the supplied arguments. The arguments will
/// be HTML encoded.
/// </summary>
/// <param name="name">The name of the string resource.</param>
/// <param name="arguments">The values to format the string with.</param>
/// <returns>The formatted string resource as a <see cref="LocalizedHtmlString"/>.</returns>
LocalizedHtmlString this[string name, params object[] arguments] { get; }
/// <summary>
/// Gets the string resource with the given name.
/// </summary>
/// <param name="stringLocalizer">The <see cref="IStringLocalizer"/>.</param>
/// <param name="name">The name of the string resource.</param>
/// <returns>The string resource as a <see cref="LocalizedString"/>.</returns>
LocalizedString GetString(string name);
/// <summary>
/// Gets the string resource with the given name and formatted with the supplied arguments.
/// </summary>
/// <param name="stringLocalizer">The <see cref="IStringLocalizer"/>.</param>
/// <param name="name">The name of the string resource.</param>
/// <param name="arguments">The values to format the string with.</param>
/// <returns>The formatted string resource as a <see cref="LocalizedString"/>.</returns>
LocalizedString GetString(string name, params object[] arguments);
/// <summary>
/// Gets all string resources.
/// </summary>
/// <param name="includeParentCultures">
/// A <see cref="System.Boolean"/> indicating whether to include strings from parent cultures.
/// </param>
/// <returns>The strings.</returns>
IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures);
/// <summary>
/// Creates a new <see cref="IHtmlLocalizer"/> for a specific <see cref="CultureInfo"/>.
/// </summary>
/// <param name="culture">The <see cref="CultureInfo"/> to use.</param>
/// <returns>A culture-specific <see cref="IHtmlLocalizer"/>.</returns>
new IHtmlLocalizer WithCulture(CultureInfo culture);
/// <summary>
/// Gets the <see cref="LocalizedHtmlString"/> resource for a specific key.
/// </summary>
/// <param name="key">The key to use.</param>
/// <returns>The <see cref="LocalizedHtmlString"/> resource.</returns>
LocalizedHtmlString Html(string key);
/// <summary>
/// Gets the <see cref="LocalizedHtmlString"/> resource for a specific key.
/// </summary>
/// <param name="key">The key to use.</param>
/// <param name="arguments">The values to format the string with.</param>
/// <returns>The <see cref="LocalizedHtmlString"/> resource.</returns>
LocalizedHtmlString Html(string key, params object[] arguments);
IHtmlLocalizer WithCulture(CultureInfo culture);
}
}

View File

@ -24,26 +24,36 @@ namespace Microsoft.AspNet.Mvc.Localization
/// <summary>
/// Creates an instance of <see cref="LocalizedHtmlString"/>.
/// </summary>
/// <param name="key">The name of the string resource.</param>
/// <param name="name">The name of the string resource.</param>
/// <param name="value">The string resource.</param>
/// <param name="isResourceNotFound">A flag that indicates if the resource is not found.</param>
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)
{
}
/// <summary>
/// Creates an instance of <see cref="LocalizedHtmlString"/>.
/// </summary>
/// <param name="key">The name of the string resource.</param>
/// <param name="name">The name of the string resource.</param>
/// <param name="value">The string resource.</param>
/// <param name="isResourceNotFound">A flag that indicates if the resource is not found.</param>
public LocalizedHtmlString(string name, string value, bool isResourceNotFound)
: this(name, value, isResourceNotFound, arguments: EmptyArguments)
{
}
/// <summary>
/// Creates an instance of <see cref="LocalizedHtmlString"/>.
/// </summary>
/// <param name="name">The name of the string resource.</param>
/// <param name="value">The string resource.</param>
/// <param name="isResourceNotFound">A flag that indicates if the resource is not found.</param>
/// <param name="arguments">The values to format the <paramref name="value"/> with.</param>
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
/// <summary>
/// The name of the string resource.
/// </summary>
public string Key { get; }
public string Name { get; }
/// <summary>
/// The string resource.

View File

@ -42,7 +42,7 @@ namespace Microsoft.AspNet.Mvc.Localization
}
/// <inheritdoc />
public virtual LocalizedString this[string key]
public virtual LocalizedHtmlString this[string key]
{
get
{
@ -56,7 +56,7 @@ namespace Microsoft.AspNet.Mvc.Localization
}
/// <inheritdoc />
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);
/// <inheritdoc />
public LocalizedHtmlString Html(string key) => _localizer.Html(key);
public IHtmlLocalizer WithCulture(CultureInfo culture) => _localizer.WithCulture(culture);
/// <inheritdoc />
public LocalizedHtmlString Html(string key, params object[] arguments) => _localizer.Html(key, arguments);
/// <inheritdoc />
public IStringLocalizer WithCulture(CultureInfo culture) => _localizer.WithCulture(culture);
/// <inheritdoc />
IHtmlLocalizer IHtmlLocalizer.WithCulture(CultureInfo culture) => _localizer.WithCulture(culture);
public IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures) =>
_localizer.GetAllStrings(includeParentCultures);
/// <summary>
/// Apply the specified <see cref="ViewContext"/>.
/// </summary>
/// <param name="viewContext">The <see cref="ViewContext"/>.</param>
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);
}
/// <inheritdoc />
public IEnumerable<LocalizedString> GetAllStrings(bool includeAncestorCultures) =>
_localizer.GetAllStrings(includeAncestorCultures);
}
}

View File

@ -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<IHtmlLocalizer>();
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<IApplicationEnvironment>();
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<IHtmlLocalizer>();
htmlLocalizer.Setup(h => h["Hello", "test"]).Returns(localizedString);

View File

@ -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<IStringLocalizer>();
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<object[]> 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<FormatException>(
@ -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();

View File

@ -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<LocalizedString> 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<LocalizedString> 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<HomeController> : IHtmlLocalizer<HomeController>
{
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<LocalizedString> 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<LocalizedString> 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

View File

@ -54,12 +54,12 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
}
}
public IEnumerable<LocalizedString> GetAllStrings(bool includeAncestorCultures)
public IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures)
{
var allStrings = new List<LocalizedString>();
allStrings.Add(new LocalizedString("Hello", "World"));
if (includeAncestorCultures)
if (includeParentCultures)
{
allStrings.Add(new LocalizedString("Foo", "Bar"));
}

View File

@ -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<IApplicationEnvironment>();
applicationEnvironment.Setup(a => a.ApplicationName).Returns("TestApplication");
var localizedString = new LocalizedString("Hello", "Bonjour");
var localizedString = new LocalizedHtmlString("Hello", "Bonjour");
var htmlLocalizer = new Mock<IHtmlLocalizer>();
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<IApplicationEnvironment>();
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<IHtmlLocalizer>();
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<LocalizedString> GetAllStrings(bool includeAncestorCultures)
public IEnumerable<LocalizedString> 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<LocalizedString> IStringLocalizer.GetAllStrings(bool includeAncestorCultures)
{
return _stringLocalizer.GetAllStrings(includeAncestorCultures);
return _stringLocalizer.GetString(name, arguments);
}
}