Merge branch 'release' into dev

This commit is contained in:
damianedwards 2015-05-28 12:31:39 -07:00
commit 7fb51d4535
6 changed files with 63 additions and 52 deletions

View File

@ -9,7 +9,7 @@ namespace Microsoft.Framework.Localization
/// <summary>
/// Represents a service that provides localized strings.
/// </summary>
public interface IStringLocalizer : IEnumerable<LocalizedString>
public interface IStringLocalizer
{
/// <summary>
/// Gets the string resource with the given name.
@ -25,7 +25,17 @@ namespace Microsoft.Framework.Localization
/// <param name="arguments">The values to format the string with.</param>
/// <returns>The formatted string resource as a <see cref="LocalizedString"/>.</returns>
LocalizedString this[string name, params object[] arguments] { get; }
/// <summary>
/// Gets all string resources.
/// </summary>
/// <param name="includeAncestorCultures">
/// A <see cref="System.Boolean"/> indicating whether to include
/// strings from ancestor cultures.
/// </param>
/// <returns>The strings.</returns>
IEnumerable<LocalizedString> GetAllStrings(bool includeAncestorCultures);
/// <summary>
/// Creates a new <see cref="ResourceManagerStringLocalizer"/> for a specific <see cref="CultureInfo"/>.
/// </summary>

View File

@ -1,6 +1,7 @@
// 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.Collections.Generic;
using Microsoft.Framework.Internal;
namespace Microsoft.Framework.Localization
@ -10,22 +11,31 @@ namespace Microsoft.Framework.Localization
/// <summary>
/// Gets the string resource with the given name.
/// </summary>
/// <param name="stringLocalizer">The <see cref="IStringLocalizer"/>.</param>
/// <param name="name">The name of the string resource.</param>
/// <returns>The string resource as a <see cref="LocalizedString"/>.</returns>
public static LocalizedString GetString([NotNull] this IStringLocalizer stringLocalizer, [NotNull] string name)
{
return stringLocalizer[name];
}
public static LocalizedString GetString(
[NotNull] this IStringLocalizer stringLocalizer,
[NotNull] string name) => stringLocalizer[name];
/// <summary>
/// Gets the string resource with the given name and formatted with the supplied arguments.
/// </summary>
/// <param name="stringLocalizer">The <see cref="IStringLocalizer"/>.</param>
/// <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([NotNull] this IStringLocalizer stringLocalizer, [NotNull] string name, params object[] arguments)
{
return stringLocalizer[name, arguments];
}
public static LocalizedString GetString(
[NotNull] this IStringLocalizer stringLocalizer,
[NotNull] string name,
params object[] arguments) => stringLocalizer[name, arguments];
/// <summary>
/// Gets all string resources including those for ancestor cultures.
/// </summary>
/// <param name="stringLocalizer">The <see cref="IStringLocalizer"/>.</param>
/// <returns>The string resources.</returns>
public static IEnumerable<LocalizedString> GetAllStrings([NotNull] this IStringLocalizer stringLocalizer) =>
stringLocalizer.GetAllStrings(includeAncestorCultures: true);
}
}

View File

@ -1,7 +1,6 @@
// 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.Collections;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.Framework.Internal;
@ -36,15 +35,7 @@ namespace Microsoft.Framework.Localization
_localizer[name, arguments];
/// <inheritdoc />
public virtual LocalizedString GetString([NotNull] string name) => _localizer.GetString(name);
/// <inheritdoc />
public virtual LocalizedString GetString([NotNull] string name, params object[] arguments) =>
_localizer.GetString(name, arguments);
/// <inheritdoc />
public IEnumerator<LocalizedString> GetEnumerator() => _localizer.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => _localizer.GetEnumerator();
public IEnumerable<LocalizedString> GetAllStrings(bool includeAncestorCultures) =>
_localizer.GetAllStrings(includeAncestorCultures);
}
}

View File

