alielie
2025-05-20 99df058a665ea04065a23e729ec79a2f23f79881
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using HMI.Core.Entity;
using HMI.Core.Setting;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Windows;
using System.Windows.Markup;
 
namespace HMI.Core.Util
{
    public class LangUtil
    {
        public static List<LangInfo> langs = new List<LangInfo>() {
            new LangInfo("zh-CN","中文"),
            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>();
 
        static LangUtil()
        {
 
        }
 
        private static string GetLang()
        {
            var lang = SystemSetting.Instance.CurrentLanguage;
            if (string.IsNullOrEmpty(lang)) lang = CultureInfo.CurrentUICulture.ToString();
            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
            {
                /// 按Json加载
                //LoadAllDataByJson();
                /// 下面是按xaml加载
                LoadAllDataByXaml();
                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 LoadDataByXaml(string lang)
        {
            string langFolder = PathHelper.LangFolder;
            string folder = Path.Combine(langFolder, lang);
            if (!Directory.Exists(folder))
            {
                return;
            }
 
            string[] files = Directory.GetFiles(folder, "*.xaml", SearchOption.AllDirectories);
 
            foreach (var path in files)
            {
                try
                {
                    using (FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read))
                    {
                        var resource = (ResourceDictionary)XamlReader.Load(stream);
                        if (!ResourceMap.ContainsKey(lang))
                        {
                            ResourceMap[lang] = resource;
                        }
                        else
                        {
                            ResourceMap[lang].MergedDictionaries.Add(resource);
                        }
                    }
                }
                catch
                {
                }
            }
        }
 
        private static void LoadAllDataByJson()
        {
            foreach (var lang in langs)
            {
                LoadDataByJson(lang.Code);
            }
        }
 
        private static void LoadAllDataByXaml()
        {
            foreach (var lang in langs)
            {
                LoadXamlFromJson(lang.Code);
            }
        }
    }
 
    public static class LangReader
    {
        public static string lang(this object dp, string key, params object[] datas)
        {
            return string.Format(LangUtil.Get(key), datas);
        }
    }
}