2023-05-11 10:41:01 +02:00
|
|
|
using Microsoft.Extensions.Configuration;
|
2022-12-12 11:22:21 +01:00
|
|
|
using Microsoft.Extensions.Options;
|
2024-10-21 11:59:05 +02:00
|
|
|
using System.Collections.Concurrent;
|
2022-12-12 11:22:21 +01:00
|
|
|
|
|
|
|
|
namespace zero.Localization;
|
|
|
|
|
|
|
|
|
|
public class ConfigurationLocalizer : Localizer
|
|
|
|
|
{
|
2024-10-21 11:59:05 +02:00
|
|
|
protected ConcurrentDictionary<string, Translation> FileCache { get; set; } = [];
|
2022-12-12 11:22:21 +01:00
|
|
|
|
|
|
|
|
|
2023-05-11 11:35:16 +02:00
|
|
|
public ConfigurationLocalizer(IConfiguration configuration, ICultureResolver cultureResolver, IOptionsMonitor<LocalizationOptions> options) : base(cultureResolver)
|
2022-12-12 11:22:21 +01:00
|
|
|
{
|
2024-10-21 11:59:05 +02:00
|
|
|
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 });
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-12 11:22:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected override Translation LoadTranslation(string key)
|
|
|
|
|
{
|
2024-10-21 11:59:05 +02:00
|
|
|
if (!FileCache.TryGetValue(key, out Translation translation))
|
2022-12-12 11:22:21 +01:00
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-21 11:59:05 +02:00
|
|
|
return translation;
|
2022-12-12 11:22:21 +01:00
|
|
|
}
|
|
|
|
|
}
|