From 5b96ddfce49fd473527b0af4de06f9986880709e Mon Sep 17 00:00:00 2001 From: Jukka Snellman Date: Tue, 16 Aug 2022 16:50:28 +0300 Subject: [PATCH] added strongly typed API --- .../StringLocalizerWithCultureTests.cs | 7 +++++++ .../StringLocalizerWithCultureFactory.cs | 21 ++++++++++--------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/StringLocalizerWithCulture.Tests/StringLocalizerWithCultureTests.cs b/StringLocalizerWithCulture.Tests/StringLocalizerWithCultureTests.cs index 985ae6e..848fd34 100644 --- a/StringLocalizerWithCulture.Tests/StringLocalizerWithCultureTests.cs +++ b/StringLocalizerWithCulture.Tests/StringLocalizerWithCultureTests.cs @@ -64,6 +64,13 @@ public void GetString_Missing() Translate("Missing", CultureInfo.GetCultureInfo("en-US")).ResourceNotFound.Should().BeTrue(); } + [Fact] + public void GetString_Typed() + { + var localizer = _factory.Create(CultureInfo.InvariantCulture); + localizer["Hello"].Value.Should().Be("Hello World"); + } + } class MyClass diff --git a/StringLocalizerWithCulture/StringLocalizerWithCultureFactory.cs b/StringLocalizerWithCulture/StringLocalizerWithCultureFactory.cs index b5c26f3..c0b61cb 100644 --- a/StringLocalizerWithCulture/StringLocalizerWithCultureFactory.cs +++ b/StringLocalizerWithCulture/StringLocalizerWithCultureFactory.cs @@ -12,6 +12,7 @@ namespace StringLocalizerWithCulture public interface IStringLocalizerWithCultureFactory { IStringLocalizer Create(Type resourceSource, CultureInfo culture); + IStringLocalizer Create(CultureInfo culture); } // Adapted from https://github.com/dotnet/aspnetcore/issues/7756 @@ -32,27 +33,27 @@ public StringLocalizerWithCultureFactory( private readonly ConcurrentDictionary _localizerCache = new ConcurrentDictionary(); - public IStringLocalizer Create(Type resourceSource, CultureInfo culture) - { - if (resourceSource == null) - { - throw new ArgumentNullException(nameof(resourceSource)); - } + public IStringLocalizer Create(Type resourceSource, CultureInfo culture) + => CreateResourceManagerStringLocalizer(resourceSource, culture); + + public IStringLocalizer Create(CultureInfo culture) => CreateResourceManagerStringLocalizer(typeof(T), culture); + private StringLocalizerWithCulture CreateResourceManagerStringLocalizer(Type resourceSource, + CultureInfo culture) + { var typeInfo = resourceSource.GetTypeInfo(); var baseName = GetResourcePrefix(typeInfo); var assembly = typeInfo.Assembly; - - return _localizerCache.GetOrAdd(baseName, _ => CreateResourceManagerStringLocalizer(assembly, baseName, culture)); + return (StringLocalizerWithCulture)_localizerCache.GetOrAdd(baseName, _ => CreateResourceManagerStringLocalizer(assembly, baseName, culture)); } - private StringLocalizerWithCulture CreateResourceManagerStringLocalizer( + private StringLocalizerWithCulture CreateResourceManagerStringLocalizer( Assembly assembly, string baseName, CultureInfo culture) { var resources = new ResourceManager(baseName, assembly); - return new StringLocalizerWithCulture(resources, assembly, baseName, _resourceNamesCache, + return new StringLocalizerWithCulture(resources, assembly, baseName, _resourceNamesCache, _loggerFactory.CreateLogger(), culture); }