diff --git a/src/Microsoft.Framework.Localization.Abstractions/IStringLocalizer.cs b/src/Microsoft.Framework.Localization.Abstractions/IStringLocalizer.cs index aad6b9bb3e..4a1613504e 100644 --- a/src/Microsoft.Framework.Localization.Abstractions/IStringLocalizer.cs +++ b/src/Microsoft.Framework.Localization.Abstractions/IStringLocalizer.cs @@ -9,7 +9,7 @@ namespace Microsoft.Framework.Localization /// /// Represents a service that provides localized strings. /// - public interface IStringLocalizer : IEnumerable + public interface IStringLocalizer { /// /// Gets the string resource with the given name. @@ -25,7 +25,17 @@ namespace Microsoft.Framework.Localization /// The values to format the string with. /// The formatted string resource as a . LocalizedString this[string name, params object[] arguments] { get; } - + + /// + /// Gets all string resources. + /// + /// + /// A indicating whether to include + /// strings from ancestor cultures. + /// + /// The strings. + IEnumerable GetAllStrings(bool includeAncestorCultures); + /// /// Creates a new for a specific . /// diff --git a/src/Microsoft.Framework.Localization.Abstractions/StringLocalizerExtensions.cs b/src/Microsoft.Framework.Localization.Abstractions/StringLocalizerExtensions.cs index c3cd604ad6..0bd30c124e 100644 --- a/src/Microsoft.Framework.Localization.Abstractions/StringLocalizerExtensions.cs +++ b/src/Microsoft.Framework.Localization.Abstractions/StringLocalizerExtensions.cs @@ -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 /// /// Gets the string resource with the given name. /// + /// The . /// The name of the string resource. /// The string resource as a . - 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]; /// /// Gets the string resource with the given name and formatted with the supplied arguments. /// + /// The . /// The name of the string resource. /// The values to format the string with. /// The formatted string resource as a . - 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]; + + /// + /// Gets all string resources including those for ancestor cultures. + /// + /// The . + /// The string resources. + public static IEnumerable GetAllStrings([NotNull] this IStringLocalizer stringLocalizer) => + stringLocalizer.GetAllStrings(includeAncestorCultures: true); } } diff --git a/src/Microsoft.Framework.Localization.Abstractions/StringLocalizerOfT.cs b/src/Microsoft.Framework.Localization.Abstractions/StringLocalizerOfT.cs index 2a5334cce0..d8621cf516 100644 --- a/src/Microsoft.Framework.Localization.Abstractions/StringLocalizerOfT.cs +++ b/src/Microsoft.Framework.Localization.Abstractions/StringLocalizerOfT.cs @@ -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]; /// - public virtual LocalizedString GetString([NotNull] string name) => _localizer.GetString(name); - - /// - public virtual LocalizedString GetString([NotNull] string name, params object[] arguments) => - _localizer.GetString(name, arguments); - - /// - public IEnumerator GetEnumerator() => _localizer.GetEnumerator(); - - IEnumerator IEnumerable.GetEnumerator() => _localizer.GetEnumerator(); + public IEnumerable GetAllStrings(bool includeAncestorCultures) => + _localizer.GetAllStrings(includeAncestorCultures); } } \ No newline at end of file diff --git a/src/Microsoft.Framework.Localization/ResourceManagerStringLocalizer.cs b/src/Microsoft.Framework.Localization/ResourceManagerStringLocalizer.cs index ee62f3db03..b970d5a4d4 100644 --- a/src/Microsoft.Framework.Localization/ResourceManagerStringLocalizer.cs +++ b/src/Microsoft.Framework.Localization/ResourceManagerStringLocalizer.cs @@ -100,6 +100,29 @@ namespace Microsoft.Framework.Localization culture); } + /// + public virtual IEnumerable GetAllStrings(bool includeAncestorCultures) => + GetAllStrings(includeAncestorCultures, CultureInfo.CurrentUICulture); + + /// + /// Returns all strings in the specified culture. + /// + /// + /// The to get strings for. + /// The strings. + protected IEnumerable 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); + } + } + /// /// Gets a resource string from the and returns null instead of /// throwing exceptions if a match isn't found. @@ -127,30 +150,6 @@ namespace Microsoft.Framework.Localization } } - /// - /// Returns an for all strings in the current culture. - /// - /// The . - public virtual IEnumerator GetEnumerator() => GetEnumerator(CultureInfo.CurrentUICulture); - - IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); - - /// - /// Returns an for all strings in the specified culture. - /// - /// The to get strings for. - /// The . - protected IEnumerator 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 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(); - using (var cultureResourceStream = _resourceAssemblyWrapper.GetManifestResourceStream(key)) + using (var cultureResourceStream = _resourceAssemblyWrapper.GetManifestResourceStream(resourceStreamName)) using (var resources = new ResourceReader(cultureResourceStream)) { foreach (DictionaryEntry entry in resources) diff --git a/src/Microsoft.Framework.Localization/ResourceManagerWithCultureStringLocalizer.cs b/src/Microsoft.Framework.Localization/ResourceManagerWithCultureStringLocalizer.cs index bc79cf838b..03f0135b54 100644 --- a/src/Microsoft.Framework.Localization/ResourceManagerWithCultureStringLocalizer.cs +++ b/src/Microsoft.Framework.Localization/ResourceManagerWithCultureStringLocalizer.cs @@ -58,6 +58,7 @@ namespace Microsoft.Framework.Localization } /// - public override IEnumerator GetEnumerator() => GetEnumerator(_culture); + public override IEnumerable GetAllStrings(bool includeAncestorCultures) => + GetAllStrings(includeAncestorCultures, _culture); } } \ No newline at end of file diff --git a/test/Microsoft.Framework.Localization.Test/ResourceManagerStringLocalizerTest.cs b/test/Microsoft.Framework.Localization.Test/ResourceManagerStringLocalizerTest.cs index 92191c0bf8..0bb208bdf8 100644 --- a/test/Microsoft.Framework.Localization.Test/ResourceManagerStringLocalizerTest.cs +++ b/test/Microsoft.Framework.Localization.Test/ResourceManagerStringLocalizerTest.cs @@ -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);