Skip to content

Commit

Permalink
basic localizer
Browse files Browse the repository at this point in the history
  • Loading branch information
Jukka Snellman committed Aug 12, 2022
1 parent af8e794 commit 8fbad11
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 5 deletions.
53 changes: 49 additions & 4 deletions StringLocalizerWithCulture/IStringLocalizerWithCultureFactory.cs
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>()
);
}
}
}
24 changes: 24 additions & 0 deletions StringLocalizerWithCulture/ServiceCollectionExtensions.cs
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;
}
}
}
14 changes: 13 additions & 1 deletion StringLocalizerWithCulture/StringLocalizerWithCulture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@

namespace StringLocalizerWithCulture
{
internal class StringLocalizerWithCulture : IStringLocalizer
internal sealed class StringLocalizerWithCulture : IStringLocalizer
{

public StringLocalizerWithCulture(ResourceManager resources, CultureInfo culture)
{
_resources = resources;
_culture = culture;
}

private readonly CultureInfo _culture;
private readonly ResourceManager _resources;

Expand All @@ -23,5 +29,11 @@ public IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures)
}
}

internal class StringLocalizerWithCulture<T> : StringLocalizerWithCulture, IStringLocalizer<T>
{
public StringLocalizerWithCulture(ResourceManager resources, CultureInfo culture) : base(resources, culture)
{
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Localization" Version="6.0.8" />
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="6.0.8" />
</ItemGroup>

Expand Down

0 comments on commit 8fbad11

Please sign in to comment.