Language.cs
5.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
using System;
using System.Xml;
using System.Windows.Forms;
using System.Collections.Generic;
namespace Asa.Theme
{
/// <summary>
/// 多语言类
/// </summary>
public class Language
{
private XmlDocument doc;
private XmlNode root;
private bool save; //是否保存
private string[] _file; //XML文件的路径
//public Dictionary<string, string> dialog;
/// <summary>
/// 多语言
/// </summary>
public Language()
{ }
/// <summary>
/// 语言名称
/// </summary>
public string[] Name { set; get; }
/// <summary>
/// 加载语言目录
/// </summary>
/// <param name="path"></param>
public void Load(string path)
{
if (!System.IO.Directory.Exists(path)) return;
//dialog = new Dictionary<string, string>();
_file = System.IO.Directory.GetFiles(path, "*.xml");
Name = new string[_file.Length];
for (int i = 0; i < Name.Length; i++)
{
doc = new XmlDocument();
doc.Load(_file[i]);
Name[i] = ((XmlElement)doc.LastChild).GetAttribute("Name");
}
}
/// <summary>
/// 语言切换
/// </summary>
/// <param name="index"></param>
/// <param name="frm"></param>
public void Shift(int index, params FlatForm[] frm)
{
if (index < 0 || index >= _file.Length) return;
doc = new XmlDocument();
doc.Load(_file[index]);
save = false;
for (int i = 0; i < frm.Length; i++)
{
XmlNode node = doc.LastChild.SelectSingleNode(frm[i].Name);
if (node == null)
{
XmlElement ele = doc.CreateElement(frm[i].Name);
ele.SetAttribute("Text", frm[i].Text);
doc.LastChild.AppendChild(ele);
root = ele;
save = true;
}
else
{
root = node;
frm[i].DialogLang = new Dictionary<string, string>();
XmlNode dia = node.SelectSingleNode("Dialog");
if (dia != null)
{
foreach (XmlNode xn in dia)
frm[i].DialogLang.Add(xn.Name, ((XmlElement)xn).GetAttribute("Text"));
}
}
frm[i].Text = ((XmlElement)root).GetAttribute("Text");
SetLang(frm[i]);
}
if (save)
doc.Save(_file[index]);
doc = null;
}
/// <summary>
/// 设置控件的文本
/// </summary>
/// <param name="ctl"></param>
private void SetLang(System.Windows.Forms.Control ctl)
{
string s;
foreach (System.Windows.Forms.Control cc in ctl.Controls)
{
if (string.IsNullOrWhiteSpace(cc.Name)) continue;
XmlNode xn = root.SelectSingleNode(cc.Name);
if (xn == null)
{
XmlElement ele = doc.CreateElement(cc.Name);
ele.SetAttribute("Text", cc.Text);
//ele.SetAttribute("Tag", cc.Tag == null ? "" : cc.Tag.ToString());
ele.SetAttribute("Font", ConvFont(cc.Font));
if (cc.Text.Length == 0)
ele.SetAttribute("Enabled", "false");
else
ele.SetAttribute("Enabled", "true");
root.AppendChild(ele);
save = true;
}
else
{
s = ((XmlElement)xn).GetAttribute("Enabled");
if (Convert.ToBoolean(s))
{
cc.Text = ((XmlElement)xn).GetAttribute("Text");
s = ((XmlElement)xn).GetAttribute("Font");
if (s != "") cc.Font = ConvFont(s);
}
}
if (cc.HasChildren)
{
root = root.SelectSingleNode(cc.Name);
SetLang(cc);
}
}
root = root.ParentNode;
}
/// <summary>
/// 转换字体
/// </summary>
/// <param name="s">格式:字体,字号,粗体,斜体</param>
/// <returns></returns>
private System.Drawing.Font ConvFont(string s)
{
string[] t = s.Split(',');
float emSize = Convert.ToSingle(t[1]);
System.Drawing.FontStyle style = System.Drawing.FontStyle.Regular;
if (t[2] == "B") style |= System.Drawing.FontStyle.Bold;
if (t[3] == "I") style |= System.Drawing.FontStyle.Italic;
System.Drawing.Font font = new System.Drawing.Font(t[0], emSize, style);
return font;
}
/// <summary>
/// 转换字体
/// </summary>
/// <param name="f">字体</param>
/// <returns></returns>
private string ConvFont(System.Drawing.Font f)
{
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);
}
}
}