new configuration localizer
This commit is contained in:
@@ -1,32 +1,35 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace zero.Localization;
|
||||
|
||||
public class ConfigurationLocalizer : Localizer
|
||||
{
|
||||
private IConfiguration _configuration;
|
||||
protected ConcurrentDictionary<string, Translation> FileCache { get; set; } = [];
|
||||
|
||||
|
||||
public ConfigurationLocalizer(IConfiguration configuration, ICultureResolver cultureResolver, IOptionsMonitor<LocalizationOptions> options) : base(cultureResolver)
|
||||
{
|
||||
_configuration = configuration;
|
||||
IConfigurationSection section = configuration.GetSection($"Zero:Localization:{LanguageCode}");
|
||||
|
||||
if (section != null)
|
||||
{
|
||||
foreach (IConfigurationSection child in section.GetChildren())
|
||||
{
|
||||
FileCache.TryAdd(child.Key, new Translation() { Value = child.Value });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected override Translation LoadTranslation(string key)
|
||||
{
|
||||
IConfigurationSection section = _configuration.GetSection($"Zero:Localization:{LanguageCode}:{key}");
|
||||
|
||||
if (section == null)
|
||||
if (!FileCache.TryGetValue(key, out Translation translation))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new()
|
||||
{
|
||||
Key = key,
|
||||
Value = section.Value
|
||||
};
|
||||
return translation;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
namespace zero.Localization;
|
||||
|
||||
public class Translation
|
||||
public record Translation
|
||||
{
|
||||
/// <summary>
|
||||
/// Key of the translation
|
||||
|
||||
@@ -1,12 +1,8 @@
|
||||
using FluentValidation;
|
||||
using Microsoft.AspNetCore.Mvc.DataAnnotations;
|
||||
using Microsoft.AspNetCore.Mvc.Localization;
|
||||
using Microsoft.AspNetCore.Mvc.Localization;
|
||||
using Microsoft.AspNetCore.Mvc.Razor;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.Localization;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace zero.Localization;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user