winform 国际化多语言
要在C#中实现国际化,需要相关资源文件,比如要在一个软件中支持英文、中文、繁体三种语言,那么就必须有这三种语言的资源文件,这在C#中可以采用资源文件(后缀名为.resx)来实现,我们不妨定义英文资源文件名称为Resource.en-US.resx,中文资源文件名称一般默认为Resource.resx(也可以是Resource.zh-CN.resx),繁体资源文件名称为:Resource.zh-CHT.resx,三种资源文件所涉及的ID都应该是一样的(这对于其他更多的资源文件均是一样的),只不过是展示的名称不同罢了。
1.下面是多语言类,放到跟窗体一个程序集下:
(
SetLang(string lang, Form form, Type formType) 设置语言根据实际项目去设置语言
)
#region SetAllLang /// <summary> /// 设置所有窗体的界面语言 /// </summary> /// <param name="lang">language:zh-CN, en-US</param> public static void SetAllLang(string lang) { System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang); Form frm = null; //string path = AssemblyName.GetAssemblyName("PMISServer").ToString();//项目的Assembly选项名称 //Assembly asm = Assembly.Load("PMISServer");//程序集名 // object frmObj = asm.CreateInstance("Fastyou.BookShop.Win." + formName);//程序集+form的类名。 string name = "MainForm"; //类的名字 frm = (Form)Assembly.Load("EndoMasterServer").CreateInstance(name); //Type formType = null; //formType = typeof(frm); if (frm != null) { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(); resources.ApplyResources(frm, "$this"); AppLang(frm, resources); } } #endregion #region SetLang /// <summary> /// 设置当前程序的界面语言 /// </summary> /// <param name="lang">language:zh-CN, en-US</param> /// <param name="form">窗体实例</param> /// <param name="formType">窗体类型</param> public static void SetLang(string lang, Form form, Type formType) { if (String.IsNullOrEmpty(lang)) { int iLanguageOption = 0; string strLanguageOption = ConfigHelper.GetAppConfig(ConfigHelper.NodeNameType.appSettings, "LanguageOption"); if (!string.IsNullOrEmpty(strLanguageOption)) { if (int.TryParse(strLanguageOption, out iLanguageOption) == false) { iLanguageOption = 0; } } if (iLanguageOption == 0) { lang = "zh-CN"; } else if (iLanguageOption == 1) { lang = "en-US"; } else if (iLanguageOption == 2) { //lang = ""; } } System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang); if (form != null) { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(formType); resources.ApplyResources(form, "$this"); AppLang(form, resources); } } #endregion public static void SetLang(Form form, Type formType) { string lang = string.Empty; int iLanguageOption = 0; string strLanguageOption = ConfigHelper.GetAppConfig(ConfigHelper.NodeNameType.appSettings, "LanguageOption"); if (!string.IsNullOrEmpty(strLanguageOption)) { if (int.TryParse(strLanguageOption, out iLanguageOption) == false) { iLanguageOption = 0; } } if (iLanguageOption == 0) { lang = "zh-CN"; } else if (iLanguageOption == 1) { lang = "en-US"; } else if (iLanguageOption == 2) { lang = "zh-CHT"; } System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang); if (form != null) { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(formType); resources.ApplyResources(form, "$this"); AppLang(form, resources); } } #region AppLang for control /// <summary> /// 遍历窗体所有控件,针对其设置当前界面语言 /// </summary> /// <param name="control"></param> /// <param name="resources"></param> private static void AppLang(Control control, System.ComponentModel.ComponentResourceManager resources) { if (control is MenuStrip) { //将资源应用与对应的属性 resources.ApplyResources(control, control.Name); MenuStrip ms = (MenuStrip)control; if (ms.Items.Count > 0) { foreach (ToolStripMenuItem c in ms.Items) { //调用 遍历菜单 设置语言 AppLang(c, resources); } } } foreach (Control c in control.Controls) { resources.ApplyResources(c, c.Name); AppLang(c, resources); } } #endregion #region AppLang for menuitem /// <summary> /// 遍历菜单 /// </summary> /// <param name="item"></param> /// <param name="resources"></param> private static void AppLang(ToolStripMenuItem item, System.ComponentModel.ComponentResourceManager resources) { if (item is ToolStripMenuItem) { resources.ApplyResources(item, item.Name); ToolStripMenuItem tsmi = (ToolStripMenuItem)item; if (tsmi.DropDownItems.Count > 0) { foreach (ToolStripMenuItem c in tsmi.DropDownItems) { //if (tsmi != ToolStripSeparator) //{ } AppLang(c, resources); } } } } #endregion
2.在选择语言的窗体页面选择时,设置语言
// 中文简体: zh-CN 英文:en-US 繁体:zh-CHT ChangLanguage.SetLang("zh-CN", this, typeof(窗体名称));
3. 在每个窗体的 _Load(object sender, EventArgs e)方法中加入以下代码:
ChangLanguage.SetLang(this, typeof(窗体名称));