Files
mixtape/Finch/Localization/Localizer.cs
T

138 lines
3.1 KiB
C#
Raw Normal View History

2022-12-07 14:05:11 +01:00
using System.Collections.Concurrent;
2023-05-11 11:35:16 +02:00
using System.Globalization;
2022-12-07 14:05:11 +01:00
using System.Reflection;
2026-04-07 14:23:29 +02:00
namespace Finch.Localization;
2022-12-07 14:05:11 +01:00
2022-12-07 15:20:53 +01:00
public abstract class Localizer : ILocalizer
2022-12-07 14:05:11 +01:00
{
2026-04-07 15:24:48 +02:00
protected ConcurrentDictionary<string, string> Cache { get; } = new();
2022-12-07 14:05:11 +01:00
2026-04-07 15:24:48 +02:00
protected ICultureResolver CultureResolver { get; }
2022-12-07 14:05:11 +01:00
2023-05-11 11:35:16 +02:00
protected string LanguageCode { get; set; }
public Localizer(ICultureResolver cultureResolver)
2023-05-11 10:41:01 +02:00
{
2023-05-11 11:35:16 +02:00
CultureResolver = cultureResolver;
CultureResolver.Subscribe(msg => OnCultureChange(msg.Culture));
OnCultureChange(CultureResolver.Current);
}
Task OnCultureChange(CultureInfo culture)
{
2024-11-14 10:33:44 +01:00
culture ??= CultureInfo.InvariantCulture;
LanguageCode = culture.Name.Split(['_', '-'])[0];
2023-05-11 11:35:16 +02:00
return Task.CompletedTask;
2023-05-11 10:41:01 +02:00
}
2022-12-07 14:05:11 +01:00
/// <inheritdoc />
public string Text(string key) => Text(key, null);
/// <inheritdoc />
public string Text(string key, Dictionary<string, string> tokens)
{
if (key.IsNullOrEmpty())
{
return null;
}
if (!Cache.TryGetValue(key, out string value))
{
Translation translation = LoadTranslation(key);
if (translation == null)
{
return null;
}
value = translation.Value;
Cache.TryAdd(key, value);
}
if (tokens != null)
{
value = TokenReplacement.Apply(value, tokens);
}
return value;
}
/// <inheritdoc />
public string Text<T>(T enumValue) where T : Enum => Text(enumValue, null);
/// <inheritdoc />
public string Text<T>(T enumValue, Dictionary<string, string> tokens) where T : Enum
{
Type type = enumValue.GetType();
MemberInfo memInfo = type.GetMember(enumValue.ToString())[0];
return Text(memInfo.GetCustomAttribute<LocalizeAttribute>()?.Key, tokens);
}
/// <inheritdoc />
public string Maybe(string key) => Maybe(key, null);
/// <inheritdoc />
public string Maybe(string key, Dictionary<string, string> tokens)
{
if (key.IsNullOrEmpty() || !key.StartsWith("@"))
{
string value = key;
if (tokens != null)
{
value = TokenReplacement.Apply(value, tokens);
}
return value;
}
return Text(key.Substring(1), tokens);
2022-12-07 14:05:11 +01:00
}
/// <summary>
/// Get translation from database or any other source
/// </summary>
2022-12-07 15:20:53 +01:00
protected abstract Translation LoadTranslation(string key);
2022-12-07 14:05:11 +01:00
}
public interface ILocalizer
2023-05-11 10:41:01 +02:00
{
2022-12-07 14:05:11 +01:00
/// <summary>
///
/// </summary>
string Text(string key);
/// <summary>
///
/// </summary>
string Text(string key, Dictionary<string, string> tokens);
/// <summary>
/// Get a text string from a [Localize] attribute
/// </summary>
string Text<T>(T enumValue) where T : Enum;
/// <summary>
/// Get a text string from a [Localize] attribute
/// </summary>
string Text<T>(T enumValue, Dictionary<string, string> tokens) where T : Enum;
/// <summary>
/// Only tries to resolve the key when it is prefixed with an @
/// </summary>
string Maybe(string key);
/// <summary>
/// Only tries to resolve the key when it is prefixed with an @
/// </summary>
string Maybe(string key, Dictionary<string, string> tokens);
}