98 lines
3.4 KiB
C#
98 lines
3.4 KiB
C#
#nullable enable
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
using System.Text.Json;
|
|
using ReminderOrganizer.Models;
|
|
|
|
namespace ReminderOrganizer.Services
|
|
{
|
|
/// <summary>
|
|
/// Loads UI strings from Locales/<lang>.json, embedded into the exe
|
|
/// (resource name "Locales.<lang>.json"). Missing keys fall back to English, then to the key.
|
|
/// </summary>
|
|
public class LocalizationService : ILocalizationService
|
|
{
|
|
public static readonly string[] SupportedLanguages = { "en", "de" };
|
|
|
|
private Dictionary<string, string> _strings = new();
|
|
private Dictionary<string, string> _fallback = new();
|
|
private string _currentLanguage = "en";
|
|
private AppData? _appData;
|
|
private readonly IDataService _dataService;
|
|
|
|
public string CurrentLanguage => _currentLanguage;
|
|
public event EventHandler? LanguageChanged;
|
|
public event EventHandler? StringsChanged;
|
|
|
|
public LocalizationService(IDataService dataService)
|
|
{
|
|
_dataService = dataService;
|
|
_fallback = LoadStrings("en");
|
|
_strings = _fallback;
|
|
}
|
|
|
|
public void SetLanguage(string languageCode)
|
|
{
|
|
if (Array.IndexOf(SupportedLanguages, languageCode) < 0) languageCode = "en";
|
|
if (_currentLanguage == languageCode) return;
|
|
_currentLanguage = languageCode;
|
|
_strings = LoadStrings(languageCode);
|
|
if (_appData != null)
|
|
{
|
|
_appData.Language = languageCode;
|
|
_ = SaveQuietly(_appData);
|
|
}
|
|
LanguageChanged?.Invoke(this, EventArgs.Empty);
|
|
StringsChanged?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
|
|
private async System.Threading.Tasks.Task SaveQuietly(AppData data)
|
|
{
|
|
try { await _dataService.SaveAsync(data); }
|
|
catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"[Localization] Save failed: {ex.Message}"); }
|
|
}
|
|
|
|
public void Initialize(AppData appData)
|
|
{
|
|
_appData = appData;
|
|
var lang = string.IsNullOrEmpty(appData.Language) ? Core.SystemPrefs.Language() : appData.Language;
|
|
if (lang == _currentLanguage)
|
|
{
|
|
appData.Language = lang;
|
|
StringsChanged?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
else
|
|
{
|
|
SetLanguage(lang);
|
|
}
|
|
}
|
|
|
|
public string GetString(string key)
|
|
{
|
|
if (_strings.TryGetValue(key, out var value)) return value;
|
|
return _fallback.TryGetValue(key, out var fb) ? fb : key;
|
|
}
|
|
|
|
public static Dictionary<string, string> LoadStrings(string languageCode)
|
|
{
|
|
try
|
|
{
|
|
var asm = Assembly.GetExecutingAssembly();
|
|
using var stream = asm.GetManifestResourceStream($"Locales.{languageCode}.json");
|
|
if (stream != null)
|
|
{
|
|
using var reader = new StreamReader(stream);
|
|
return JsonSerializer.Deserialize<Dictionary<string, string>>(reader.ReadToEnd()) ?? new();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
System.Diagnostics.Debug.WriteLine($"[Localization] Load {languageCode} failed: {ex.Message}");
|
|
}
|
|
return new();
|
|
}
|
|
}
|
|
}
|