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