Move logic from `HtmlLocalizer` to `LocalizedHtmlString`

- part of #3123 (4 of 5)
- `LocalizedHtmlString` should not subclass `HtmlString`; now implements `IHtmlContent`

nits:
- clean up a few doc comments
- remove duplicate tests reported while testing these fixes in VS
This commit is contained in:
Doug Bunting 2015-12-02 18:13:59 -08:00
parent 9fc3a80056
commit 42f3e764b0
10 changed files with 232 additions and 195 deletions

View File

@ -4,8 +4,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.Encodings.Web;
using Microsoft.Extensions.Localization;
namespace Microsoft.AspNet.Mvc.Localization
@ -17,27 +15,19 @@ namespace Microsoft.AspNet.Mvc.Localization
public class HtmlLocalizer : IHtmlLocalizer
{
private IStringLocalizer _localizer;
private readonly HtmlEncoder _encoder;
/// <summary>
/// Creates a new <see cref="HtmlLocalizer"/>.
/// </summary>
/// <param name="localizer">The <see cref="IStringLocalizer"/> to read strings from.</param>
/// <param name="encoder">The <see cref="HtmlEncoder"/>.</param>
public HtmlLocalizer(IStringLocalizer localizer, HtmlEncoder encoder)
public HtmlLocalizer(IStringLocalizer localizer)
{
if (localizer == null)
{
throw new ArgumentNullException(nameof(localizer));
}
if (encoder == null)
{
throw new ArgumentNullException(nameof(encoder));
}
_localizer = localizer;
_encoder = encoder;
}
/// <inheritdoc />
@ -80,7 +70,7 @@ namespace Microsoft.AspNet.Mvc.Localization
throw new ArgumentNullException(nameof(culture));
}
return new HtmlLocalizer(_localizer.WithCulture(culture), _encoder);
return new HtmlLocalizer(_localizer.WithCulture(culture));
}
/// <summary>
@ -95,7 +85,7 @@ namespace Microsoft.AspNet.Mvc.Localization
throw new ArgumentNullException(nameof(culture));
}
return new HtmlLocalizer(_localizer.WithCulture(culture), _encoder);
return new HtmlLocalizer(_localizer.WithCulture(culture));
}
/// <inheritdoc />
@ -143,9 +133,7 @@ namespace Microsoft.AspNet.Mvc.Localization
throw new ArgumentNullException(nameof(key));
}
var stringValue = _localizer[key].Value;
return ToHtmlString(new LocalizedString(key, EncodeArguments(stringValue, arguments)));
return ToHtmlString(_localizer.GetString(key), arguments);
}
/// <summary>
@ -155,118 +143,7 @@ namespace Microsoft.AspNet.Mvc.Localization
protected virtual LocalizedHtmlString ToHtmlString(LocalizedString result) =>
new LocalizedHtmlString(result.Name, result.Value, result.ResourceNotFound);
/// <summary>
/// Encodes the arguments based on the object type.
/// </summary>
/// <param name="resourceString">The resourceString whose arguments need to be encoded.</param>
/// <param name="arguments">The array of objects to encode.</param>
/// <returns>The string with encoded arguments.</returns>
protected virtual string EncodeArguments(string resourceString, object[] arguments)
{
if (resourceString == null)
{
throw new ArgumentNullException(nameof(resourceString));
}
if (arguments == null)
{
throw new ArgumentNullException(nameof(arguments));
}
var position = 0;
var length = resourceString.Length;
char currentCharacter;
StringBuilder tokenBuffer = null;
var outputBuffer = new StringBuilder();
var isToken = false;
while (position < length)
{
currentCharacter = resourceString[position];
position++;
if (currentCharacter == '}')
{
if (position < length && resourceString[position] == '}') // Treat as escape character for }}
{
if (isToken)
{
tokenBuffer.Append("}}");
}
else
{
outputBuffer.Append("}");
}
position++;
}
else
{
AppendToBuffer(isToken, '}', tokenBuffer, outputBuffer);
if (position == length)
{
break;
}
AppendToOutputBuffer(arguments, tokenBuffer, outputBuffer);
isToken = false;
tokenBuffer = null;
}
}
else if (currentCharacter == '{')
{
if (position < length && resourceString[position] == '{') // Treat as escape character for {{
{
if (isToken)
{
tokenBuffer.Append("{{");
}
else
{
outputBuffer.Append("{");
}
position++;
}
else
{
tokenBuffer = new StringBuilder();
tokenBuffer.Append("{");
isToken = true;
}
}
else
{
AppendToBuffer(isToken, currentCharacter, tokenBuffer, outputBuffer);
}
}
AppendToOutputBuffer(arguments, tokenBuffer, outputBuffer);
return outputBuffer.ToString();
}
private void AppendToBuffer(
bool isToken,
char value,
StringBuilder tokenBuffer,
StringBuilder outputBuffer)
{
if (isToken)
{
tokenBuffer.Append(value);
}
else
{
outputBuffer.Append(value);
}
}
private void AppendToOutputBuffer(object[] arguments, StringBuilder tokenBuffer, StringBuilder outputBuffer)
{
if (tokenBuffer != null && tokenBuffer.Length > 0)
{
outputBuffer.Append(_encoder.Encode(string.Format(tokenBuffer.ToString(), arguments)));
}
}
protected virtual LocalizedHtmlString ToHtmlString(LocalizedString result, object[] arguments) =>
new LocalizedHtmlString(result.Name, result.Value, result.ResourceNotFound, arguments);
}
}

