-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLanguageManager.cs
More file actions
61 lines (51 loc) · 1.63 KB
/
Copy pathLanguageManager.cs
File metadata and controls
61 lines (51 loc) · 1.63 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
using System.Globalization;
using FlowNoteMauiApp.Resources;
using Microsoft.Maui.ApplicationModel;
namespace FlowNoteMauiApp;
public static class LanguageManager
{
private const string LanguageKey = "SelectedLanguage";
public static event Action? LanguageChanged;
public static CultureInfo CurrentCulture { get; private set; } = CultureInfo.CurrentUICulture;
public static void Initialize()
{
string? savedLanguage;
try
{
savedLanguage = Preferences.Get(LanguageKey, null);
}
catch
{
Preferences.Remove(LanguageKey);
savedLanguage = null;
}
if (!string.IsNullOrEmpty(savedLanguage))
{
var culture = new CultureInfo(savedLanguage);
SetCulture(culture);
}
else
{
SetCulture(CultureInfo.CurrentUICulture);
}
}
public static void SetCulture(CultureInfo culture)
{
CurrentCulture = culture;
CultureInfo.DefaultThreadCurrentCulture = culture;
CultureInfo.DefaultThreadCurrentUICulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
Thread.CurrentThread.CurrentCulture = culture;
AppResources.Culture = culture;
Preferences.Set(LanguageKey, culture.Name);
if (MainThread.IsMainThread)
{
LanguageChanged?.Invoke();
}
else
{
MainThread.BeginInvokeOnMainThread(() => LanguageChanged?.Invoke());
}
}
public static bool IsChinese => CurrentCulture.Name.StartsWith("zh", StringComparison.OrdinalIgnoreCase);
}