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

@ -25,22 +25,7 @@ namespace Microsoft.Framework.Localization
/// <param name="arguments">The values to format the string with.</param> /// <param name="arguments">The values to format the string with.</param>
/// <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

@ -51,26 +51,26 @@ namespace Microsoft.Framework.Localization
/// The base name of the embedded resource in the <see cref="Assembly"/> that contains the strings. /// The base name of the embedded resource in the <see cref="Assembly"/> that contains the strings.
/// </summary> /// </summary>
protected string ResourceBaseName { get; } protected string ResourceBaseName { get; }
/// <inheritdoc />
public virtual LocalizedString this[[NotNull] string name] => GetString(name);
/// <inheritdoc /> /// <inheritdoc />
public virtual LocalizedString this[[NotNull] string name, params object[] arguments] => GetString(name, arguments); public virtual LocalizedString this[[NotNull] string name]
/// <inheritdoc />
public virtual LocalizedString GetString([NotNull] string name)
{ {
var value = GetStringSafely(name, null); get
return new LocalizedString(name, value ?? name, resourceNotFound: value == null); {
var value = GetStringSafely(name, 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]
{ {
var format = GetStringSafely(name, null); get
var value = string.Format(format ?? name, arguments); {
return new LocalizedString(name, value, resourceNotFound: format == null); var format = GetStringSafely(name, null);
var value = string.Format(format ?? name, arguments);
return new LocalizedString(name, value, resourceNotFound: format == null);
}
} }
/// <summary> /// <summary>

View File

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