Language.cs
9.5 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
using System;
using System.Xml;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Drawing;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.IO;
using System.Data.SqlTypes;
using System.Resources;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using System.Xml.Linq;
namespace Asa.FaceControl
{
/// <summary>
/// 多语言类
/// </summary>
public static class Language
{
private static XmlDocument xmlDoc;
private static string filePath; //XML文件的路径
private static int nameIndex = -1;
//private static readonly Dictionary<string,Dictionary<string, string>> dialogText = new();
private static readonly Dictionary<string, Dictionary<string, ClsLangText>> langForm = new();
/// <summary>
/// 语言名称
/// </summary>
public static List<string> Name { private set; get; } = new();
public static string CurrentLng { private set; get; } = "zh-CN";
/// <summary>
/// 加载语言路径
/// </summary>
/// <param name="path"></param>
public static void LoadPath(string path)
{
filePath = path;
var files = System.IO.Directory.GetFiles(path, "*.lngres");
foreach (var file in files)
{
var lngid = Path.GetFileNameWithoutExtension(file);
if (lngid.ToLower() == "en-US".ToLower())
{
Name.Add("English");
}
else if (lngid.ToLower() == "zh-CN".ToLower())
{
Name.Add("简体中文");
}
langForm.Add(lngid, new Dictionary<string, ClsLangText>());
var lngfs = File.ReadAllLines(file);
foreach (var lts in lngfs)
{
var lds = lts.Split('\t');
if (lds.Length < 3)
continue;
var key = lds[0];
var value = lds[2];
var fnt = lds.Length > 3 ? lds[3] : "";
ClsLangText clsLangText = new ClsLangText();
clsLangText.Font = ConvFont(fnt);
clsLangText.Name = key;
clsLangText.Text = value;
if (!langForm[lngid].ContainsKey(key))
langForm[lngid].Add(key, clsLangText);
}
}
LoadLanguage("zh-CN");
}
/// <summary>
/// 加载语言
/// </summary>
/// <param name="name"></param>
public static void LoadLanguage(string name)
{
switch (name)
{
case "简体中文":
CurrentLng = "zh-CN";
break;
case "English":
CurrentLng = "en-US";
break;
default:
CurrentLng = "zh-CN";
break;
}
}
/// <summary>
/// 对话框
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static string Dialog(string key,string defaulttxt="")
{
if (!langForm.ContainsKey(CurrentLng)) return "";
var dk = "Dialog_" + key;
if (langForm[CurrentLng].TryGetValue(dk, out var value))
return value.Text;
else
{
SetNoLangXML("Dialog", key, defaulttxt);
return defaulttxt;
}
}
/// <summary>
/// 设置语言
/// </summary>
/// <param name="frm"></param>
public static void SetLanguage(FormBase frm)
{
if (!langForm.ContainsKey(CurrentLng) || !langForm[CurrentLng].ContainsKey(frm.Name + "_" + frm.Name))
return;
frm.Text = langForm[CurrentLng][frm.Name+"_"+ frm.Name].Text;
frm.TitleFont = langForm[CurrentLng][frm.Name + "_" + frm.Name].Font;
SetLang(frm.Name, frm);
}
/// <summary>
/// 设置语言
/// </summary>
/// <param name="ctl"></param>
public static void SetLanguage(Control ctl)
{
SetLang(ctl.Name, ctl);
}
///// <summary>
///// 语言索引
///// </summary>
//public static int Index
//{
// set
// {
// if (value > -1 && value < _file.Length)
// {
// _index = value;
// LoadLanguage();
// foreach (Form frm in Application.OpenForms)
// {
// if (frm is FormBase)
// SetLanguage((FormBase)frm);
// }
// }
// }
// get
// {
// return _index;
// }
//}
///// <summary>
///// 加载语言
///// </summary>
///// <param name="name"></param>
//public static void Load(string name)
//{
// int idx = Array.FindIndex(Name, s => s == name);
// Index = idx;
//}
///// <summary>
///// 设置语言
///// </summary>
///// <param name="frm"></param>
//public static void SetLanguage(Form frm)
//{
// int langIdx = langForm.FindIndex(s => s.FormLang.Name == frm.Name);
// if (langIdx == -1) return;
// //frm.Text = langForm[langIdx].FormLang.Text;
// //frm.TitleFont = langForm[langIdx].FormLang.Font;
// SetLang(langIdx, frm);
//}
private static void SetLang(string frmname, Control ctl)
{
if (!langForm.ContainsKey(CurrentLng))
return;
foreach (Control cc in ctl.Controls)
{
if (cc.Name == "") continue;
var fn = frmname+"_"+cc.Name;
var idx = langForm[CurrentLng].ContainsKey(fn);
if (idx)
{
//Debugger.Break();
cc.Text = langForm[CurrentLng][fn].Text;
if (cc is FacePanel)
((FacePanel)cc).TitleFont = langForm[CurrentLng][fn].Font;
else
cc.Font = langForm[CurrentLng][fn].Font;
//cc.Tag = langForm[langIndex].ControlLang[idx].Tag;
}
else {
if (!string.IsNullOrWhiteSpace(cc.Text) && HasChinese(cc.Text))
{
if (cc.Tag == null || cc.Tag.ToString() != "not")
{
SetNoLangXML(frmname, cc.Name, cc.Text);
}
}
}
if (cc.HasChildren)
SetLang(frmname,cc);
}
}
static void SetNoLangXML(string formname, string controlname, string text)
{
string att = "";
if (formname == "Dialog")
att = $"{formname}_{controlname}\t{text}\t\t\r\n";
else
{
if (CurrentLng == "简体中文")
att = $"{formname}_{controlname}\t{text}\t\t微软雅黑,12,,\r\n";
else
att = $"{formname}_{controlname}\t{text}\t\tArial,12,,\r\n";
}
File.AppendAllText(Path.Combine(filePath, CurrentLng + ".lngres"), att);
}
static bool HasChinese(string txt)
{
//for (int i = 0; i < txt.Length; i++)
//{
if (Regex.IsMatch(txt.ToString(), @"[\u4E00-\u9FA5]+"))
return true;
//}
return false;
}
/// <summary>
/// 转换字体
/// </summary>
/// <param name="s">格式:字体,字号,粗体,斜体</param>
/// <returns></returns>
private static Font ConvFont(string s)
{
if (string.IsNullOrWhiteSpace(s))
return null;
string[] t = s.Split(',');
float emSize = Convert.ToSingle(t[1]);
FontStyle style = FontStyle.Regular;
if (t[2] == "B") style |= FontStyle.Bold;
if (t[3] == "I") style |= FontStyle.Italic;
Font font = new(t[0], emSize, style);
return font;
}
///// <summary>
///// 转换字体
///// </summary>
///// <param name="f">字体</param>
///// <returns></returns>
//private static string ConvFont(Font f)
//{
// if (f == null)
// return "";
// string[] s = new string[4];
// s[0] = f.Name;
// s[1] = f.Size.ToString();
// if (f.Bold) s[2] = "B";
// if (f.Italic) s[3] = "I";
// return string.Join(",", s);
//}
private class ClsLangForm
{
public ClsLangText FormLang;
public List<ClsLangText> ControlLang;
public ClsLangForm()
{
FormLang = new ClsLangText();
ControlLang = new List<ClsLangText>();
}
}
private class ClsLangText
{
public string Name = "";
public string Text = "";
public Font Font = null;
//public static string Tag = "";
}
}
}