// 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.IO; using System.Text.Encodings.Web; using Microsoft.AspNetCore.Html; namespace Microsoft.AspNetCore.Mvc.Localization { /// /// An with localized content. /// public class LocalizedHtmlString : IHtmlContent { private readonly object[] _arguments; /// /// Creates an instance of . /// /// The name of the string resource. /// The string resource. public LocalizedHtmlString(string name, string value) : this(name, value, isResourceNotFound: false, arguments: Array.Empty()) { } /// /// Creates an instance of . /// /// The name of the string resource. /// The string resource. /// A flag that indicates if the resource is not found. public LocalizedHtmlString(string name, string value, bool isResourceNotFound) : this(name, value, isResourceNotFound, arguments: Array.Empty()) { } /// /// Creates an instance of . /// /// The name of the string resource. /// The string resource. /// A flag that indicates if the resource is not found. /// The values to format the with. public LocalizedHtmlString(string name, string value, bool isResourceNotFound, params object[] arguments) { if (name == null) { throw new ArgumentNullException(nameof(name)); } if (value == null) { throw new ArgumentNullException(nameof(value)); } if (arguments == null) { throw new ArgumentNullException(nameof(arguments)); } Name = name; Value = value; IsResourceNotFound = isResourceNotFound; _arguments = arguments; } /// /// The name of the string resource. /// public string Name { get; } /// /// The original resource string, prior to formatting with any constructor arguments. /// public string Value { get; } /// /// Gets a flag that indicates if the resource is not found. /// public bool IsResourceNotFound { get; } /// public void WriteTo(TextWriter writer, HtmlEncoder encoder) { if (writer == null) { throw new ArgumentNullException(nameof(writer)); } if (encoder == null) { throw new ArgumentNullException(nameof(encoder)); } var formattableString = new HtmlFormattableString(Value, _arguments); formattableString.WriteTo(writer, encoder); } } }