Files
game-ideas/src/GameIdeas/GameIdeas.Resources/TranslationService.cs
Egamorf 58da2e6843
All checks were successful
Game Ideas deploy / build-test-deploy (push) Successful in 1m13s
Run code clean and fix messages (#45)
Reviewed-on: #45
2025-05-07 01:28:37 +02:00

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
}
}