View File

@ -2,7 +2,6 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Text.Encodings.Web;
using Microsoft.Extensions.Localization;
namespace Microsoft.AspNet.Mvc.Localization
@ -13,27 +12,19 @@ namespace Microsoft.AspNet.Mvc.Localization
public class HtmlLocalizerFactory : IHtmlLocalizerFactory
{
private readonly IStringLocalizerFactory _factory;
private readonly HtmlEncoder _encoder;
/// <summary>
/// Creates a new <see cref="HtmlLocalizer"/>.
/// </summary>
/// <param name="localizerFactory">The <see cref="IStringLocalizerFactory"/>.</param>
/// <param name="encoder">The <see cref="HtmlEncoder"/>.</param>
public HtmlLocalizerFactory(IStringLocalizerFactory localizerFactory, HtmlEncoder encoder)
public HtmlLocalizerFactory(IStringLocalizerFactory localizerFactory)
{
if (localizerFactory == null)
{
throw new ArgumentNullException(nameof(localizerFactory));
}
if (encoder == null)
{
throw new ArgumentNullException(nameof(encoder));
}
_factory = localizerFactory;
_encoder = encoder;
}
/// <summary>
@ -49,7 +40,7 @@ namespace Microsoft.AspNet.Mvc.Localization
throw new ArgumentNullException(nameof(resourceSource));
}
return new HtmlLocalizer(_factory.Create(resourceSource), _encoder);
return new HtmlLocalizer(_factory.Create(resourceSource));
}
/// <summary>
@ -71,7 +62,7 @@ namespace Microsoft.AspNet.Mvc.Localization
}
var localizer = _factory.Create(baseName, location);
return new HtmlLocalizer(localizer, _encoder);
return new HtmlLocalizer(localizer);
}
}
}

View File

