设置 Form_Login的 Localizable 属性为 true, 设置该属性后,.net 将根据不同的语言,为应用程序生成不同的资源文件
更改 Form_Login的 Language 属性为想要支持的另一种语言,此例中我们选用 English
此时.net 将为 Form_Login生成另一个资源文件,在本例中名为 Form_Login.resx
在资源文件中要像下面的label1这样设置属性,才能在Language属性选择为这种语言时这个页面为这种语言
>>label1.Name label1
>>label1.Parent $this
>>label1.Type System.Windows.Forms.Label, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
>>label1.ZOrder 4
/// <summary> /// Get The Resource for the language which is selected. /// </summary> /// <param name="FormType">eg:typeof(Form_Login)</param> /// <param name="sLanguageCode">eg:"en","zh-CN","zh-HK"</param> private void GetResource(string sLanguageCode,Type FormType) { Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(sLanguageCode); System.ComponentModel.ComponentResourceManager res = new ComponentResourceManager(FormType); foreach (Control ctl in Controls) { res.ApplyResources(ctl, ctl.Name); } }
GetResource("zh-HK", typeof(Form_Login));//在Form_Login页面使用“中国香港繁体”语言的资源:Form_Login.zh-HK.resx
结果:
其为每种语言生成了一个DLL文件:bin\Debug\zh-HK\项目名.resources.dll
当窗体里的panel或datagridview里的控件都要翻译时,上面那个代码行不通,我改为如下(写在类里不能直接使用Controls集合):
/// <summary> /// Get The Resource for the language which is selected. /// </summary> /// <param name="FormType">eg:typeof(Form_Login)</param> /// <param name="sLanguageCode">eg:"en","zh-CN","zh-HK"</param> private void GetResource(string sLanguageCode, Type FormType, Control.ControlCollection Controls) { Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(sLanguageCode); System.ComponentModel.ComponentResourceManager res = new ComponentResourceManager(FormType); foreach (Control ctl in Controls) { res.ApplyResources(ctl, ctl.Name); string a = ctl.GetType().ToString(); if (a == "System.Windows.Forms.Panel") { foreach (Control item in ctl.Controls) { res.ApplyResources(item, item.Name); } } if (a == "System.Windows.Forms.DataGridView") { DataGridView dgvSend = ctl as DataGridView; foreach (DataGridViewColumn item in dgvSend.Columns) { res.ApplyResources(item, item.Name); } } } }
因为Controls是Form的属性,而且上面没有改到窗体的标题,所以GetResource方法改为
private void GetResource(string sLanguageCode,Form form) { Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(sLanguageCode); System.ComponentModel.ComponentResourceManager res = new ComponentResourceManager(form.GetType()); res.ApplyResources(form,"$this");//窗体标题 foreach (Control ctl in form.Controls) { res.ApplyResources(ctl, ctl.Name); string a = ctl.GetType().ToString(); if (a == "System.Windows.Forms.Panel") { foreach (Control item in ctl.Controls) { res.ApplyResources(item, item.Name); } } if (a == "System.Windows.Forms.DataGridView") { DataGridView dgvSend = ctl as DataGridView; foreach (DataGridViewColumn item in dgvSend.Columns) { res.ApplyResources(item, item.Name); } } } }
添加xml文件作为资源文件:
1.添加现有文件XML_Info11.xml
2.属性=>生成操作=>嵌入的资源
3.获取xml元素的值:
private void LoadResources() { //get current Assembly object. System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly(); //load embedded resource into stream String ResourceName = "如何添加及使用资源文件.XML_Info11.xml";//ProjectName.FileName System.IO.Stream str = asm.GetManifestResourceStream(ResourceName); System.IO.StreamReader reader = new System.IO.StreamReader(str); //*** convert stream into XDocument and load in System.Xml.Linq.XDocument xdoc = System.Xml.Linq.XDocument.Parse(reader.ReadToEnd()); this.Text = xdoc.Element("rss").Element("channel").Element("title").Value.ToString(); }