Reads the language preference from settings.json and sets the UI culture before any XAML is loaded. Falls back to system locale if missing or "system".
()
| 21 | /// before any XAML is loaded. Falls back to system locale if missing or "system". |
| 22 | /// </summary> |
| 23 | private static void ApplyLanguagePreference() |
| 24 | { |
| 25 | try |
| 26 | { |
| 27 | var configDir = Path.Combine( |
| 28 | Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), |
| 29 | "CloudRedirect"); |
| 30 | var settingsPath = Path.Combine(configDir, "settings.json"); |
| 31 | |
| 32 | if (!File.Exists(settingsPath)) return; |
| 33 | |
| 34 | var json = File.ReadAllText(settingsPath); |
| 35 | using var doc = JsonDocument.Parse(json); |
| 36 | |
| 37 | if (!doc.RootElement.TryGetProperty("language", out var langProp)) |
| 38 | return; |
| 39 | |
| 40 | var lang = langProp.GetString(); |
| 41 | if (string.IsNullOrEmpty(lang) || lang == "system") |
| 42 | return; |
| 43 | |
| 44 | var culture = new CultureInfo(lang); |
| 45 | Thread.CurrentThread.CurrentUICulture = culture; |
| 46 | CultureInfo.DefaultThreadCurrentUICulture = culture; |
| 47 | } |
| 48 | catch |
| 49 | { |
| 50 | // If settings.json is missing, malformed, or the culture code is invalid, |
| 51 | // silently fall back to the system default. |
| 52 | } |
| 53 | } |
| 54 | } |