C#?winform實現(xiàn)多語言切換功能
更新時間:2024年02月06日 14:10:54 作者:廷益--飛鳥
這篇文章主要為大家詳細介紹了如何使用C#?winform實現(xiàn)多語言切換功能,文中的示例代碼講解詳細,具有一定的借鑒價值,感興趣的小伙伴可以了解下
前后對比
使用nuget json工具包
總體思路
創(chuàng)建對應(yīng)的json字典對照表
{ "測試":"Test", "語言":"Language", "設(shè)置":"Set", "中文(默認)":"Chinese (default)", "英文":"English", "標簽測試":"Label Test", "主界面-標題":"Main Title", "組合框":"group box", "選擇框":"checkBox", "單選按鈕":"radioButton", "列標題1":"ColumnHeader1", "列標題2":"ColumnHeader2", }
加載對應(yīng) json文件
/// <summary> /// 當前項目文件夾Debug\Language\參數(shù)文件夾 /// </summary> /// <param name="language">配置文件所在文件夾名</param> public static void LoadLanguage(TransType transType, string language = "") { if (string.IsNullOrEmpty(language)) { language = System.Threading.Thread.CurrentThread.CurrentUICulture.Name; } resources = new Dictionary<string, string>(); string dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, string.Format("Language/{0}.json", language)); if (File.Exists(dir)) { LoadFile(transType, dir); } } /// <summary> /// 配置文件加載 /// </summary> /// <param name="path">配置文件絕對路徑(包括文件本身)</param> public static void LoadFile(TransType transType, string path) { var content = File.ReadAllText(path, Encoding.UTF8); if (!string.IsNullOrEmpty(content)) { var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(content); foreach (string key in dict.Keys) { if(transType == TransType.cn_en) { // 中文轉(zhuǎn)英文 if (!resources.ContainsKey(key)) { resources.Add(key, dict[key]); } else resources[key] = dict[key]; } else if(transType == TransType.en_cn) { // 英文轉(zhuǎn)中文 var value = dict[key]; if (!resources.ContainsKey(value)) { resources.Add(value, key); } else resources[value] = key; } } } }
設(shè)置窗口語言
/// <summary> /// 遍歷翻譯 窗體或控件及其子控件 /// </summary> /// <param name="control">需要翻譯的控件或窗體</param> public static void InitLanguage(Control control) { SetControlLanguage(control); // 循環(huán)所有控件 foreach (Control ctrl in control.Controls) { InitLanguage(ctrl); } //工具欄 或者菜單動態(tài)構(gòu)建窗體或者控件的時候,重新對子控件進行處理 control.ControlAdded += (sender, e) => { InitLanguage(e.Control); }; } /// <summary> /// 控件及子控件翻譯 /// </summary> /// <param name="control">需要翻譯的控件</param> public static void SetControlLanguage(Control control) { if (control is ComboBox) { // ComboBox 轉(zhuǎn)換 ComboBox combox = control as ComboBox; int curSelect = combox.SelectedIndex; string[] NewItems = new string[combox.Items.Count]; for (int i = 0; i < combox.Items.Count; i++) { if (resources.ContainsKey(combox.Items[i].ToString())) { NewItems[i] = resources[combox.Items[i].ToString()]; } else NewItems[i] = combox.Items[i].ToString(); } combox.Text = (resources.ContainsKey(combox.Text)) ? resources[combox.Text] : combox.Text; combox.Items.Clear(); combox.Items.AddRange(NewItems); combox.SelectedIndex = curSelect; } else if (control is TreeView) { //control is 其他控件或者特殊控件 如:TreeView } else if(control is ListView) { // ListView 標題修改 var listView = control as ListView; for (int i = 0; i < listView.Columns.Count; i++) { string titleName = listView.Columns[i].Text.ToString(); if (resources.ContainsKey(titleName)) { listView.Columns[i].Text = resources[titleName]; } } } else { control.Text = (resources.ContainsKey(control.Text)) ? resources[control.Text] : control.Text; } }
到此這篇關(guān)于C# winform實現(xiàn)多語言切換功能的文章就介紹到這了,更多相關(guān)C# winform多語言內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
DevExpress之餅狀圖突出(Explode)設(shè)置實例
這篇文章主要介紹了DevExpress之餅狀圖突出(Explode)設(shè)置方法,以實例形式展示了餅狀圖突出設(shè)置的具體實現(xiàn)過程,非常具有實用價值,需要的朋友可以參考下2014-10-10C#中IList<T>與List<T>的區(qū)別深入解析
本篇文章主要是對C#中IList<T>與List<T>的區(qū)別進行了詳細的分析介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01C#使用SQL Dataset數(shù)據(jù)集代碼實例
今天小編就為大家分享一篇關(guān)于的文章,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10DevExpress設(shè)置FocusedNode背景色的方法
這篇文章主要介紹了DevExpress設(shè)置FocusedNode背景色的方法,很實用的功能,需要的朋友可以參考下2014-08-08