-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jukka Snellman
committed
Aug 12, 2022
1 parent
af8e794
commit 8fbad11
Showing
4 changed files
with
87 additions
and
5 deletions.
There are no files selected for viewing
53 changes: 49 additions & 4 deletions
53
StringLocalizerWithCulture/IStringLocalizerWithCultureFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,64 @@ | ||
using Microsoft.Extensions.Localization; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Globalization; | ||
using System.Reflection; | ||
using System.Resources; | ||
|
||
namespace StringLocalizerWithCulture | ||
{ | ||
public interface IStringLocalizerWithCultureFactory | ||
{ | ||
IStringLocalizer Create(Type type, CultureInfo culture); | ||
IStringLocalizer Create(Type resourceSource, CultureInfo culture); | ||
} | ||
|
||
public class StringLocalizerWithCultureFactory : IStringLocalizerWithCultureFactory | ||
// Adapted from https://github.com/dotnet/aspnetcore/issues/7756 | ||
// and https://gist.github.com/vaclavholusa-LTD/2a27d0bb0af5c07589cffbf1c2fff4f4 | ||
internal sealed class StringLocalizerWithCultureFactory : ResourceManagerStringLocalizerFactory, IStringLocalizerWithCultureFactory | ||
{ | ||
public IStringLocalizer Create(Type type, CultureInfo culture) | ||
|
||
public StringLocalizerWithCultureFactory( | ||
IOptions<LocalizationOptions> localizationOptions, | ||
ILoggerFactory loggerFactory) | ||
: base(localizationOptions, loggerFactory) | ||
{ | ||
_loggerFactory = loggerFactory; | ||
} | ||
|
||
private readonly IResourceNamesCache _resourceNamesCache = new ResourceNamesCache(); | ||
private readonly ILoggerFactory _loggerFactory; | ||
private readonly ConcurrentDictionary<string, StringLocalizerWithCulture> _localizerCache | ||
= new ConcurrentDictionary<string, StringLocalizerWithCulture>(); | ||
|
||
public IStringLocalizer Create(Type resourceSource, CultureInfo culture) | ||
{ | ||
if (resourceSource == null) | ||
{ | ||
throw new ArgumentNullException(nameof(resourceSource)); | ||
} | ||
|
||
var typeInfo = resourceSource.GetTypeInfo(); | ||
var baseName = GetResourcePrefix(typeInfo); | ||
var assembly = typeInfo.Assembly; | ||
|
||
return _localizerCache.GetOrAdd(baseName, _ => CreateResourceManagerStringLocalizer(assembly, baseName, culture)); | ||
} | ||
|
||
private StringLocalizerWithCulture CreateResourceManagerStringLocalizer( | ||
Assembly assembly, | ||
string baseName, | ||
CultureInfo culture) | ||
{ | ||
throw new NotImplementedException(); | ||
return new StringLocalizerWithCulture( | ||
new ResourceManager(baseName, assembly), | ||
culture | ||
//assembly, | ||
//baseName, | ||
//_resourceNamesCache, | ||
//_loggerFactory.CreateLogger<ResourceManagerStringLocalizer>() | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using Microsoft.Extensions.Localization; | ||
using System; | ||
|
||
namespace StringLocalizerWithCulture | ||
{ | ||
public static class ServiceCollectionExtensions | ||
{ | ||
public static IServiceCollection AddLocalizationWithCulture(this IServiceCollection services, Action<LocalizationOptions> setupAction = null) | ||
{ | ||
services.AddOptions(); | ||
|
||
services.TryAddSingleton<IStringLocalizerWithCultureFactory, StringLocalizerWithCultureFactory>(); | ||
services.TryAddTransient(typeof(IStringLocalizer<>), typeof(StringLocalizerWithCulture<>)); | ||
if (setupAction != null) | ||
{ | ||
services.Configure(setupAction); | ||
} | ||
|
||
return services; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters