alielie
2026-09-10 9ec80cafc23f5ee3278cacce3c9f86f4087b435a
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using AddInPlugin.Setting;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OpenTap;
using OpenTapEditor.Util;
using System.ComponentModel;
using System.IO;
using System.Windows;
 
namespace SeqEditor.Util
{
    public class LangUtil
    {
        public const string DefaultLang = "zh-CN";
        public static List<LangInfo> langs = new List<LangInfo>() {
            new LangInfo(DefaultLang,"中文"),
            new LangInfo("en-US","English")
        };
 
        private static ResourceDictionary currentResource;
 
        private static Dictionary<string, ResourceDictionary> ResourceMap
            = new Dictionary<string, ResourceDictionary>();
        private static Dictionary<string, JObject> JsonMap = new Dictionary<string, JObject>();
 
        private static StationSettings setting;
 
        static LangUtil()
        {
            var td = TypeData.GetDerivedTypes<ComponentSettings>()
           .Where(x => x.CanCreateInstance && (x.GetAttribute<BrowsableAttribute>()?.Browsable ?? true))
           .OfType<TypeData>()
           .FirstOrDefault(x => x.Name == typeof(StationSettings).FullName);
            if (td == null)
            {
                return;
            }
            setting = (StationSettings)ComponentSettings.GetCurrent(td.Load());
        }
 
        private static string GetLang()
        {
            var lang = setting?.Lang ?? DefaultLang;
            if (string.IsNullOrEmpty(lang)) lang = "zh-CN";
            return lang;
        }
 
        public static void LoadLang()
        {
            var lang = GetLang();
            if (!ResourceMap.ContainsKey(lang)) return;
            var newLang = ResourceMap[lang];
            if (currentResource == newLang)
            {
                return;
            }
            if (currentResource != null)
            {
                Application.Current.Resources.MergedDictionaries.Remove(currentResource);
            }
            currentResource = newLang;
            Application.Current.Resources.MergedDictionaries.Add(currentResource);
        }
 
        public static void Load()
        {
            try
            {
                LoadAllData();
                LoadLang();
            }
            finally
            {
 
            }
        }
 
        public static string Get(string key)
        {
            var lang = GetLang();
            if (!JsonMap.ContainsKey(lang)) return key;
            if (!JsonMap[lang].ContainsKey(key)) return key;
            return JsonMap[lang][key].ToString();
        }
 
        private static void LoadDataByJson(string lang)
        {
            string langFolder = PathHelper.LangFolder;
            string folder = Path.Combine(langFolder, lang);
            if (!Directory.Exists(folder))
            {
                return;
            }
 
            string[] files = Directory.GetFiles(folder, "*.json", SearchOption.AllDirectories);
 
            foreach (var path in files)
            {
                try
                {
                    using (FileStream stream = File.Open(path, FileMode.Open))
                    {
                        using (StreamReader streamReader = new StreamReader(stream))
                        {
                            JsonTextReader jsonTextReader = new JsonTextReader(streamReader);
                            JObject jsonObject = (JObject)JToken.ReadFrom(jsonTextReader);
 
                            if (!JsonMap.ContainsKey(lang))
                            {
                                JsonMap[lang] = jsonObject;
                            }
                            else
                            {
                                foreach (var item in jsonObject)
                                {
                                    JsonMap[lang][item.Key] = item.Value;
                                }
                            }
                        }
                    }
                }
                catch
                {
                }
            }
        }
 
        private static void LoadXamlFromJson(string lang)
        {
            LoadDataByJson(lang);
            if (!JsonMap.ContainsKey(lang)) return;
            JObject json = JsonMap[lang];
            var resource = new ResourceDictionary();
            foreach (var item in json)
            {
                resource.Add(item.Key, item.Value.ToString());
            }
            ResourceMap[lang] = resource;
        }
 
        private static void LoadXamlFromJson2(string lang)
        {
            LoadDataByJson(lang);
 
            if (!JsonMap.ContainsKey(lang)) return;
            JObject json = JsonMap[lang];
            var resource = new ResourceDictionary();
            foreach (var item in json)
            {
                resource.Add(item.Key, item.Value.ToString());
            }
            ResourceMap[lang] = resource;
            if (lang != DefaultLang)
            {
                var defaultMap = new ResourceDictionary();
                foreach (var item in json)
                {
                    defaultMap.Add(item.Key, item.Key);
                }
                ResourceMap[DefaultLang] = defaultMap;
            }
        }
 
        private static void LoadAllData()
        {
            foreach (var lang in langs)
            {
                LoadXamlFromJson(lang.Code);
            }
        }
 
        public static string Format(string key, params object[] datas)
        {
            return string.Format(Get(key), datas);
        }
    }
 
}