ability to switch translation language

This commit is contained in:
2023-05-11 10:41:01 +02:00
parent 6e5702c10c
commit 286a1477d1
3 changed files with 55 additions and 8 deletions
+5 -5
View File
@@ -1,7 +1,4 @@
using System.IO;
using Microsoft.AspNetCore.Hosting;
using System.Text;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
namespace zero.Localization;
@@ -14,12 +11,15 @@ public class ConfigurationLocalizer : Localizer
public ConfigurationLocalizer(IConfiguration configuration, IOptionsMonitor<LocalizationOptions> options) : base()
{
_configuration = configuration;
// set default language
Language(_configuration.GetValue<string>("Zero:Localization:Default", "de"));
}
protected override Translation LoadTranslation(string key)
{
IConfigurationSection section = _configuration.GetSection($"Zero:Localization:{key}");
IConfigurationSection section = _configuration.GetSection($"Zero:Localization:{LanguageCode}:{key}");
if (section == null)
{
+24 -1
View File
@@ -5,9 +5,22 @@ namespace zero.Localization;
public abstract class Localizer : ILocalizer
{
/// <inheritdoc />
public string LanguageCode { get; private set; } = "de";
protected ConcurrentDictionary<string, string> Cache { get; private set; } = new();
/// <inheritdoc />
public ILocalizer Language(string languageCode)
{
Cache = new();
LanguageCode = languageCode;
return this;
}
/// <inheritdoc />
public string Text(string key) => Text(key, null);
@@ -83,7 +96,17 @@ public abstract class Localizer : ILocalizer
}
public interface ILocalizer
{
{
/// <summary>
/// Current language code
/// </summary>
string LanguageCode { get; }
/// <summary>
/// Set language for localizer
/// </summary>
ILocalizer Language(string languageCode);
/// <summary>
///
/// </summary>
+26 -2
View File
@@ -22,7 +22,14 @@ public class Numbers : INumbers
/// <inheritdoc />
public async Task<string> Next(string alias, DateTimeOffset? date = null, bool store = true)
public Task<string> Next(string alias, DateTimeOffset? date = null, bool store = true)
{
return Next(alias, date.HasValue ? DateOnly.FromDateTime(date.Value.Date) : null, store);
}
/// <inheritdoc />
public async Task<string> Next(string alias, DateOnly? date = null, bool store = true)
{
Number number = await Get(alias);
string key = GetCounterKey(number);
@@ -92,10 +99,17 @@ public class Numbers : INumbers
/// <inheritdoc />
public string Compile(string template, long value, int minLength, DateTimeOffset? date = null)
{
return Compile(template, value, minLength, date.HasValue ? DateOnly.FromDateTime(date.Value.Date) : null);
}
/// <inheritdoc />
public string Compile(string template, long value, int minLength, DateOnly? date = null)
{
string output = template;
MatchCollection matches = templateRegex.Matches(output);
DateTimeOffset usedDate = date ?? DateTimeOffset.Now;
DateOnly usedDate = date ?? DateOnly.FromDateTime(DateTime.Now);
foreach (Match match in matches)
{
@@ -228,6 +242,11 @@ public interface INumbers
/// </summary>
Task<string> Next(string alias, DateTimeOffset? date = null, bool store = true);
/// <summary>
/// Get the next available number for the specified template
/// </summary>
Task<string> Next(string alias, DateOnly? date = null, bool store = true);
/// <summary>
/// Resets the current counter for the specified template
/// </summary>
@@ -242,4 +261,9 @@ public interface INumbers
/// Renders the final output from the template and the value
/// </summary>
string Compile(string template, long value, int minLength, DateTimeOffset? date = null);
/// <summary>
/// Renders the final output from the template and the value
/// </summary>
string Compile(string template, long value, int minLength, DateOnly? date = null);
}