All checks were successful
Game Ideas deploy / build-test-deploy (push) Successful in 1m13s
Reviewed-on: #45
26 lines
871 B
C#
26 lines
871 B
C#
using System.Globalization;
|
|
using System.Text.Json;
|
|
|
|
namespace GameIdeas.Resources;
|
|
|
|
public class TranslationService
|
|
{
|
|
private readonly Dictionary<string, Dictionary<string, string>?> _translations = [];
|
|
|
|
public void Initialize(Dictionary<string, string> translations)
|
|
{
|
|
foreach (var translation in translations)
|
|
{
|
|
var json = JsonSerializer.Deserialize<Dictionary<string, string>>(translation.Value);
|
|
_translations[translation.Key] = json;
|
|
}
|
|
}
|
|
|
|
public string Translate(string key, string? culture = "fr")
|
|
{
|
|
culture ??= CultureInfo.CurrentCulture.TwoLetterISOLanguageName;
|
|
if (_translations.TryGetValue(culture, out var value) && value?.TryGetValue(key, out var translate) == true)
|
|
return translate;
|
|
return key; // Fallback to key if translation is missing
|
|
}
|
|
} |