C# 實現多語言介面
建立一個存放語言的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");
}