@ -100,6 +100,29 @@ namespace Microsoft.Framework.Localization
culture);
}
/// <inheritdoc />
public virtual IEnumerable<LocalizedString> GetAllStrings(bool includeAncestorCultures) =>
GetAllStrings(includeAncestorCultures, CultureInfo.CurrentUICulture);
/// <summary>
/// Returns all strings in the specified culture.
/// </summary>
/// <param name="includeAncestorCultures"></param>
/// <param name="culture">The <see cref="CultureInfo"/> to get strings for.</param>
/// <returns>The strings.</returns>
protected IEnumerable<LocalizedString> GetAllStrings(bool includeAncestorCultures, [NotNull] CultureInfo culture)
{
var resourceNames = includeAncestorCultures
? GetResourceNamesFromCultureHierarchy(culture)
: GetResourceNamesForCulture(culture);
foreach (var name in resourceNames)
{
var value = GetStringSafely(name, culture);
yield return new LocalizedString(name, value ?? name, resourceNotFound: value == null);
}
}
/// <summary>
/// Gets a resource string from the <see cref="_resourceManager"/> and returns <c>null</c> instead of
/// throwing exceptions if a match isn't found.
@ -127,30 +150,6 @@ namespace Microsoft.Framework.Localization
}
}
/// <summary>
/// Returns an <see cref="IEnumerator{LocalizedString}"/> for all strings in the current culture.
/// </summary>
/// <returns>The <see cref="IEnumerator{LocalizedString}"/>.</returns>
public virtual IEnumerator<LocalizedString> GetEnumerator() => GetEnumerator(CultureInfo.CurrentUICulture);
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
/// <summary>
/// Returns an <see cref="IEnumerator{LocalizedString}"/> for all strings in the specified culture.
/// </summary>
/// <param name="culture">The <see cref="CultureInfo"/> to get strings for.</param>
/// <returns>The <see cref="IEnumerator{LocalizedString}"/>.</returns>
protected IEnumerator<LocalizedString> GetEnumerator([NotNull] CultureInfo culture)
{
var resourceNames = GetResourceNamesFromCultureHierarchy(culture);
foreach (var name in resourceNames)
{
var value = GetStringSafely(name, culture);
yield return new LocalizedString(name, value ?? name, resourceNotFound: value == null);
}
}
private IEnumerable<string> GetResourceNamesFromCultureHierarchy(CultureInfo startingCulture)
{
var currentCulture = startingCulture;
@ -191,10 +190,10 @@ namespace Microsoft.Framework.Localization
var cacheKey = $"assembly={_resourceAssemblyWrapper.FullName};resourceStreamName={resourceStreamName}";
var cultureResourceNames = _resourceNamesCache.GetOrAdd(cacheKey, key =>
var cultureResourceNames = _resourceNamesCache.GetOrAdd(cacheKey, _ =>
{
var names = new List<string>();
using (var cultureResourceStream = _resourceAssemblyWrapper.GetManifestResourceStream(key))
using (var cultureResourceStream = _resourceAssemblyWrapper.GetManifestResourceStream(resourceStreamName))
using (var resources = new ResourceReader(cultureResourceStream))
{
foreach (DictionaryEntry entry in resources)

View File

@ -58,6 +58,7 @@ namespace Microsoft.Framework.Localization
}
/// <inheritdoc />
public override IEnumerator<LocalizedString> GetEnumerator() => GetEnumerator(_culture);
public override IEnumerable<LocalizedString> GetAllStrings(bool includeAncestorCultures) =>
GetAllStrings(includeAncestorCultures, _culture);
}
}

View File

@ -27,8 +27,8 @@ namespace Microsoft.Framework.Localization.Test
// Act
for (int i = 0; i < 5; i++)
{
localizer1.ToList();
localizer2.ToList();
localizer1.GetAllStrings().ToList();
localizer2.GetAllStrings().ToList();
}
// Assert
@ -50,8 +50,8 @@ namespace Microsoft.Framework.Localization.Test
var localizer2 = new ResourceManagerStringLocalizer(resourceManager2, resourceAssembly2, baseName, resourceNamesCache);
// Act
localizer1.ToList();
localizer2.ToList();
localizer1.GetAllStrings().ToList();
localizer2.GetAllStrings().ToList();
// Assert
var expectedCallCount = GetCultureInfoDepth(CultureInfo.CurrentUICulture);