Files
HC_APTBS/Services/Impl/LocalizationService.cs
LucianoDev 827b811b39 feat: developer tools page, auto-test orchestrator, BIP display, tests redesign
Bundles several feature streams that have been iterating on the working tree:

- Developer Tools page (Debug-only via DEVELOPER_TOOLS symbol): hosts the
  identification card, manual KWP write + transaction log, ROM/EEPROM dump
  card with progress banner and completion message, persisted custom-commands
  library, persisted EEPROM passwords library. New service primitives:
  IKwpService.SendRawCustomAsync / ReadEepromAsync / ReadRomEepromAsync.
  Persistence mirrors the Clients XML pattern in two new files
  (custom_commands.xml, eeprom_passwords.xml).
- Auto-test orchestrator (IAutoTestOrchestrator + AutoTestState): linear
  K-Line read -> unlock -> bench-on -> test sequence with snackbar UI and
  progress dialog VM, gated on dashboard alarms.
- BIP-STATUS display: BipDisplayViewModel + BipDisplayView, RAM read at
  0x0106 via IKwpService.ReadBipStatusAsync; status definitions in
  BipStatusDefinition.
- Tests page redesign: TestSectionCard + PhaseTileView replacing the old
  TestPlanView/TestRunningView/TestDoneView/TestPreconditionsView/
  TestSectionView controls and their VMs.
- Pump command sliders: Fluent thick-track style with overhang thumb,
  click-anywhere-and-drag, mouse-wheel adjustment.
- Window startup: app.manifest declares PerMonitorV2 DPI awareness,
  MainWindow installs a WM_GETMINMAXINFO hook in OnSourceInitialized and
  maximizes there (after the hook is in place) so the app fits the work
  area exactly on any display configuration.
- Misc: PercentToPixelsConverter, seed_aliases.py one-shot pump-alias
  importer, tools/Import-BipStatus.ps1, kline_eeprom_spec.md and
  dump-functions reference docs.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-05-07 13:59:50 +02:00

107 lines
3.9 KiB
C#

using System;
using System.Windows;
using HC_APTBS.Infrastructure.Logging;
namespace HC_APTBS.Services.Impl
{
/// <summary>
/// Manages runtime language switching by swapping a merged
/// <see cref="ResourceDictionary"/> in <see cref="Application.Current"/>.
/// </summary>
/// <remarks>
/// On construction the service reads <see cref="Models.AppSettings.Language"/>
/// and loads the corresponding dictionary. Subsequent calls to
/// <see cref="SetLanguage"/> replace it in-place and persist the preference.
/// </remarks>
public sealed class LocalizationService : ILocalizationService
{
private const string LogId = "LOCALIZATION";
private const string EspUri = "pack://application:,,,/Resources/Strings.es.xaml";
private const string EngUri = "pack://application:,,,/Resources/Strings.en.xaml";
private readonly IConfigurationService _config;
private readonly IAppLogger _log;
// Single, persistent slot in Application.Resources.MergedDictionaries whose
// Source we re-point on every language change. Mutating an existing
// dictionary's Source triggers WPF's DynamicResource invalidation reliably,
// whereas removing-and-adding entries can leave stale resolved values
// depending on the dictionary's parent/owner state.
private readonly ResourceDictionary _stringsSlot = new();
/// <inheritdoc />
public string CurrentLanguage { get; private set; } = "ESP";
/// <inheritdoc />
public event Action? LanguageChanged;
/// <summary>
/// Initialises the localization service and loads the language
/// stored in <see cref="Models.AppSettings.Language"/>.
/// </summary>
public LocalizationService(IConfigurationService config, IAppLogger log)
{
_config = config;
_log = log;
// Mount the persistent slot once. From here on we only update its Source.
Application.Current.Resources.MergedDictionaries.Add(_stringsSlot);
// Load persisted language without saving (already persisted).
LoadDictionary(_config.Settings.Language);
}
/// <inheritdoc />
public void SetLanguage(string languageCode)
{
var code = NormaliseCode(languageCode);
_log.Info(LogId, $"SetLanguage('{languageCode}') -> normalised='{code}', current='{CurrentLanguage}'");
if (code == CurrentLanguage)
{
_log.Info(LogId, "SetLanguage: already current, no-op.");
return;
}
LoadDictionary(code);
// Persist the choice.
_config.Settings.Language = code;
_config.SaveSettings();
LanguageChanged?.Invoke();
}
/// <inheritdoc />
public string GetString(string key)
{
return Application.Current.Resources[key]?.ToString() ?? key;
}
// ── Helpers ──────────────────────────────────────────────────────────────
private void LoadDictionary(string languageCode)
{
var code = NormaliseCode(languageCode);
var uri = code == "ENG" ? EngUri : EspUri;
try
{
_stringsSlot.Source = new Uri(uri, UriKind.Absolute);
}
catch (Exception ex)
{
_log.Error(LogId, $"LoadDictionary({code}) failed to load {uri}: {ex.Message}");
return;
}
CurrentLanguage = code;
_log.Info(LogId,
$"LoadDictionary({code}): {uri} loaded with {_stringsSlot.Count} keys.");
}
private static string NormaliseCode(string code) =>
string.Equals(code, "ENG", StringComparison.OrdinalIgnoreCase) ? "ENG" : "ESP";
}
}