Merge branch 'release' into dev
This commit is contained in:
commit
7fb51d4535
|
|
@ -9,7 +9,7 @@ namespace Microsoft.Framework.Localization
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Represents a service that provides localized strings.
|
/// Represents a service that provides localized strings.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IStringLocalizer : IEnumerable<LocalizedString>
|
public interface IStringLocalizer
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the string resource with the given name.
|
/// Gets the string resource with the given name.
|
||||||
|
|
@ -26,6 +26,16 @@ 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 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>
|
/// <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>
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
// 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.
|
// 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;
|
using Microsoft.Framework.Internal;
|
||||||
|
|
||||||
namespace Microsoft.Framework.Localization
|
namespace Microsoft.Framework.Localization
|
||||||
|
|
@ -10,22 +11,31 @@ namespace Microsoft.Framework.Localization
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the string resource with the given name.
|
/// Gets the string resource with the given name.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="stringLocalizer">The <see cref="IStringLocalizer"/>.</param>
|
||||||
/// <param name="name">The name of the string resource.</param>
|
/// <param name="name">The name of the string resource.</param>
|
||||||
/// <returns>The string resource as a <see cref="LocalizedString"/>.</returns>
|
/// <returns>The string resource as a <see cref="LocalizedString"/>.</returns>
|
||||||
public static LocalizedString GetString([NotNull] this IStringLocalizer stringLocalizer, [NotNull] string name)
|
public static LocalizedString GetString(
|
||||||
{
|
[NotNull] this IStringLocalizer stringLocalizer,
|
||||||
return stringLocalizer[name];
|
[NotNull] string name) => stringLocalizer[name];
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the string resource with the given name and formatted with the supplied arguments.
|
/// Gets the string resource with the given name and formatted with the supplied arguments.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="stringLocalizer">The <see cref="IStringLocalizer"/>.</param>
|
||||||
/// <param name="name">The name of the string resource.</param>
|
/// <param name="name">The name of the string resource.</param>
|
||||||
/// <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>
|
||||||
public static LocalizedString GetString([NotNull] this IStringLocalizer stringLocalizer, [NotNull] string name, params object[] arguments)
|
public static LocalizedString GetString(
|
||||||
{
|
[NotNull] this IStringLocalizer stringLocalizer,
|
||||||
return stringLocalizer[name, arguments];
|
[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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
// 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.
|
// 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.Collections.Generic;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using Microsoft.Framework.Internal;
|
using Microsoft.Framework.Internal;
|
||||||
|
|
@ -36,15 +35,7 @@ namespace Microsoft.Framework.Localization
|
||||||
_localizer[name, arguments];
|
_localizer[name, arguments];
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public virtual LocalizedString GetString([NotNull] string name) => _localizer.GetString(name);
|
public IEnumerable<LocalizedString> GetAllStrings(bool includeAncestorCultures) =>
|
||||||
|
_localizer.GetAllStrings(includeAncestorCultures);
|
||||||
/// <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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -100,6 +100,29 @@ namespace Microsoft.Framework.Localization
|
||||||
culture);
|
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>
|
/// <summary>
|
||||||
/// Gets a resource string from the <see cref="_resourceManager"/> and returns <c>null</c> instead of
|
/// Gets a resource string from the <see cref="_resourceManager"/> and returns <c>null</c> instead of
|
||||||
/// throwing exceptions if a match isn't found.
|
/// 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)
|
private IEnumerable<string> GetResourceNamesFromCultureHierarchy(CultureInfo startingCulture)
|
||||||
{
|
{
|
||||||
var currentCulture = startingCulture;
|
var currentCulture = startingCulture;
|
||||||
|
|
@ -191,10 +190,10 @@ namespace Microsoft.Framework.Localization
|
||||||
|
|
||||||
var cacheKey = $"assembly={_resourceAssemblyWrapper.FullName};resourceStreamName={resourceStreamName}";
|
var cacheKey = $"assembly={_resourceAssemblyWrapper.FullName};resourceStreamName={resourceStreamName}";
|
||||||
|
|
||||||
var cultureResourceNames = _resourceNamesCache.GetOrAdd(cacheKey, key =>
|
var cultureResourceNames = _resourceNamesCache.GetOrAdd(cacheKey, _ =>
|
||||||
{
|
{
|
||||||
var names = new List<string>();
|
var names = new List<string>();
|
||||||
using (var cultureResourceStream = _resourceAssemblyWrapper.GetManifestResourceStream(key))
|
using (var cultureResourceStream = _resourceAssemblyWrapper.GetManifestResourceStream(resourceStreamName))
|
||||||
using (var resources = new ResourceReader(cultureResourceStream))
|
using (var resources = new ResourceReader(cultureResourceStream))
|
||||||
{
|
{
|
||||||
foreach (DictionaryEntry entry in resources)
|
foreach (DictionaryEntry entry in resources)
|
||||||
|
|
|
||||||
|
|
@ -58,6 +58,7 @@ namespace Microsoft.Framework.Localization
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override IEnumerator<LocalizedString> GetEnumerator() => GetEnumerator(_culture);
|
public override IEnumerable<LocalizedString> GetAllStrings(bool includeAncestorCultures) =>
|
||||||
|
GetAllStrings(includeAncestorCultures, _culture);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -27,8 +27,8 @@ namespace Microsoft.Framework.Localization.Test
|
||||||
// Act
|
// Act
|
||||||
for (int i = 0; i < 5; i++)
|
for (int i = 0; i < 5; i++)
|
||||||
{
|
{
|
||||||
localizer1.ToList();
|
localizer1.GetAllStrings().ToList();
|
||||||
localizer2.ToList();
|
localizer2.GetAllStrings().ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
|
|
@ -50,8 +50,8 @@ namespace Microsoft.Framework.Localization.Test
|
||||||
var localizer2 = new ResourceManagerStringLocalizer(resourceManager2, resourceAssembly2, baseName, resourceNamesCache);
|
var localizer2 = new ResourceManagerStringLocalizer(resourceManager2, resourceAssembly2, baseName, resourceNamesCache);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
localizer1.ToList();
|
localizer1.GetAllStrings().ToList();
|
||||||
localizer2.ToList();
|
localizer2.GetAllStrings().ToList();
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
var expectedCallCount = GetCultureInfoDepth(CultureInfo.CurrentUICulture);
|
var expectedCallCount = GetCultureInfoDepth(CultureInfo.CurrentUICulture);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue