diff --git a/src/Microsoft.AspNet.Mvc.Localization/HtmlLocalizer.cs b/src/Microsoft.AspNet.Mvc.Localization/HtmlLocalizer.cs
index 1cbf91ed29..ceaa04de02 100644
--- a/src/Microsoft.AspNet.Mvc.Localization/HtmlLocalizer.cs
+++ b/src/Microsoft.AspNet.Mvc.Localization/HtmlLocalizer.cs
@@ -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;
///
/// Creates a new .
///
/// The to read strings from.
- /// The .
- 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;
}
///
@@ -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));
}
///
@@ -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));
}
///
@@ -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);
}
///
@@ -155,118 +143,7 @@ namespace Microsoft.AspNet.Mvc.Localization
protected virtual LocalizedHtmlString ToHtmlString(LocalizedString result) =>
new LocalizedHtmlString(result.Name, result.Value, result.ResourceNotFound);
- ///
- /// Encodes the arguments based on the object type.
- ///
- /// The resourceString whose arguments need to be encoded.
- /// The array of objects to encode.
- /// The string with encoded arguments.
- 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);
}
}
\ No newline at end of file
diff --git a/src/Microsoft.AspNet.Mvc.Localization/HtmlLocalizerFactory.cs b/src/Microsoft.AspNet.Mvc.Localization/HtmlLocalizerFactory.cs
index 81e8d8f74a..8ad004bb67 100644
--- a/src/Microsoft.AspNet.Mvc.Localization/HtmlLocalizerFactory.cs
+++ b/src/Microsoft.AspNet.Mvc.Localization/HtmlLocalizerFactory.cs
@@ -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;
///
/// Creates a new .
///
/// The .
- /// The .
- 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;
}
///
@@ -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));
}
///
@@ -71,7 +62,7 @@ namespace Microsoft.AspNet.Mvc.Localization
}
var localizer = _factory.Create(baseName, location);
- return new HtmlLocalizer(localizer, _encoder);
+ return new HtmlLocalizer(localizer);
}
}
}
\ No newline at end of file
diff --git a/src/Microsoft.AspNet.Mvc.Localization/HtmlLocalizerOfT.cs b/src/Microsoft.AspNet.Mvc.Localization/HtmlLocalizerOfT.cs
index 4342496503..4247dd8121 100644
--- a/src/Microsoft.AspNet.Mvc.Localization/HtmlLocalizerOfT.cs
+++ b/src/Microsoft.AspNet.Mvc.Localization/HtmlLocalizerOfT.cs
@@ -9,9 +9,9 @@ using Microsoft.Extensions.Localization;
namespace Microsoft.AspNet.Mvc.Localization
{
///
- /// This is an that provides localized HTML content.
+ /// This is an implementation that provides localized HTML content.
///
- /// The to scope the resource names.
+ /// The to scope the resource names.
public class HtmlLocalizer : IHtmlLocalizer
{
private readonly IHtmlLocalizer _localizer;
diff --git a/src/Microsoft.AspNet.Mvc.Localization/IHtmlLocalizerOfT.cs b/src/Microsoft.AspNet.Mvc.Localization/IHtmlLocalizerOfT.cs
index 4df8e73fe5..62323d8142 100644
--- a/src/Microsoft.AspNet.Mvc.Localization/IHtmlLocalizerOfT.cs
+++ b/src/Microsoft.AspNet.Mvc.Localization/IHtmlLocalizerOfT.cs
@@ -6,7 +6,7 @@ namespace Microsoft.AspNet.Mvc.Localization
///
/// An that provides localized HTML content.
///
- /// The to scope the resource names.
+ /// The to scope the resource names.
public interface IHtmlLocalizer : IHtmlLocalizer
{
}
diff --git a/src/Microsoft.AspNet.Mvc.Localization/LocalizedHtmlString.cs b/src/Microsoft.AspNet.Mvc.Localization/LocalizedHtmlString.cs
index d6842a83d0..f2be394544 100644
--- a/src/Microsoft.AspNet.Mvc.Localization/LocalizedHtmlString.cs
+++ b/src/Microsoft.AspNet.Mvc.Localization/LocalizedHtmlString.cs
@@ -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
{
///
- /// An with localized content.
+ /// An with localized content.
///
- public class LocalizedHtmlString : HtmlString
+ public class LocalizedHtmlString : IHtmlContent
{
+#if DOTNET5_5
+ private static readonly object[] EmptyArguments = Array.Empty