-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDynamicLanguageModel.cs
More file actions
42 lines (33 loc) · 1.1 KB
/
Copy pathDynamicLanguageModel.cs
File metadata and controls
42 lines (33 loc) · 1.1 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
using System.ComponentModel;
using System.Dynamic;
using System.Runtime.CompilerServices;
public class DynamicLanguageModel : DynamicObject, INotifyPropertyChanged
{
private readonly IniFile _ini;
private string _currentLang;
private HashSet<string> _cachedKeys = new();
public DynamicLanguageModel(string iniPath)
{
_ini = new IniFile(iniPath);
_currentLang = "zh"; // 默认语言
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string name = null)
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
public void SetLanguage(string lang)
{
_currentLang = lang;
foreach (var key in _cachedKeys)
{
OnPropertyChanged(key);
}
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
string key = binder.Name;
_cachedKeys.Add(key);
result = _ini.Read(_currentLang, key);
return true;
}
public List<string> AvailableLanguages => _ini.GetSections();
}