feat(i18n): localize ViewModel-built strings via ambient Loc accessor
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
43
src/ClaudeDo.Ui/Localization/Loc.cs
Normal file
43
src/ClaudeDo.Ui/Localization/Loc.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using ClaudeDo.Localization;
|
||||
|
||||
namespace ClaudeDo.Ui.Localization;
|
||||
|
||||
/// Ambient access to the active localizer for code-built (ViewModel) strings.
|
||||
/// Set once at startup. Defaults to a key-echo localizer so unit tests that
|
||||
/// construct ViewModels without startup wiring do not crash.
|
||||
public static class Loc
|
||||
{
|
||||
private static ILocalizer _current = new KeyEchoLocalizer();
|
||||
|
||||
public static ILocalizer Current
|
||||
{
|
||||
get => _current;
|
||||
set
|
||||
{
|
||||
if (_current is not null) _current.LanguageChanged -= OnInnerChanged;
|
||||
_current = value;
|
||||
_current.LanguageChanged += OnInnerChanged;
|
||||
OnInnerChanged(value, EventArgs.Empty);
|
||||
}
|
||||
}
|
||||
|
||||
public static event EventHandler? LanguageChanged;
|
||||
|
||||
private static void OnInnerChanged(object? sender, EventArgs e) =>
|
||||
LanguageChanged?.Invoke(sender, e);
|
||||
|
||||
public static string T(string key) => Current[key];
|
||||
public static string T(string key, params object[] args) => Current.Get(key, args);
|
||||
|
||||
private sealed class KeyEchoLocalizer : ILocalizer
|
||||
{
|
||||
public string this[string key] => key;
|
||||
public string Get(string key, params object[] args) => key;
|
||||
public string CurrentCode => "en";
|
||||
public IReadOnlyList<LanguageOption> AvailableLanguages => Array.Empty<LanguageOption>();
|
||||
public void SetLanguage(string code) { }
|
||||
public event EventHandler? LanguageChanged { add { } remove { } }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user