Files
ReminderOrganizer/Services/LocalizationService.cs
Michi 1918712f7f Fix data loss risk, reminder bugs; theme, credits, tests
- Store tasks in %APPDATA% (single-file extract dir lost data on update),
  atomic serialized saves, corrupt-file backup, migration of old tasks.json
- Reminders without description never showed (empty balloon text threw)
- Catch up missed reminders on start; monthly recurrence no longer drifts
- Task dialog: date pick kept 00:00, cancel leaked recurrence changes
- CSV import understands quoted fields written by export
- Confirm before deleting; name folders on create; tasks sorted by date
- Single instance, autostart starts hidden in tray, tray icon disposed
- Theme helper for all dialogs, MDL2/Path icons instead of emojis,
  fixed folder selection/badge, localized counts and messages
- Credits (c) Michael Media + Discord link, exe Company/Copyright
- Locales embedded; xUnit tests (24); README, CLAUDE.md, sprite-generator

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 14:08:53 +01:00

92 lines
3.2 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/&lt;lang&gt;.json, embedded into the exe
/// (resource name "Locales.&lt;lang&gt;.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;
if (!string.IsNullOrEmpty(appData.Language))
{
SetLanguage(appData.Language);
}
}
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();
}
}
}