Moved methods off of IStringLocalizer and into extension methods

This commit is contained in:
damianedwards 2015-05-15 12:37:02 -07:00
parent ecfb7f342a
commit 2ba7049648
4 changed files with 56 additions and 42 deletions

View File

@ -26,21 +26,6 @@ namespace Microsoft.Framework.Localization
/// <returns>The formatted string resource as a <see cref="LocalizedString"/>.</returns> /// <returns>The formatted string resource as a <see cref="LocalizedString"/>.</returns>
LocalizedString this[string name, params object[] arguments] { get; } LocalizedString this[string name, params object[] arguments] { get; }
/// <summary>
/// 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="LocalizedString"/>.</returns>
LocalizedString GetString(string name);
/// <summary>
/// Gets the string resource with the given name and formatted with the supplied arguments.
/// </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="LocalizedString"/>.</returns>
LocalizedString GetString(string name, params object[] arguments);
/// <summary> /// <summary>
/// Creates a new <see cref="ResourceManagerStringLocalizer"/> for a specific <see cref="CultureInfo"/>. /// Creates a new <see cref="ResourceManagerStringLocalizer"/> for a specific <see cref="CultureInfo"/>.
/// </summary> /// </summary>

View File

@ -0,0 +1,29 @@
// 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.
namespace Microsoft.Framework.Localization
{
public static class IStringLocalizerExtensions
{
/// <summary>
/// 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="LocalizedString"/>.</returns>
public static LocalizedString GetString(this IStringLocalizer stringLocalizer, string name)
{
return stringLocalizer[name];
}
/// <summary>
/// Gets the string resource with the given name and formatted with the supplied arguments.
/// </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="LocalizedString"/>.</returns>
public static LocalizedString GetString(this IStringLocalizer stringLocalizer, string name, params object[] arguments)
{
return stringLocalizer[name, arguments];
}
}
}

View File

@ -53,25 +53,25 @@ namespace Microsoft.Framework.Localization
protected string ResourceBaseName { get; } protected string ResourceBaseName { get; }
/// <inheritdoc /> /// <inheritdoc />
public virtual LocalizedString this[[NotNull] string name] => GetString(name); public virtual LocalizedString this[[NotNull] string name]
{
/// <inheritdoc /> get
public virtual LocalizedString this[[NotNull] string name, params object[] arguments] => GetString(name, arguments);
/// <inheritdoc />
public virtual LocalizedString GetString([NotNull] string name)
{ {
var value = GetStringSafely(name, null); var value = GetStringSafely(name, null);
return new LocalizedString(name, value ?? name, resourceNotFound: value == null); return new LocalizedString(name, value ?? name, resourceNotFound: value == null);
} }
}
/// <inheritdoc /> /// <inheritdoc />
public virtual LocalizedString GetString([NotNull] string name, params object[] arguments) public virtual LocalizedString this[[NotNull] string name, params object[] arguments]
{
get
{ {
var format = GetStringSafely(name, null); var format = GetStringSafely(name, null);
var value = string.Format(format ?? name, arguments); var value = string.Format(format ?? name, arguments);
return new LocalizedString(name, value, resourceNotFound: format == null); return new LocalizedString(name, value, resourceNotFound: format == null);
} }
}
/// <summary> /// <summary>
/// Creates a new <see cref="ResourceManagerStringLocalizer"/> for a specific <see cref="CultureInfo"/>. /// Creates a new <see cref="ResourceManagerStringLocalizer"/> for a specific <see cref="CultureInfo"/>.

View File

@ -36,25 +36,25 @@ namespace Microsoft.Framework.Localization
} }
/// <inheritdoc /> /// <inheritdoc />
public override LocalizedString this[[NotNull] string name] => GetString(name); public override LocalizedString this[[NotNull] string name]
{
/// <inheritdoc /> get
public override LocalizedString this[[NotNull] string name, params object[] arguments] => GetString(name, arguments);
/// <inheritdoc />
public override LocalizedString GetString([NotNull] string name)
{ {
var value = GetStringSafely(name, _culture); var value = GetStringSafely(name, _culture);
return new LocalizedString(name, value ?? name); return new LocalizedString(name, value ?? name);
} }
}
/// <inheritdoc /> /// <inheritdoc />
public override LocalizedString GetString([NotNull] string name, params object[] arguments) public override LocalizedString this[[NotNull] string name, params object[] arguments]
{
get
{ {
var format = GetStringSafely(name, _culture); var format = GetStringSafely(name, _culture);
var value = string.Format(_culture, format ?? name, arguments); var value = string.Format(_culture, format ?? name, arguments);
return new LocalizedString(name, value ?? name, resourceNotFound: format == null); return new LocalizedString(name, value ?? name, resourceNotFound: format == null);
} }
}
/// <inheritdoc /> /// <inheritdoc />
public override IEnumerator<LocalizedString> GetEnumerator() public override IEnumerator<LocalizedString> GetEnumerator()