LanguageWwitchover.cs
7.6 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
using Asa.FaceControl;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml.Wordprocessing;
using Model;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Media;
using System.Xml;
using Control = System.Windows.Controls.Control;
using Font = System.Drawing.Font;
using FontStyle = System.Drawing.FontStyle;
namespace SmartScan.SetControl.WPF
{
public class LanguageWwitchover
{
private class ClsLangText
{
public string Name = "";
public string Text = "";
public Font Font = null;
}
public static List<string> Name { get; private set; } = new List<string>();
private static readonly Dictionary<string, Dictionary<string, ClsLangText>> langDict = new Dictionary<string, Dictionary<string, ClsLangText>>();
public static string CurrentLng { get; private set; } = "zh-CN";
private static string filePath;
public static void LoadPath(string path)
{
langDict.Clear();
filePath = path;
string[] files = Directory.GetFiles(path, "*.lngres");
string[] array = files;
foreach (var file in array)
{
var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);
//var langDictTemp = new Dictionary<string, string>();
if (fileNameWithoutExtension.ToLower() == "en-US".ToLower())
{
Name.Add("English");
}
else if (fileNameWithoutExtension.ToLower() == "zh-CN".ToLower())
{
Name.Add("简体中文");
}
langDict.Add(fileNameWithoutExtension, new Dictionary<string, ClsLangText>());
var lines = File.ReadAllLines(file);
string[] array3 = lines;
foreach (var line in array3)
{
string[] parts = line.Split('\t');
if (parts.Length >= 3)
{
var key = parts[0];
var text = parts[2];
string s = ((parts.Length > 3) ? parts[3] : "");
ClsLangText clsLangText = new ClsLangText();
clsLangText.Font = ConvFont(s);
clsLangText.Name = key;
clsLangText.Text = text;
if (!langDict[fileNameWithoutExtension].ContainsKey(key))
{
langDict[fileNameWithoutExtension].Add(key, clsLangText);
}
}
}
//langDict.Add(fileNameWithoutExtension, langDictTemp);
}
}
private static Font ConvFont(string s)
{
if (string.IsNullOrWhiteSpace(s))
{
return null;
}
string[] array = s.Split(',');
float emSize = Convert.ToSingle(array[1]);
FontStyle fontStyle = FontStyle.Regular;
if (array[2] == "B")
{
fontStyle |= FontStyle.Bold;
}
if (array[3] == "I")
{
fontStyle |= FontStyle.Italic;
}
return new Font(array[0], emSize, fontStyle);
}
public static void LoadLanguage(string name)
{
CurrentLng = name;
}
public static string GetText(string key)
{
if (langDict.ContainsKey(CurrentLng) && langDict[CurrentLng].ContainsKey(key))
{
return langDict[CurrentLng][key].ToString();
}
return key; // Return key if text not found
}
public static void SetLanguage(DependencyObject parent)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(parent, i);
SetLanguage(child);
if (child is Control control)
{
// 执行你需要的操作,例如设置文本或字体
Console.WriteLine($"Control Type: {control.GetType().Name}, Name: {control.Name}, Content:{control.Name}");
var key = control.Name; // 假设控件名称是资源键
var text = LanguageWwitchover.GetText(key);
if (control is System.Windows.Controls.Button button)
{
try
{
text = LanguageWwitchover.GetText(langDict[CurrentLng][key].Text);
button.Content = text;
}
catch (Exception)
{
}
}
// 示例:设置控件文本
// string text = LanguageSwitcher.GetText("YourKeyHere");
// control.Content = text;
// 示例:设置控件字体
// if (control is TextBlock textBlock)
// {
// var font = new FontFamily("微软雅黑"), emSize = 12, fontStyle = FontStyles.Normal;
// textBlock.FontFamily = font;
// textBlock.FontSize = emSize;
// textBlock.FontStyle = fontStyle;
// }
}
}
//if (langDict.ContainsKey(CurrentLng) && langDict[CurrentLng].ContainsKey(frm.Name + "_" + frm.Name))
//{
// frm.Text = langDict[CurrentLng][frm.Name + "_" + frm.Name].Text;
// frm.TitleFont = langDict[CurrentLng][frm.Name + "_" + frm.Name].Font;
// SetLang(frm.Name, frm);
//}
}
private static void SetLang(string frmname, System.Windows.Forms.Control ctl)
{
if (!langDict.ContainsKey(CurrentLng))
{
return;
}
foreach (System.Windows.Forms.Control control in ctl.Controls)
{
if (control.Name == "")
{
continue;
}
string key = frmname + "_" + control.Name;
if (control.HasChildren)
{
SetLang(frmname, control);
}
}
}
private static bool HasChinese(string txt)
{
if (Regex.IsMatch(txt.ToString(), "[\\u4E00-\\u9FA5]+"))
{
return true;
}
return false;
}
public static void UpdateUI(UIElement element, string key)
{
if (element is FrameworkElement fe)
{
fe.Dispatcher.Invoke(() =>
{
var text = GetText(key);
if (fe is TextBlock tb)
{
tb.Text = text;
}
else if (fe is System.Windows.Controls.Button btn)
{
btn.Content = text;
}
// Add more controls as needed
});
}
}
}
}