@ -9,9 +9,9 @@ using Microsoft.Extensions.Localization;
namespace Microsoft.AspNet.Mvc.Localization
{
/// <summary>
/// This is an <see cref="HtmlLocalizer"/> that provides localized HTML content.
/// This is an <see cref="IHtmlLocalizer"/> implementation that provides localized HTML content.
/// </summary>
/// <typeparam name = "TResource"> The <see cref="System.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>
{
private readonly IHtmlLocalizer _localizer;

View File

@ -6,7 +6,7 @@ namespace Microsoft.AspNet.Mvc.Localization
/// <summary>
/// An <see cref="IHtmlLocalizer"/> that provides localized HTML content.
/// </summary>
/// <typeparam name = "TResource"> The <see cref="System.Type"/> to scope the resource names.</typeparam>
/// <typeparam name="TResource">The <see cref="System.Type"/> to scope the resource names.</typeparam>
public interface IHtmlLocalizer<TResource> : IHtmlLocalizer
{
}

View File

@ -1,22 +1,34 @@
// 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 Microsoft.AspNet.Mvc.Rendering;
using System;
using System.IO;
using System.Text;
using System.Text.Encodings.Web;
using Microsoft.AspNet.Html;
namespace Microsoft.AspNet.Mvc.Localization
{
/// <summary>
/// An <see cref="HtmlString"/> with localized content.
/// An <see cref="IHtmlContent"/> with localized content.
/// </summary>
public class LocalizedHtmlString : HtmlString
public class LocalizedHtmlString : IHtmlContent
{
#if DOTNET5_5
private static readonly object[] EmptyArguments = Array.Empty<object>();
#else
private static readonly object[] EmptyArguments = new object[0];
#endif
private readonly object[] _arguments;
/// <summary>
/// Creates an instance of <see cref="LocalizedHtmlString"/>.
/// </summary>
/// <param name="key">The name of the string resource.</param>
/// <param name="value">The string resource.</param>
public LocalizedHtmlString(string key, string value)
: this(key, value, isResourceNotFound: false)
/// <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)
{
}
@ -26,11 +38,28 @@ namespace Microsoft.AspNet.Mvc.Localization
/// <param name="key">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)
: base(value)
/// <param name="arguments">The values to format the <paramref name="value"/> with.</param>
public LocalizedHtmlString(string key, string value, bool isResourceNotFound, params object[] arguments)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
if (arguments == null)
{
throw new ArgumentNullException(nameof(arguments));
}
Key = key;
Value = value;
IsResourceNotFound = isResourceNotFound;
_arguments = arguments;
}
/// <summary>
@ -41,11 +70,145 @@ namespace Microsoft.AspNet.Mvc.Localization
/// <summary>
/// The string resource.
/// </summary>
public string Value => ToString();
public string Value { get; }
/// <summary>
/// Gets a flag that indicates if the resource is not found.
/// </summary>
public bool IsResourceNotFound { get; }
/// <inheritdoc />
public void WriteTo(TextWriter writer, HtmlEncoder encoder)
{
if (writer == null)
{
throw new ArgumentNullException(nameof(writer));
}
if (encoder == null)
{
throw new ArgumentNullException(nameof(encoder));
}
var htmlTextWriter = writer as HtmlTextWriter;
if (htmlTextWriter == null)
{
FormatValue(writer, encoder, Value, _arguments);
}
else
{
htmlTextWriter.Write(this);
}
}
private static void FormatValue(
TextWriter writer,
HtmlEncoder encoder,
string resourceString,
object[] arguments)
{
var position = 0;
var length = resourceString.Length;
StringBuilder tokenBuffer = null;
var isToken = false;
while (position < length)
{
var currentCharacter = resourceString[position];
position++;
if (currentCharacter == '}')
{
if (position < length && resourceString[position] == '}')
{
// Escaped curly brace: "}}".
AppendCurlyBrace(isToken, currentCharacter, tokenBuffer, writer);
position++;
}
else
{
// End of a token.
Append(isToken, '}', tokenBuffer, writer);
if (position == length)
{
break;
}
AppendToOutput(tokenBuffer, arguments, writer, encoder);
isToken = false;
tokenBuffer = null;
}
}
else if (currentCharacter == '{')
{
if (position < length && resourceString[position] == '{')
{
// Escaped curly brace: "{{".
AppendCurlyBrace(isToken, currentCharacter, tokenBuffer, writer);
position++;
}
else
{
// Start of a new token.
tokenBuffer = new StringBuilder();
tokenBuffer.Append('{');
isToken = true;
}
}
else
{
Append(isToken, currentCharacter, tokenBuffer, writer);
}
}
AppendToOutput(tokenBuffer, arguments, writer, encoder);
}
private static void Append(
bool isToken,
char value,
StringBuilder tokenBuffer,
TextWriter writer)
{
if (isToken)
{
tokenBuffer.Append(value);
}
else
{
writer.Write(value);
}
}
private static void AppendCurlyBrace(
bool isToken,
char curlyBrace,
StringBuilder tokenBuffer,
TextWriter writer)
{
if (isToken)
{
tokenBuffer
.Append(curlyBrace)
.Append(curlyBrace);
}
else
{
writer.Write(curlyBrace);
}
}
private static void AppendToOutput(
StringBuilder tokenBuffer,
object[] arguments,
TextWriter writer,
HtmlEncoder encoder)
{
if (tokenBuffer != null && tokenBuffer.Length > 0)
{
encoder.Encode(writer, string.Format(tokenBuffer.ToString(), arguments));
}
}
}
}

View File

@ -12,7 +12,7 @@ using Microsoft.Extensions.PlatformAbstractions;
namespace Microsoft.AspNet.Mvc.Localization
{
/// <summary>
/// A <see cref="HtmlLocalizer"/> that provides localized strings for views.
/// A <see cref="IHtmlLocalizer"/> implementation that provides localized strings for views.
/// </summary>
public class ViewLocalizer : IViewLocalizer, ICanHasViewContext
{
@ -25,9 +25,7 @@ namespace Microsoft.AspNet.Mvc.Localization
/// </summary>
/// <param name="localizerFactory">The <see cref="IHtmlLocalizerFactory"/>.</param>
/// <param name="applicationEnvironment">The <see cref="IApplicationEnvironment"/>.</param>
public ViewLocalizer(
IHtmlLocalizerFactory localizerFactory,
IApplicationEnvironment applicationEnvironment)
public ViewLocalizer(IHtmlLocalizerFactory localizerFactory, IApplicationEnvironment applicationEnvironment)
{
if (localizerFactory == null)
{
@ -75,15 +73,13 @@ namespace Microsoft.AspNet.Mvc.Localization
public LocalizedString GetString(string name) => _localizer.GetString(name);
/// <inheritdoc />
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 />
public LocalizedHtmlString Html(string key) => _localizer.Html(key);
/// <inheritdoc />
public LocalizedHtmlString Html(string key, params object[] arguments) =>
_localizer.Html(key, arguments);
public LocalizedHtmlString Html(string key, params object[] arguments) => _localizer.Html(key, arguments);
/// <inheritdoc />
public IStringLocalizer WithCulture(CultureInfo culture) => _localizer.WithCulture(culture);
@ -103,6 +99,7 @@ namespace Microsoft.AspNet.Mvc.Localization
{
baseName = baseName.Substring(1);
}
_localizer = _localizerFactory.Create(baseName, _applicationName);
}

View File

@ -1357,7 +1357,6 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
}
}
// Reviewers: Is this inconsistency with CanUpdateProperty() an issue we should be tracking?
[Theory]
[MemberData(nameof(MyCanUpdateButCannotSetPropertyData))]
public void SetProperty_ValueProvidedAndCanUpdatePropertyTrue_DoesNothing(

View File

@ -26,14 +26,12 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
{
"/Home/Create",
"/Home/Create?Name=Billy&Blurb=hello&DateOfBirth=2000-11-30&YearsEmployeed=0",
"/Home/Create",
"/Home/Create?Name=Joe&Blurb=goodbye&DateOfBirth=1980-10-20&YearsEmployeed=1",
"/Home/Edit/0",
"/Home/Edit/0?Name=Bobby&Blurb=howdy&DateOfBirth=1999-11-30&YearsEmployeed=1",
"/Home/Edit/0?Name=Bobby&Blurb=howdy&DateOfBirth=1999-11-30&YearsEmployeed=2",
"/Home/Edit/1",
"/Home/Edit/1?Name=Jack&Blurb=goodbye&DateOfBirth=1979-10-20&YearsEmployeed=4",
"/Home/Edit/0",
"/Home/Edit/0?Name=Bobby&Blurb=howdy&DateOfBirth=1999-11-30&YearsEmployeed=2",
};
// Three paths hit aspnet/External#50 with Mono on Mac.

View File

@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using Microsoft.AspNet.Testing;
using Microsoft.Extensions.Localization;
@ -23,7 +24,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
var stringLocalizer = new Mock<IStringLocalizer>();
stringLocalizer.Setup(s => s["Hello"]).Returns(localizedString);
var htmlLocalizer = new HtmlLocalizer(stringLocalizer.Object, new HtmlTestEncoder());
var htmlLocalizer = new HtmlLocalizer(stringLocalizer.Object);
// Act
var actualLocalizedString = htmlLocalizer["Hello"];
@ -41,7 +42,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
var stringLocalizer = new Mock<IStringLocalizer>();
stringLocalizer.Setup(s => s["Hello", "test"]).Returns(localizedString);
var htmlLocalizer = new HtmlLocalizer(stringLocalizer.Object, new HtmlTestEncoder());
var htmlLocalizer = new HtmlLocalizer(stringLocalizer.Object);
// Act
var actualLocalizedString = htmlLocalizer["Hello", "test"];
@ -104,14 +105,19 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
var stringLocalizer = new Mock<IStringLocalizer>();
stringLocalizer.Setup(s => s["Hello"]).Returns(localizedString);
var htmlLocalizer = new HtmlLocalizer(stringLocalizer.Object, new HtmlTestEncoder());
var htmlLocalizer = new HtmlLocalizer(stringLocalizer.Object);
// Act
var localizedHtmlString = htmlLocalizer.Html("Hello", arguments);
// Assert
Assert.NotNull(localizedHtmlString);
Assert.Equal(expectedText, localizedHtmlString.Value);
Assert.Equal(format, localizedHtmlString.Value);
using (var writer = new StringWriter())
{
localizedHtmlString.WriteTo(writer, new HtmlTestEncoder());
Assert.Equal(expectedText, writer.ToString());
}
}
public static TheoryData<string> InvalidResourceStringData
@ -135,7 +141,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
[Theory]
[MemberData(nameof(InvalidResourceStringData))]
public void HtmlLocalizer_HtmlWithInvalidResourcestring_ThrowsException(string format)
public void HtmlLocalizer_HtmlWithInvalidResourceString_ContentThrowsException(string format)
{
// Arrange
var localizedString = new LocalizedString("Hello", format);
@ -143,10 +149,12 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
var stringLocalizer = new Mock<IStringLocalizer>();
stringLocalizer.Setup(s => s["Hello"]).Returns(localizedString);
var htmlLocalizer = new HtmlLocalizer(stringLocalizer.Object, new HtmlTestEncoder());
var htmlLocalizer = new HtmlLocalizer(stringLocalizer.Object);
var content = htmlLocalizer.Html("Hello", new object[] { });
// Act
var exception = Assert.Throws<FormatException>(() => htmlLocalizer.Html("Hello", new object[] { }));
var exception = Assert.Throws<FormatException>(
() => content.WriteTo(TextWriter.Null, new HtmlTestEncoder()));
// Assert
Assert.NotNull(exception);
@ -159,7 +167,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
// Arrange
var stringLocalizer = new TestStringLocalizer();
var htmlLocalizer = new HtmlLocalizer(stringLocalizer, new HtmlTestEncoder());
var htmlLocalizer = new HtmlLocalizer(stringLocalizer);
// Act
var actualLocalizedString = htmlLocalizer.GetString("John");
@ -174,7 +182,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
// Arrange
var stringLocalizer = new TestStringLocalizer();
var htmlLocalizer = new HtmlLocalizer(stringLocalizer, new HtmlTestEncoder());
var htmlLocalizer = new HtmlLocalizer(stringLocalizer);
// Act
var actualLocalizedString = htmlLocalizer.GetString("John", "Doe");
@ -189,7 +197,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
// Arrange
var stringLocalizer = new TestStringLocalizer();
var htmlLocalizer = new HtmlLocalizer(stringLocalizer, new HtmlTestEncoder());
var htmlLocalizer = new HtmlLocalizer(stringLocalizer);
// Act
var actualLocalizedString = htmlLocalizer.Html("John");
@ -204,7 +212,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
// Arrange
var stringLocalizer = new TestStringLocalizer();
var htmlLocalizer = new HtmlLocalizer(stringLocalizer, new HtmlTestEncoder());
var htmlLocalizer = new HtmlLocalizer(stringLocalizer);
// Act
var actualLocalizedString = htmlLocalizer.WithCulture(new CultureInfo("fr"))["John"];
@ -219,7 +227,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
// Arrange
var stringLocalizer = new TestStringLocalizer();
var htmlLocalizer = new HtmlLocalizer(stringLocalizer, new HtmlTestEncoder());
var htmlLocalizer = new HtmlLocalizer(stringLocalizer);
// Act
var allLocalizedStrings = htmlLocalizer.GetAllStrings(includeAncestorCultures: false).ToList();
@ -235,7 +243,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
// Arrange
var stringLocalizer = new TestStringLocalizer();
var htmlLocalizer = new HtmlLocalizer(stringLocalizer, new HtmlTestEncoder());
var htmlLocalizer = new HtmlLocalizer(stringLocalizer);
// Act
var allLocalizedStrings = htmlLocalizer.GetAllStrings().ToList();

View File

@ -5,12 +5,10 @@ using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.Encodings.Web;
using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.AspNet.Mvc.ViewEngines;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.PlatformAbstractions;
using Microsoft.Extensions.WebEncoders.Testing;
using Moq;
using Xunit;
@ -87,7 +85,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
{
// Arrange
var stringLocalizer = new TestStringLocalizer();
var htmlLocalizer = new HtmlLocalizer(stringLocalizer, new HtmlTestEncoder());
var htmlLocalizer = new HtmlLocalizer(stringLocalizer);
var applicationEnvironment = new Mock<IApplicationEnvironment>();
applicationEnvironment.Setup(a => a.ApplicationName).Returns("TestApplication");
var viewLocalizer = new ViewLocalizer(new TestHtmlLocalizerFactory(), applicationEnvironment.Object);
@ -112,7 +110,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
{
// Arrange
var stringLocalizer = new TestStringLocalizer();
var htmlLocalizer = new HtmlLocalizer(stringLocalizer, new HtmlTestEncoder());
var htmlLocalizer = new HtmlLocalizer(stringLocalizer);
var applicationEnvironment = new Mock<IApplicationEnvironment>();
applicationEnvironment.Setup(a => a.ApplicationName).Returns("TestApplication");
var viewLocalizer = new ViewLocalizer(new TestHtmlLocalizerFactory(), applicationEnvironment.Object);
@ -138,7 +136,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
{
// Arrange
var stringLocalizer = new TestStringLocalizer();
var htmlLocalizer = new HtmlLocalizer(stringLocalizer, new HtmlTestEncoder());
var htmlLocalizer = new HtmlLocalizer(stringLocalizer);
var applicationEnvironment = new Mock<IApplicationEnvironment>();
applicationEnvironment.Setup(a => a.ApplicationName).Returns("TestApplication");
var viewLocalizer = new ViewLocalizer(new TestHtmlLocalizerFactory(), applicationEnvironment.Object);
@ -162,7 +160,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
{
// Arrange
var stringLocalizer = new TestStringLocalizer();
var htmlLocalizer = new HtmlLocalizer(stringLocalizer, new HtmlTestEncoder());
var htmlLocalizer = new HtmlLocalizer(stringLocalizer);
var applicationEnvironment = new Mock<IApplicationEnvironment>();
applicationEnvironment.Setup(a => a.ApplicationName).Returns("TestApplication");
var viewLocalizer = new ViewLocalizer(new TestHtmlLocalizerFactory(), applicationEnvironment.Object);
@ -186,7 +184,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
{
// Arrange
var stringLocalizer = new TestStringLocalizer();
var htmlLocalizer = new HtmlLocalizer(stringLocalizer, new HtmlTestEncoder());
var htmlLocalizer = new HtmlLocalizer(stringLocalizer);
var applicationEnvironment = new Mock<IApplicationEnvironment>();
applicationEnvironment.Setup(a => a.ApplicationName).Returns("TestApplication");
var viewLocalizer = new ViewLocalizer(new TestHtmlLocalizerFactory(), applicationEnvironment.Object);
@ -210,7 +208,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
{
// Arrange
var stringLocalizer = new TestStringLocalizer();
var htmlLocalizer = new HtmlLocalizer(stringLocalizer, new HtmlTestEncoder());
var htmlLocalizer = new HtmlLocalizer(stringLocalizer);
var applicationEnvironment = new Mock<IApplicationEnvironment>();
applicationEnvironment.Setup(a => a.ApplicationName).Returns("TestApplication");
var viewLocalizer = new ViewLocalizer(new TestHtmlLocalizerFactory(), applicationEnvironment.Object);
@ -234,7 +232,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
{
// Arrange
var stringLocalizer = new TestStringLocalizer();
var htmlLocalizer = new HtmlLocalizer(stringLocalizer, new HtmlTestEncoder());
var htmlLocalizer = new HtmlLocalizer(stringLocalizer);
var applicationEnvironment = new Mock<IApplicationEnvironment>();
applicationEnvironment.Setup(a => a.ApplicationName).Returns("TestApplication");
var viewLocalizer = new ViewLocalizer(new TestHtmlLocalizerFactory(), applicationEnvironment.Object);
@ -257,7 +255,7 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
{
private IStringLocalizer _stringLocalizer { get; set; }
public TestHtmlLocalizer(IStringLocalizer stringLocalizer, HtmlEncoder encoder)
public TestHtmlLocalizer(IStringLocalizer stringLocalizer)
{
_stringLocalizer = stringLocalizer;
}
@ -285,25 +283,31 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
public IStringLocalizer WithCulture(CultureInfo culture)
{
return new TestStringLocalizer(culture);
return new TestHtmlLocalizer(new TestStringLocalizer(culture));
}
IHtmlLocalizer IHtmlLocalizer.WithCulture(CultureInfo culture)
{
return new TestHtmlLocalizer(new TestStringLocalizer(culture), new HtmlTestEncoder());
return new TestHtmlLocalizer(new TestStringLocalizer(culture));
}
public LocalizedHtmlString Html(string key)
{
var localiziedString = _stringLocalizer.GetString(key);
return new LocalizedHtmlString(localiziedString.Name, localiziedString.Value);
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);
return new LocalizedHtmlString(
localizedString.Name,
localizedString.Value,
isResourceNotFound: false,
arguments: arguments);
}
IEnumerable<LocalizedString> IStringLocalizer.GetAllStrings(bool includeAncestorCultures)
@ -316,12 +320,12 @@ namespace Microsoft.AspNet.Mvc.Localization.Test
{
public IHtmlLocalizer Create(Type resourceSource)
{
return new TestHtmlLocalizer(new TestStringLocalizer(), new HtmlTestEncoder());
return new TestHtmlLocalizer(new TestStringLocalizer());
}
public IHtmlLocalizer Create(string baseName, string location)
{
return new TestHtmlLocalizer(new TestStringLocalizer(), new HtmlTestEncoder());
return new TestHtmlLocalizer(new TestStringLocalizer());
}
}
}