|
| 1 | +using System; |
| 2 | +using System.Globalization; |
| 3 | +using GeneralUpdate.Tools.Services; |
| 4 | + |
| 5 | +namespace Irihi.Lingua; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Concrete Lingua language manager for GeneralUpdate.Tools. |
| 9 | +/// Loads resources from the existing <see cref="LocalizationService"/> JSON files |
| 10 | +/// and bridges reactive culture switching with the legacy localization system. |
| 11 | +/// |
| 12 | +/// Usage: |
| 13 | +/// <code> |
| 14 | +/// // Switch language (updates both Lingua observables AND legacy bindings) |
| 15 | +/// AppLanguageManager.Instance.UpdateCulture(new CultureInfo("zh-CN")); |
| 16 | +/// |
| 17 | +/// // Reactive access |
| 18 | +/// var title = AppLanguageManager.Instance.App_Title; |
| 19 | +/// title.Subscribe(t => Console.WriteLine(t)); |
| 20 | +/// |
| 21 | +/// // XAML with TranslateExtension |
| 22 | +/// <TextBlock Text="{Translate {x:Static lingua:AppLanguageManager+Keys.App_Title}}" /> |
| 23 | +/// </code> |
| 24 | +/// </summary> |
| 25 | +[LinguaManager("./Resources/Locales")] |
| 26 | +public sealed class AppLanguageManager : LanguageManager |
| 27 | +{ |
| 28 | + public static new AppLanguageManager Instance { get; } = new(); |
| 29 | + |
| 30 | + private AppLanguageManager() : base("zh-CN") |
| 31 | + { |
| 32 | + // Load all resources from existing LocalizationService |
| 33 | + LoadFromLocalizationService(); |
| 34 | + |
| 35 | + // Set initial culture |
| 36 | + UpdateCulture(new CultureInfo(LocalizationService.Instance.Locale)); |
| 37 | + |
| 38 | + // Listen for legacy locale changes and sync back to Lingua |
| 39 | + LocalizationService.Instance.PropertyChanged += (_, e) => |
| 40 | + { |
| 41 | + if (e.PropertyName == nameof(LocalizationService.Locale)) |
| 42 | + UpdateCulture(new CultureInfo(LocalizationService.Instance.Locale)); |
| 43 | + }; |
| 44 | + } |
| 45 | + |
| 46 | + private void LoadFromLocalizationService() |
| 47 | + { |
| 48 | + var loc = LocalizationService.Instance; |
| 49 | + |
| 50 | + // Register zh-CN from the loaded JSON or fallback dictionary |
| 51 | + AddResources(new CultureInfo("zh-CN"), loc.GetAllStrings("zh-CN")); |
| 52 | + |
| 53 | + // Register en-US |
| 54 | + AddResources(new CultureInfo("en-US"), loc.GetAllStrings("en-US")); |
| 55 | + } |
| 56 | + |
| 57 | + // ── Strongly-typed observable properties ───────────────── |
| 58 | + // Only the most commonly used keys are exposed as properties. |
| 59 | + // All other keys are accessible via GetObservable("Key.Name"). |
| 60 | + |
| 61 | + public IObservable<string?> App_Title => GetObservable("App.Title"); |
| 62 | + public IObservable<string?> Nav_Patch => GetObservable("Nav.Patch"); |
| 63 | + public IObservable<string?> Nav_Extension => GetObservable("Nav.Extension"); |
| 64 | + public IObservable<string?> Nav_OSS => GetObservable("Nav.OSS"); |
| 65 | + public IObservable<string?> Nav_Simulate => GetObservable("Nav.Simulate"); |
| 66 | + public IObservable<string?> Nav_Config => GetObservable("Nav.Config"); |
| 67 | + public IObservable<string?> Nav_Settings => GetObservable("Nav.Settings"); |
| 68 | + public IObservable<string?> Patch_Title => GetObservable("Patch.Title"); |
| 69 | + public IObservable<string?> Patch_Build => GetObservable("Patch.Build"); |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Gets the current translation for a key (synchronous, non-observable). |
| 73 | + /// </summary> |
| 74 | + public string this[string key] => Resolve(key) ?? key; |
| 75 | + |
| 76 | + // ── Keys class (mirrors source-generator output) ───────── |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Strongly-typed keys for use with <see cref="TranslateExtension"/> in XAML. |
| 80 | + /// Usage: <c>{Translate {x:Static lingua:AppLanguageManager+Keys.App_Title}}</c> |
| 81 | + /// </summary> |
| 82 | + public static class Keys |
| 83 | + { |
| 84 | + public static LinguaKey App_Title => new("App.Title", Instance); |
| 85 | + public static LinguaKey Nav_Patch => new("Nav.Patch", Instance); |
| 86 | + public static LinguaKey Nav_Extension => new("Nav.Extension", Instance); |
| 87 | + public static LinguaKey Nav_OSS => new("Nav.OSS", Instance); |
| 88 | + public static LinguaKey Nav_Simulate => new("Nav.Simulate", Instance); |
| 89 | + public static LinguaKey Nav_Config => new("Nav.Config", Instance); |
| 90 | + public static LinguaKey Nav_Settings => new("Nav.Settings", Instance); |
| 91 | + public static LinguaKey Patch_Title => new("Patch.Title", Instance); |
| 92 | + public static LinguaKey Patch_OldDir => new("Patch.OldDir", Instance); |
| 93 | + public static LinguaKey Patch_NewDir => new("Patch.NewDir", Instance); |
| 94 | + public static LinguaKey Patch_Build => new("Patch.Build", Instance); |
| 95 | + public static LinguaKey Patch_PackageInfo => new("Patch.PackageInfo", Instance); |
| 96 | + public static LinguaKey Patch_OutputDir => new("Patch.OutputDir", Instance); |
| 97 | + public static LinguaKey Patch_Select => new("Patch.Select", Instance); |
| 98 | + public static LinguaKey Ext_Title => new("Ext.Title", Instance); |
| 99 | + public static LinguaKey OSS_Title => new("OSS.Title", Instance); |
| 100 | + public static LinguaKey Sim_Title => new("Sim.Title", Instance); |
| 101 | + public static LinguaKey Config_Title => new("Config.Title", Instance); |
| 102 | + public static LinguaKey Settings_UploadSection => new("Settings.UploadSection", Instance); |
| 103 | + public static LinguaKey Settings_Save => new("Settings.Save", Instance); |
| 104 | + public static LinguaKey Settings_Reset => new("Settings.Reset", Instance); |
| 105 | + public static LinguaKey Upload_Success => new("Upload.Success", Instance); |
| 106 | + public static LinguaKey Upload_Failed => new("Upload.Failed", Instance); |
| 107 | + } |
| 108 | +} |
0 commit comments