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!!"] @LocString["Hello there!!"]
@ViewBag.Message @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 namespace Microsoft.AspNet.Mvc.Localization
{ {
/// <summary> /// <summary>
/// An <see cref="IHtmlLocalizer"/> that uses the <see cref="IStringLocalizer"/> to provide localized HTML content. /// An <see cref="IHtmlLocalizer"/> that uses the provided <see cref="IStringLocalizer"/> to do HTML-aware
/// This service just encodes the arguments but not the resource string. /// localization of content.
/// </summary> /// </summary>
public class HtmlLocalizer : IHtmlLocalizer public class HtmlLocalizer : IHtmlLocalizer
{ {
private IStringLocalizer _localizer; private readonly IStringLocalizer _localizer;
/// <summary> /// <summary>
/// Creates a new <see cref="HtmlLocalizer"/>. /// Creates a new <see cref="HtmlLocalizer"/>.
@ -31,38 +31,60 @@ namespace Microsoft.AspNet.Mvc.Localization
} }
/// <inheritdoc /> /// <inheritdoc />
public virtual LocalizedString this[string key] public virtual LocalizedHtmlString this[string name]
{ {
get 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 /> /// <inheritdoc />
public virtual LocalizedString this[string key, params object[] arguments] public virtual LocalizedHtmlString this[string name, params object[] arguments]
{ {
get 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> /// <inheritdoc />
/// Creates a new <see cref="IHtmlLocalizer"/> for a specific <see cref="CultureInfo"/>. public virtual LocalizedString GetString(string name)
/// </summary> {
/// <param name="culture">The <see cref="CultureInfo"/> to use.</param> if (name == null)
/// <returns>A culture-specific <see cref="IHtmlLocalizer"/>.</returns> {
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) public virtual IHtmlLocalizer WithCulture(CultureInfo culture)
{ {
if (culture == null) if (culture == null)
@ -73,69 +95,6 @@ namespace Microsoft.AspNet.Mvc.Localization
return new HtmlLocalizer(_localizer.WithCulture(culture)); 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> /// <summary>
/// Creates a new <see cref="LocalizedHtmlString"/> for a <see cref="LocalizedString"/>. /// Creates a new <see cref="LocalizedHtmlString"/> for a <see cref="LocalizedString"/>.
/// </summary> /// </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 namespace Microsoft.AspNet.Mvc.Localization
{ {
/// <summary> /// <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> /// </summary>
public class HtmlLocalizerFactory : IHtmlLocalizerFactory public class HtmlLocalizerFactory : IHtmlLocalizerFactory
{ {
private readonly IStringLocalizerFactory _factory; private readonly IStringLocalizerFactory _factory;
/// <summary> /// <summary>
/// Creates a new <see cref="HtmlLocalizer"/>. /// Creates a new <see cref="HtmlLocalizerFactory"/>.
/// </summary> /// </summary>
/// <param name="localizerFactory">The <see cref="IStringLocalizerFactory"/>.</param> /// <param name="localizerFactory">The <see cref="IStringLocalizerFactory"/>.</param>
public HtmlLocalizerFactory(IStringLocalizerFactory localizerFactory) public HtmlLocalizerFactory(IStringLocalizerFactory localizerFactory)
@ -28,10 +29,9 @@ namespace Microsoft.AspNet.Mvc.Localization
} }
/// <summary> /// <summary>
/// Creates an <see cref="HtmlLocalizer"/> using the <see cref="System.Reflection.Assembly"/> and /// Creates an <see cref="HtmlLocalizer"/> using the specified <see cref="Type"/>.
/// <see cref="Type.FullName"/> of the specified <see cref="Type"/>.
/// </summary> /// </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> /// <returns>The <see cref="HtmlLocalizer"/>.</returns>
public virtual IHtmlLocalizer Create(Type resourceSource) public virtual IHtmlLocalizer Create(Type resourceSource)
{ {
@ -44,7 +44,7 @@ namespace Microsoft.AspNet.Mvc.Localization
} }
/// <summary> /// <summary>
/// Creates an <see cref="HtmlLocalizer"/>. /// Creates an <see cref="HtmlLocalizer"/> using the specified base name and location.
/// </summary> /// </summary>
/// <param name="baseName">The base name of the resource to load strings from.</param> /// <param name="baseName">The base name of the resource to load strings from.</param>
/// <param name="location">The location to load resources 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 namespace Microsoft.AspNet.Mvc.Localization
{ {
/// <summary> /// <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> /// </summary>
/// <typeparam name="TResource">The <see cref="Type"/> to scope the resource names.</typeparam> /// <typeparam name="TResource">The <see cref="Type"/> to scope the resource names.</typeparam>
public class HtmlLocalizer<TResource> : IHtmlLocalizer<TResource> public class HtmlLocalizer<TResource> : IHtmlLocalizer<TResource>
@ -17,7 +18,7 @@ namespace Microsoft.AspNet.Mvc.Localization
private readonly IHtmlLocalizer _localizer; private readonly IHtmlLocalizer _localizer;
/// <summary> /// <summary>
/// Creates a new <see cref="HtmlLocalizer"/>. /// Creates a new <see cref="HtmlLocalizer{TResource}"/>.
/// </summary> /// </summary>
/// <param name="factory">The <see cref="IHtmlLocalizerFactory"/>.</param> /// <param name="factory">The <see cref="IHtmlLocalizerFactory"/>.</param>
public HtmlLocalizer(IHtmlLocalizerFactory factory) public HtmlLocalizer(IHtmlLocalizerFactory factory)
@ -26,33 +27,59 @@ namespace Microsoft.AspNet.Mvc.Localization
} }
/// <inheritdoc /> /// <inheritdoc />
public virtual LocalizedString this[string key] public virtual LocalizedHtmlString this[string name]
{ {
get get
{ {
if (key == null) if (name == null)
{ {
throw new ArgumentNullException(nameof(key)); throw new ArgumentNullException(nameof(name));
} }
return _localizer[key]; return _localizer[name];
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public virtual LocalizedString this[string key, params object[] arguments] public virtual LocalizedHtmlString this[string name, params object[] arguments]
{ {
get 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 /> /// <inheritdoc />
public virtual IHtmlLocalizer WithCulture(CultureInfo culture) public virtual IHtmlLocalizer WithCulture(CultureInfo culture)
{ {
@ -63,64 +90,5 @@ namespace Microsoft.AspNet.Mvc.Localization
return _localizer.WithCulture(culture); 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. // 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. // 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 System.Globalization;
using Microsoft.Extensions.Localization; using Microsoft.Extensions.Localization;
namespace Microsoft.AspNet.Mvc.Localization namespace Microsoft.AspNet.Mvc.Localization
{ {
/// <summary> /// <summary>
/// This service does not HTML encode the resource string. It HTML encodes all arguments that are formatted in /// Represents a type that that does HTML-aware localization of strings, by HTML encoding arguments that are
/// the resource string. /// formatted in the resource string.
/// </summary> /// </summary>
public interface IHtmlLocalizer : IStringLocalizer public interface IHtmlLocalizer
{ {
/// <summary> /// <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> /// </summary>
/// <param name="culture">The <see cref="CultureInfo"/> to use.</param> /// <param name="culture">The <see cref="CultureInfo"/> to use.</param>
/// <returns>A culture-specific <see cref="IHtmlLocalizer"/>.</returns> /// <returns>A culture-specific <see cref="IHtmlLocalizer"/>.</returns>
new IHtmlLocalizer WithCulture(CultureInfo culture); 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);
} }
} }

View File

@ -24,26 +24,36 @@ namespace Microsoft.AspNet.Mvc.Localization
/// <summary> /// <summary>
/// Creates an instance of <see cref="LocalizedHtmlString"/>. /// Creates an instance of <see cref="LocalizedHtmlString"/>.
/// </summary> /// </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="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)
public LocalizedHtmlString(string key, string value, bool isResourceNotFound) : this(name, value, isResourceNotFound: false, arguments: EmptyArguments)
: this(key, value, isResourceNotFound, arguments: EmptyArguments)
{ {
} }
/// <summary> /// <summary>
/// Creates an instance of <see cref="LocalizedHtmlString"/>. /// Creates an instance of <see cref="LocalizedHtmlString"/>.
/// </summary> /// </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="value">The string resource.</param>
/// <param name="isResourceNotFound">A flag that indicates if the resource is not found.</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> /// <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) if (value == null)
@ -56,7 +66,7 @@ namespace Microsoft.AspNet.Mvc.Localization
throw new ArgumentNullException(nameof(arguments)); throw new ArgumentNullException(nameof(arguments));
} }
Key = key; Name = name;
Value = value; Value = value;
IsResourceNotFound = isResourceNotFound; IsResourceNotFound = isResourceNotFound;
_arguments = arguments; _arguments = arguments;
@ -65,7 +75,7 @@ namespace Microsoft.AspNet.Mvc.Localization
/// <summary> /// <summary>
/// The name of the string resource. /// The name of the string resource.
/// </summary> /// </summary>
public string Key { get; } public string Name { get; }
/// <summary> /// <summary>
/// The string resource. /// The string resource.

View File

@ -42,7 +42,7 @@ namespace Microsoft.AspNet.Mvc.Localization
} }
/// <inheritdoc /> /// <inheritdoc />
public virtual LocalizedString this[string key] public virtual LocalizedHtmlString this[string key]
{ {
get get
{ {
@ -56,7 +56,7 @@ namespace Microsoft.AspNet.Mvc.Localization
} }
/// <inheritdoc /> /// <inheritdoc />
public virtual LocalizedString this[string key, params object[] arguments] public virtual LocalizedHtmlString this[string key, params object[] arguments]
{ {
get get
{ {
@ -76,17 +76,16 @@ namespace Microsoft.AspNet.Mvc.Localization
public LocalizedString GetString(string name, params object[] values) => _localizer.GetString(name, values); public LocalizedString GetString(string name, params object[] values) => _localizer.GetString(name, values);
/// <inheritdoc /> /// <inheritdoc />
public LocalizedHtmlString Html(string key) => _localizer.Html(key); public IHtmlLocalizer WithCulture(CultureInfo culture) => _localizer.WithCulture(culture);
/// <inheritdoc /> /// <inheritdoc />
public LocalizedHtmlString Html(string key, params object[] arguments) => _localizer.Html(key, arguments); public IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures) =>
_localizer.GetAllStrings(includeParentCultures);
/// <inheritdoc />
public IStringLocalizer WithCulture(CultureInfo culture) => _localizer.WithCulture(culture);
/// <inheritdoc />
IHtmlLocalizer IHtmlLocalizer.WithCulture(CultureInfo culture) => _localizer.WithCulture(culture);
/// <summary>
/// Apply the specified <see cref="ViewContext"/>.
/// </summary>
/// <param name="viewContext">The <see cref="ViewContext"/>.</param>
public void Contextualize(ViewContext viewContext) public void Contextualize(ViewContext viewContext)
{ {
if (viewContext == null) if (viewContext == null)
@ -95,16 +94,12 @@ namespace Microsoft.AspNet.Mvc.Localization
} }
var baseName = viewContext.View.Path.Replace('/', '.').Replace('\\', '.'); var baseName = viewContext.View.Path.Replace('/', '.').Replace('\\', '.');
if (baseName.StartsWith(".")) if (baseName.StartsWith(".", StringComparison.OrdinalIgnoreCase))
{ {
baseName = baseName.Substring(1); baseName = baseName.Substring(1);
} }
_localizer = _localizerFactory.Create(baseName, _applicationName); _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 public class HtmlLocalizerOfTTest
{ {
[Fact] [Fact]
public void HtmlLocalizerOfTTest_UseIndexer_ReturnsLocalizedString() public void HtmlLocalizerOfTTest_UseIndexer_ReturnsLocalizedHtmlString()
{ {
// Arrange // Arrange
var localizedString = new LocalizedString("Hello", "Bonjour"); var localizedString = new LocalizedHtmlString("Hello", "Bonjour");
var htmlLocalizer = new Mock<IHtmlLocalizer>(); var htmlLocalizer = new Mock<IHtmlLocalizer>();
htmlLocalizer.Setup(h => h["Hello"]).Returns(localizedString); htmlLocalizer.Setup(h => h["Hello"]).Returns(localizedString);
@ -36,13 +36,13 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
} }
[Fact] [Fact]
public void HtmlLocalizerOfTTest_UseIndexerWithArguments_ReturnsLocalizedString() public void HtmlLocalizerOfTTest_UseIndexerWithArguments_ReturnsLocalizedHtmlString()
{ {
// Arrange // Arrange
var applicationEnvironment = new Mock<IApplicationEnvironment>(); var applicationEnvironment = new Mock<IApplicationEnvironment>();
applicationEnvironment.Setup(a => a.ApplicationName).Returns("TestApplication"); 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>(); var htmlLocalizer = new Mock<IHtmlLocalizer>();
htmlLocalizer.Setup(h => h["Hello", "test"]).Returns(localizedString); htmlLocalizer.Setup(h => h["Hello", "test"]).Returns(localizedString);

View File

@ -17,7 +17,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
public class HtmlLocalizerTest public class HtmlLocalizerTest
{ {
[Fact] [Fact]
public void HtmlLocalizer_UseIndexer_ReturnsLocalizedString() public void HtmlLocalizer_UseIndexer_ReturnsLocalizedHtmlString()
{ {
// Arrange // Arrange
var localizedString = new LocalizedString("Hello", "Bonjour"); var localizedString = new LocalizedString("Hello", "Bonjour");
@ -27,28 +27,30 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
var htmlLocalizer = new HtmlLocalizer(stringLocalizer.Object); var htmlLocalizer = new HtmlLocalizer(stringLocalizer.Object);
// Act // Act
var actualLocalizedString = htmlLocalizer["Hello"]; var actualLocalizedHtmlString = htmlLocalizer["Hello"];
// Assert // Assert
Assert.Equal(localizedString, actualLocalizedString); Assert.Equal(localizedString.Name, actualLocalizedHtmlString.Name);
Assert.Equal(localizedString.Value, actualLocalizedHtmlString.Value);
} }
[Fact] [Fact]
public void HtmlLocalizer_UseIndexerWithArguments_ReturnsLocalizedString() public void HtmlLocalizer_UseIndexerWithArguments_ReturnsLocalizedHtmlString()
{ {
// Arrange // Arrange
var localizedString = new LocalizedString("Hello", "Bonjour test"); var localizedString = new LocalizedString("Hello", "Bonjour test");
var stringLocalizer = new Mock<IStringLocalizer>(); 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); var htmlLocalizer = new HtmlLocalizer(stringLocalizer.Object);
// Act // Act
var actualLocalizedString = htmlLocalizer["Hello", "test"]; var actualLocalizedHtmlString = htmlLocalizer["Hello", "test"];
// Assert // Assert
Assert.Equal(localizedString, actualLocalizedString); Assert.Equal(localizedString.Name, actualLocalizedHtmlString.Name);
Assert.Equal(localizedString.Value, actualLocalizedHtmlString.Value);
} }
public static IEnumerable<object[]> HtmlData public static IEnumerable<object[]> HtmlData
@ -108,7 +110,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
var htmlLocalizer = new HtmlLocalizer(stringLocalizer.Object); var htmlLocalizer = new HtmlLocalizer(stringLocalizer.Object);
// Act // Act
var localizedHtmlString = htmlLocalizer.Html("Hello", arguments); var localizedHtmlString = htmlLocalizer.GetHtml("Hello", arguments);
// Assert // Assert
Assert.NotNull(localizedHtmlString); Assert.NotNull(localizedHtmlString);
@ -150,7 +152,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
stringLocalizer.Setup(s => s["Hello"]).Returns(localizedString); stringLocalizer.Setup(s => s["Hello"]).Returns(localizedString);
var htmlLocalizer = new HtmlLocalizer(stringLocalizer.Object); var htmlLocalizer = new HtmlLocalizer(stringLocalizer.Object);
var content = htmlLocalizer.Html("Hello", new object[] { }); var content = htmlLocalizer.GetHtml("Hello", new object[] { });
// Act // Act
var exception = Assert.Throws<FormatException>( var exception = Assert.Throws<FormatException>(
@ -200,10 +202,10 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
var htmlLocalizer = new HtmlLocalizer(stringLocalizer); var htmlLocalizer = new HtmlLocalizer(stringLocalizer);
// Act // Act
var actualLocalizedString = htmlLocalizer.Html("John"); var actualLocalizedHtmlString = htmlLocalizer.GetHtml("John");
// Assert // Assert
Assert.Equal("Hello John", actualLocalizedString.Value); Assert.Equal("Hello John", actualLocalizedHtmlString.Value);
} }
[Fact] [Fact]
@ -215,10 +217,10 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
var htmlLocalizer = new HtmlLocalizer(stringLocalizer); var htmlLocalizer = new HtmlLocalizer(stringLocalizer);
// Act // Act
var actualLocalizedString = htmlLocalizer.WithCulture(new CultureInfo("fr"))["John"]; var actualLocalizedHtmlString = htmlLocalizer.WithCulture(new CultureInfo("fr"))["John"];
// Assert // Assert
Assert.Equal("Bonjour John", actualLocalizedString.Value); Assert.Equal("Bonjour John", actualLocalizedHtmlString.Value);
} }
[Fact] [Fact]
@ -230,7 +232,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
var htmlLocalizer = new HtmlLocalizer(stringLocalizer); var htmlLocalizer = new HtmlLocalizer(stringLocalizer);
// Act // Act
var allLocalizedStrings = htmlLocalizer.GetAllStrings(includeAncestorCultures: false).ToList(); var allLocalizedStrings = htmlLocalizer.GetAllStrings(includeParentCultures: false).ToList();
//Assert //Assert
Assert.Equal(1, allLocalizedStrings.Count); Assert.Equal(1, allLocalizedStrings.Count);
@ -238,7 +240,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
} }
[Fact] [Fact]
public void HtmlLocalizer_GetAllStringsIncludeAncestorCulture_ReturnsAllLocalizedStrings() public void HtmlLocalizer_GetAllStringsIncludeParentCulture_ReturnsAllLocalizedStrings()
{ {
// Arrange // Arrange
var stringLocalizer = new TestStringLocalizer(); var stringLocalizer = new TestStringLocalizer();

View File

@ -219,7 +219,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Internal
public class TestViewLocalizer : IViewLocalizer public class TestViewLocalizer : IViewLocalizer
{ {
public LocalizedString this[string name] public LocalizedHtmlString this[string name]
{ {
get 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 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(); throw new NotImplementedException();
} }
public LocalizedHtmlString Html(string key) public LocalizedString GetString(string name, params object[] arguments)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public LocalizedHtmlString Html(string key, params object[] arguments) public IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@ -254,16 +254,11 @@ namespace Microsoft.AspNet.Mvc.Localization.Internal
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
IStringLocalizer IStringLocalizer.WithCulture(CultureInfo culture)
{
throw new NotImplementedException();
}
} }
public class TestHtmlLocalizer<HomeController> : IHtmlLocalizer<HomeController> public class TestHtmlLocalizer<HomeController> : IHtmlLocalizer<HomeController>
{ {
public LocalizedString this[string name] public LocalizedHtmlString this[string name]
{ {
get 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 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(); throw new NotImplementedException();
} }
public LocalizedHtmlString Html(string key) public LocalizedString GetString(string name, params object[] arguments)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public LocalizedHtmlString Html(string key, params object[] arguments) public IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@ -298,11 +293,6 @@ namespace Microsoft.AspNet.Mvc.Localization.Internal
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
IStringLocalizer IStringLocalizer.WithCulture(CultureInfo culture)
{
throw new NotImplementedException();
}
} }
public class TestHtmlLocalizerFactory : IHtmlLocalizerFactory 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>(); var allStrings = new List<LocalizedString>();
allStrings.Add(new LocalizedString("Hello", "World")); allStrings.Add(new LocalizedString("Hello", "World"));
if (includeAncestorCultures) if (includeParentCultures)
{ {
allStrings.Add(new LocalizedString("Foo", "Bar")); allStrings.Add(new LocalizedString("Foo", "Bar"));
} }

View File

@ -17,13 +17,13 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
public class ViewLocalizerTest public class ViewLocalizerTest
{ {
[Fact] [Fact]
public void ViewLocalizer_UseIndexer_ReturnsLocalizedString() public void ViewLocalizer_UseIndexer_ReturnsLocalizedHtmlString()
{ {
// Arrange // Arrange
var applicationEnvironment = new Mock<IApplicationEnvironment>(); var applicationEnvironment = new Mock<IApplicationEnvironment>();
applicationEnvironment.Setup(a => a.ApplicationName).Returns("TestApplication"); applicationEnvironment.Setup(a => a.ApplicationName).Returns("TestApplication");
var localizedString = new LocalizedString("Hello", "Bonjour"); var localizedString = new LocalizedHtmlString("Hello", "Bonjour");
var htmlLocalizer = new Mock<IHtmlLocalizer>(); var htmlLocalizer = new Mock<IHtmlLocalizer>();
htmlLocalizer.Setup(h => h["Hello"]).Returns(localizedString); htmlLocalizer.Setup(h => h["Hello"]).Returns(localizedString);
@ -49,13 +49,13 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
} }
[Fact] [Fact]
public void ViewLocalizer_UseIndexerWithArguments_ReturnsLocalizedString() public void ViewLocalizer_UseIndexerWithArguments_ReturnsLocalizedHtmlString()
{ {
// Arrange // Arrange
var applicationEnvironment = new Mock<IApplicationEnvironment>(); var applicationEnvironment = new Mock<IApplicationEnvironment>();
applicationEnvironment.Setup(a => a.ApplicationName).Returns("TestApplication"); 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>(); var htmlLocalizer = new Mock<IHtmlLocalizer>();
htmlLocalizer.Setup(h => h["Hello", "test"]).Returns(localizedString); htmlLocalizer.Setup(h => h["Hello", "test"]).Returns(localizedString);
@ -81,7 +81,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
} }
[Fact] [Fact]
public void ViewLocalizer_GetAllStrings_ReturnsLocalizedHtmlString() public void ViewLocalizer_GetAllStrings_ReturnsLocalizedString()
{ {
// Arrange // Arrange
var stringLocalizer = new TestStringLocalizer(); var stringLocalizer = new TestStringLocalizer();
@ -98,7 +98,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
viewLocalizer.Contextualize(viewContext); viewLocalizer.Contextualize(viewContext);
// Act // Act
var allLocalizedStrings = viewLocalizer.GetAllStrings(includeAncestorCultures: false).ToList(); var allLocalizedStrings = viewLocalizer.GetAllStrings(includeParentCultures: false).ToList();
// Assert // Assert
Assert.Equal(1, allLocalizedStrings.Count); Assert.Equal(1, allLocalizedStrings.Count);
@ -106,7 +106,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
} }
[Fact] [Fact]
public void ViewLocalizer_GetAllStringsIncludeAncestorCulture_ReturnsLocalizedHtmlString() public void ViewLocalizer_GetAllStringsIncludeParentCulture_ReturnsLocalizedString()
{ {
// Arrange // Arrange
var stringLocalizer = new TestStringLocalizer(); var stringLocalizer = new TestStringLocalizer();
@ -123,7 +123,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
viewLocalizer.Contextualize(viewContext); viewLocalizer.Contextualize(viewContext);
// Act // Act
var allLocalizedStrings = viewLocalizer.GetAllStrings().ToList(); var allLocalizedStrings = viewLocalizer.GetAllStrings(includeParentCultures: true).ToList();
// Assert // Assert
Assert.Equal(2, allLocalizedStrings.Count); Assert.Equal(2, allLocalizedStrings.Count);
@ -197,7 +197,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
viewLocalizer.Contextualize(viewContext); viewLocalizer.Contextualize(viewContext);
// Act // Act
var actualLocalizedString = viewLocalizer.Html("John"); var actualLocalizedString = viewLocalizer.GetHtml("John");
// Assert // Assert
Assert.Equal("Hello John", actualLocalizedString.Value); Assert.Equal("Hello John", actualLocalizedString.Value);
@ -221,7 +221,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
viewLocalizer.Contextualize(viewContext); viewLocalizer.Contextualize(viewContext);
// Act // Act
var actualLocalizedString = viewLocalizer.Html("John", "Doe"); var actualLocalizedString = viewLocalizer.GetHtml("John", "Doe");
// Assert // Assert
Assert.Equal("Hello John Doe", actualLocalizedString.Value); Assert.Equal("Hello John Doe", actualLocalizedString.Value);
@ -260,59 +260,49 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
_stringLocalizer = stringLocalizer; _stringLocalizer = stringLocalizer;
} }
public LocalizedString this[string name] public LocalizedHtmlString this[string name]
{ {
get 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 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)); 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 _stringLocalizer.GetString(name, arguments);
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);
} }
} }