2023-05-11 10:41:01 +02:00
|
|
|
using Microsoft.Extensions.Configuration;
|
2022-12-12 11:22:21 +01:00
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
|
|
|
|
|
namespace zero.Localization;
|
|
|
|
|
|
|
|
|
|
public class ConfigurationLocalizer : Localizer
|
|
|
|
|
{
|
|
|
|
|
private IConfiguration _configuration;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public ConfigurationLocalizer(IConfiguration configuration, IOptionsMonitor<LocalizationOptions> options) : base()
|
|
|
|
|
{
|
|
|
|
|
_configuration = configuration;
|
2023-05-11 10:41:01 +02:00
|
|
|
|
|
|
|
|
// set default language
|
|
|
|
|
Language(_configuration.GetValue<string>("Zero:Localization:Default", "de"));
|
2022-12-12 11:22:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected override Translation LoadTranslation(string key)
|
|
|
|
|
{
|
2023-05-11 10:41:01 +02:00
|
|
|
IConfigurationSection section = _configuration.GetSection($"Zero:Localization:{LanguageCode}:{key}");
|
2022-12-12 11:22:21 +01:00
|
|
|
|
|
|
|
|
if (section == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new()
|
|
|
|
|
{
|
|
|
|
|
Key = key,
|
|
|
|
|
Value = section.Value
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|