// 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 System.Globalization;
using Microsoft.Extensions.Localization;
namespace Microsoft.AspNet.Mvc.Localization
{
///
/// An that uses the provided to do HTML-aware
/// localization of content.
///
public class HtmlLocalizer : IHtmlLocalizer
{
private readonly IStringLocalizer _localizer;
///
/// Creates a new .
///
/// The to read strings from.
public HtmlLocalizer(IStringLocalizer localizer)
{
if (localizer == null)
{
throw new ArgumentNullException(nameof(localizer));
}
_localizer = localizer;
}
///
public virtual LocalizedHtmlString this[string name]
{
get
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
return ToHtmlString(_localizer[name]);
}
}
///
public virtual LocalizedHtmlString this[string name, params object[] arguments]
{
get
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
return ToHtmlString(_localizer[name], arguments);
}
}
///
public virtual LocalizedString GetString(string name)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
return _localizer[name];
}
///
public virtual LocalizedString GetString(string name, params object[] arguments)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
return _localizer[name, arguments];
}
///
public virtual IEnumerable GetAllStrings(bool includeParentCultures) =>
_localizer.GetAllStrings(includeParentCultures);
///
public virtual IHtmlLocalizer WithCulture(CultureInfo culture)
{
if (culture == null)
{
throw new ArgumentNullException(nameof(culture));
}
return new HtmlLocalizer(_localizer.WithCulture(culture));
}
///
/// Creates a new for a .
///
/// The .
protected virtual LocalizedHtmlString ToHtmlString(LocalizedString result) =>
new LocalizedHtmlString(result.Name, result.Value, result.ResourceNotFound);
protected virtual LocalizedHtmlString ToHtmlString(LocalizedString result, object[] arguments) =>
new LocalizedHtmlString(result.Name, result.Value, result.ResourceNotFound, arguments);
}
}