c#国际化
1.首先建立资源文件
LanguageDefine.xml
<?xml version="1.0" standalone="yes"?> <Language> <DefaultLanguage>ZH</DefaultLanguage> </Language>
设置默认语言
2.建立选择文件
AppConfig.xml
<?xml version="1.0" encoding="utf-8" ?> <AppConfig> <Area> <Language>ZH</Language> <List> <Item>中文[ZH]</Item> <Item>英文[EN]</Item> </List> </Area> <Area> <Language>EN</Language> <List> <Item>Chinese[ZH]</Item> <Item>English[EN]</Item> </List> </Area> </AppConfig>
选择中文还是英文
3.建立语言配置文件
AppResource_EN.xml
<?xml version="1.0" encoding="utf-8" ?> <Resource> <Form> <Name>SwellInstrument</Name> <Controls> <Control name="SwellInstrument" text="SwellInstrument" /> <Control name="Swe_label" text="Please double click on the select Swell Instrument" /> <Control name="Swe_Line_Label" text="Port:" /> <Control name="Swe_label1" text="Parity:" /> <Control name="Swe_label3" text="Baud rate:" /> <Control name="Swe_label2" text="Stop bit:" /> <Control name="Swe_label5" text="Data bits:" /> <Control name="Swe_label4" text="Handshake protocol" /> <Control name="Swe_test" text="Test" /> <Control name="Swe_confirm" text="Save" /> <Control name="Swe_cancel" text="Cancel" /> </Controls> </Form> </Resource>
AppResource_ZH.xml
<?xml version="1.0" encoding="utf-8" ?> <Resource> <Form> <Name>SwellInstrument</Name> <Controls> <Control name="SwellInstrument" text="涌浪仪" /> <Control name="Swe_label" text="请双击选择涌浪仪" /> <Control name="Swe_Line_Label" text="端口号:" /> <Control name="Swe_label1" text="奇偶校验:" /> <Control name="Swe_label3" text="波特率:" /> <Control name="Swe_label2" text="停止位:" /> <Control name="Swe_label5" text="数据位:" /> <Control name="Swe_label4" text="握手协议:" /> <Control name="Swe_test" text="测试" /> <Control name="Swe_confirm" text="保存" /> <Control name="Swe_cancel" text="取消" /> </Controls> </Form> </Resource>
4.配置文件
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing.Imaging; using System.ComponentModel; using System.Text.RegularExpressions; using System.Xml; using System.Collections; using System.IO; using System.Windows.Forms; using System.Data; using System.Resources; namespace Hydor1 { class International { public static string Global="EN"; public International() { } //frmName是窗体的Name,lang是要显示的语言(如ZH或EN), Hashtable的Key值就是Name属性的值,其value值就是Text属性将要显示的值 public static Hashtable ReadResource(string frmName, string lang) { Hashtable result = new Hashtable(); XmlReader reader = null; FileInfo fi = new FileInfo(System.Windows.Forms.Application.StartupPath + @"../../../Resources/AppResource_" + lang + ".xml"); // if (!fi.Exists) // reader = new XmlTextReader("../../Resources/AppResource.xml");//备份文件 // else reader = new XmlTextReader(System.Windows.Forms.Application.StartupPath + @"../../../Resources/AppResource_" + lang + ".xml"); XmlDocument doc = new XmlDocument(); doc.Load(reader); XmlNode root = doc.DocumentElement; XmlNodeList nodelist = root.SelectNodes("Form[Name='" + frmName + "']/Controls/Control"); foreach (XmlNode node in nodelist) { try { XmlNode node1 = node.SelectSingleNode("@name"); XmlNode node2 = node.SelectSingleNode("@text"); if (node1 != null) { result.Add(node1.InnerText.ToLower(), node2.InnerText); } } catch (FormatException fe) { Console.WriteLine(fe.ToString()); } } reader.Close(); return result; } //读取默认语言 public static string ReadDefaultLanguage() { string xmlName = System.Windows.Forms.Application.StartupPath + @"../../../Resources/LanguageDefine.xml"; // XmlReader reader = new XmlTextReader("../../Resources/LanguageDefine.xml"); XmlReader reader = XmlReader.Create(xmlName); XmlDocument doc = new XmlDocument(); doc.Load(reader); XmlNode root = doc.DocumentElement; //选取DefaultLangugae节点 XmlNode node = root.SelectSingleNode("DefaultLanguage"); string result=string.Empty ; if (node != null) //取出节点中的内容 result = node.InnerText; reader.Close(); return result; } //修改默认语言 public static void WriteDefaultLanguage(string lang) { DataSet ds = new DataSet(); ds.ReadXml(System.Windows.Forms.Application.StartupPath + @"../../../Resources/LanguageDefine.xml"); DataTable dt = ds.Tables["Language"]; dt.Rows[0]["DefaultLanguage"] = lang; ds.AcceptChanges(); ds.WriteXml(System.Windows.Forms.Application.StartupPath + @"../../../Resources/LanguageDefine.xml"); } //读取下拉数据,即变换语言 public static IList GetLanguageList(string lang) { IList result = new ArrayList(); XmlReader reader = new XmlTextReader(System.Windows.Forms.Application.StartupPath + @"../../../Resources/AppConfig.xml"); XmlDocument doc = new XmlDocument(); doc.Load(reader); XmlNode root = doc.DocumentElement; XmlNodeList nodelist = root.SelectNodes("Area[Language='" + lang + "']/List/Item"); foreach (XmlNode node in nodelist) { result.Add(node.InnerText); } reader.Close(); return result; } // 获取控件的名称 public static void getNames(Form form,string lang) { Hashtable table = ReadResource(form.Name, lang); //将所有控件放入集合 Control.ControlCollection controlNames = form.Controls; //可以设置窗体的一些统一的属性............................................kaili...没写 try { //遍历控件 foreach (Control control in controlNames) { //控件类型 //if (control.GetType() == typeof(System.Windows.Forms.Form)) // GetSubControls(control.Controls, table); if (control.GetType() == typeof(System.Windows.Forms.GroupBox)) GetSubControls(control.Controls, table); if (control.GetType() == typeof(System.Windows.Forms.Label)) GetSubControls(control.Controls, table); if (table.Contains(control.Name.ToLower())) control.Text = (string)table[control.Name.ToLower()]; } if (table.Contains(form.Name.ToLower())) form.Text = (string)table[form.Name.ToLower()]; } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } // 获得子控件的显示名 ,读取控件中子控件的显示值 private static void GetSubControls(Control.ControlCollection controls, Hashtable table) { foreach (Control control in controls) { // if (control.GetType() == typeof(System.Windows.Forms.Form)) // GetSubControls(control.Controls, table); if (control.GetType() == typeof(System.Windows.Forms.GroupBox)) GetSubControls(control.Controls, table); if (control.GetType() == typeof(System.Windows.Forms.Label)) GetSubControls(control.Controls, table); if (table.Contains(control.Name.ToLower())) control.Text = (string)table[control.Name.ToLower()]; } } } }
5.设置下拉选择语言
#region 国际化下拉取值 private void ChooseLanguage_comboBox_SelectedIndexChanged(object sender, EventArgs e) { //获取combobox的值 Global = ChooseLanguage_comboBox.SelectedItem.ToString(); Global = Global.Trim(); if (Global.IndexOf("-") == 0) { return; } else { Global = Global.Substring(Global.IndexOf("[") + 1, 2); } if (Global == "EN") { //ChooseLanguage_comboBox.SelectedIndexChanged -= new EventHandler(ChooseLanguage_comboBox_SelectedIndexChanged); ChooseLanguage_comboBox.Items.Clear(); // ChooseLanguage_comboBox.Text = "English"; International.WriteDefaultLanguage(Global); IList a = International.GetLanguageList(Global); for (int i = 0; i < a.Count; i++) { string aa = a[i].ToString(); aa=aa.Trim(); ChooseLanguage_comboBox.Items.Add(aa); } ChooseLanguage_comboBox.SelectedIndex = 1; // ChooseLanguage_comboBox.SelectedIndex = -1; } else { // ChooseLanguage_comboBox.SelectedIndexChanged -= new EventHandler(ChooseLanguage_comboBox_SelectedIndexChanged); ChooseLanguage_comboBox.Items.Clear(); International.WriteDefaultLanguage("ZH"); IList a = International.GetLanguageList(Global); // ChooseLanguage_comboBox.Text = "ZH"; for (int i = 0; i < a.Count; i++) { string aa = a[i].ToString(); aa = aa.Trim(); ChooseLanguage_comboBox.Items.Add(aa); } // ChooseLanguage_comboBox.Text = ChooseLanguage_comboBox.Items[0].ToString(); ChooseLanguage_comboBox.SelectedIndex = 0; } } //设置事件只发生一次 private void ChooseLanguage_comboBox_SelectedValueChanged(object sender, EventArgs e) { string str = ChooseLanguage_comboBox.SelectedItem.ToString().Trim().Substring(ChooseLanguage_comboBox.SelectedItem.ToString().IndexOf("[") + 1, 2).ToUpper(); ; if (this.Global == str) { this.ChooseLanguage_comboBox.SelectedIndexChanged -= ChooseLanguage_comboBox_SelectedIndexChanged; this.axMapControl1.Focus(); } else { this.ChooseLanguage_comboBox.SelectedIndexChanged += ChooseLanguage_comboBox_SelectedIndexChanged; } } #endregion
6.界面加载和关闭
private void FormMain_Load(object sender, EventArgs e) { string stringa= "---请选择语言---"; ChooseLanguage_comboBox.Items.Insert(0, stringa); ChooseLanguage_comboBox.Text = ChooseLanguage_comboBox.Items[0].ToString(); Global = International.ReadDefaultLanguage(); IList a = International.GetLanguageList(Global); for (int i = 0; i < a.Count; i++) { string aa = a[i].ToString(); aa=aa.Trim(); ChooseLanguage_comboBox.Items.Add(aa); } } private void FormMain_FormClosed(object sender, FormClosedEventArgs e) { International.WriteDefaultLanguage("ZH"); }