1. 簡體中文
2. 繁體中文
3. 英文
下面子豐介紹一下實現的過程:
1. 為每個窗口創建相應語言的resx文件。子豐以英文為例,右鍵->添加->新建項->資源文件,文件名為窗口名.en-US,如上面的兩個窗口,分別為LoginForm.en-US.resx和PasswordForm.en-US.resx。簡體中文為LoginForm.zh-CN.resx和PasswordForm.zh-CN.resx,繁體中文為LoginForm.zh-CHT.resx和PasswordForm.zh-CHT.resx。下面給出LoginForm.en-US.resx文件的截圖。
2. 在項目的Properties的Settings.settings中添加變量DefaultLanguage,用於保存當前設置的默認語言。當下次啟動程序時,會讀取該變量,從而將程序的語言設置為上次程序關閉時的語言。
3. 創建一個靜態類(MultiLanguage.cs)用於編寫與切換語言相關的變量和代碼。
(1)變量DefaultLanguage,用於保存當前默認語言
(2)函數SetDefaultLanguage修改當前默認語言
/// <summary> /// 修改默認語言 /// </summary> /// <param name="lang">待設置默認語言</param> public static void SetDefaultLanguage(string lang) { System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang); DefaultLanguage = lang; Properties.Settings.Default.DefaultLanguage = lang; Properties.Settings.Default.Save(); }
(3)函數LoadLanguage用於加載語言或切換語言
/// <summary> /// 加載語言 /// </summary> /// <param name="form">加載語言的窗口</param> /// <param name="formType">窗口的類型</param> public static void LoadLanguage(Form form, Type formType) { if (form != null) { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(formType); resources.ApplyResources(form, "$this"); Loading(form, resources); } } /// <summary> /// 加載語言 /// </summary> /// <param name="control">控件</param> /// <param name="resources">語言資源</param> private static void Loading(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) { //遍歷菜單 Loading(c, resources); } } } foreach (Control c in control.Controls) { resources.ApplyResources(c, c.Name); Loading(c, resources); } } /// <summary> /// 遍歷菜單 /// </summary> /// <param name="item">菜單項</param> /// <param name="resources">語言資源</param> private static void Loading(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) { Loading(c, resources); } } } }
4. 在主窗口的Load事件中讀取Properties.Settings.Default.DefaultLanguage,並將ComboBox賦值為當前默認語言,即簡體中文、繁體中文或英文。
private void LoginForm_Load(object sender, EventArgs e) { //設置combobox的值 string language = Properties.Settings.Default.DefaultLanguage; if (language == "zh-CN") { languageTxt.Text = "簡體中文(默認)"; } else if (language == "zh-CHT") { languageTxt.Text = "繁體中文"; } else if (language == "en-US") { languageTxt.Text = "English"; } }
5. 在每個窗口的Load事件中調用函數MultiLanguage.LoadLanguage,使窗口在出現時即顯示為當前默認語言。
private void PasswordForm_Load(object sender, EventArgs e) { //加載語言 MultiLanguage.LoadLanguage(this, typeof(PasswordForm)); }
6. 編寫用於切換語言的ComboBox的SelectedIndexChanged事件,使得當用戶選擇對應的語言時,程序會切換到該語言。
//切換語言 private void languageTxt_SelectedIndexChanged(object sender, EventArgs e) { languageTxt.Enabled = false; if (languageTxt.Text == "簡體中文(默認)") { //修改默認語言 MultiLanguage.SetDefaultLanguage("zh-CN"); //對所有打開的窗口重新加載語言 foreach (Form form in Application.OpenForms) { LoadAll(form); } } else if (languageTxt.Text == "繁體中文") { //修改默認語言 MultiLanguage.SetDefaultLanguage("zh-CHT"); //對所有打開的窗口重新加載語言 foreach (Form form in Application.OpenForms) { LoadAll(form); } } else if (languageTxt.Text == "English") { //修改默認語言 MultiLanguage.SetDefaultLanguage("en-US"); //對所有打開的窗口重新加載語言 foreach (Form form in Application.OpenForms) { LoadAll(form); } } languageTxt.Enabled = true; } private void LoadAll(Form form) { if (form.Name == "LoginForm") { MultiLanguage.LoadLanguage(form, typeof(LoginForm)); } else if (form.Name == "PasswordForm") { MultiLanguage.LoadLanguage(form, typeof(PasswordForm)); } }
轉自:http://blog.csdn.net/softimite_zifeng/article/details/54177825