建立一个存放语言的XML文件
循环遍历界面上的控件。
private void getLanguage(string LanguageCode)
{
string FileName;
FileName = Application.StartupPath + @"\Resource\Localization\Language.xml";
XmlDocument doc = new XmlDocument();
doc.Load(FileName);
XmlNodeList nodes = doc.GetElementsByTagName("item");
for (int i = 0; i < nodes.Count; i++)
{
//获得将当前元素的key属性
XmlAttribute att = nodes[i].Attributes["key"];
XmlAttribute att1 = nodes[i].Attributes[LanguageCode];
foreach (Control FrmControl in this.Controls)
{
if (FrmControl is GroupBox)
{
GroupBox gbControl = FrmControl as GroupBox;
foreach (Control subControl in gbControl.Controls)
{
if (att.Value == subControl.Name)
{
subControl.Text = att1.Value;
}
}
}
if (att.Value == FrmControl.Name)
{
FrmControl.Text = att1.Value;
}
}
}
}
通过用户点击菜单实现语言切换
private void tsmiSimplifiedChinese_Click(object sender, EventArgs e)
{
getLanguage("zh-CN");
}
private void tsmiEnglish_Click(object sender, EventArgs e)
{
getLanguage("en");
}