C#?winform實(shí)現(xiàn)多語(yǔ)言切換功能
前后對(duì)比
使用nuget json工具包



總體思路
創(chuàng)建對(duì)應(yīng)的json字典對(duì)照表
{
"測(cè)試":"Test",
"語(yǔ)言":"Language",
"設(shè)置":"Set",
"中文(默認(rèn))":"Chinese (default)",
"英文":"English",
"標(biāo)簽測(cè)試":"Label Test",
"主界面-標(biāo)題":"Main Title",
"組合框":"group box",
"選擇框":"checkBox",
"單選按鈕":"radioButton",
"列標(biāo)題1":"ColumnHeader1",
"列標(biāo)題2":"ColumnHeader2",
}
加載對(duì)應(yīng) json文件
/// <summary>
/// 當(dāng)前項(xiàng)目文件夾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">配置文件絕對(duì)路徑(包括文件本身)</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è)置窗口語(yǔ)言
/// <summary>
/// 遍歷翻譯 窗體或控件及其子控件
/// </summary>
/// <param name="control">需要翻譯的控件或窗體</param>
public static void InitLanguage(Control control)
{
SetControlLanguage(control);
// 循環(huán)所有控件
foreach (Control ctrl in control.Controls)
{
InitLanguage(ctrl);
}
//工具欄 或者菜單動(dòng)態(tài)構(gòu)建窗體或者控件的時(shí)候,重新對(duì)子控件進(jìn)行處理
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 標(biāo)題修改
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實(shí)現(xiàn)多語(yǔ)言切換功能的文章就介紹到這了,更多相關(guān)C# winform多語(yǔ)言?xún)?nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Unity技術(shù)手冊(cè)之Toggle切換使用實(shí)例
這篇文章主要為大家介紹了Unity技術(shù)手冊(cè)之Toggle切換使用實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
基于Silverlight DataGrid中無(wú)代碼設(shè)置開(kāi)始與結(jié)束日期DatePicker的實(shí)現(xiàn)方法
本篇文章是對(duì)Silverlight DataGrid中無(wú)代碼設(shè)置開(kāi)始與結(jié)束日期DatePicker的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C#如何對(duì)多線(xiàn)程、多任務(wù)管理(demo)
這篇文章主要通過(guò)一個(gè)小demo介紹了C#如何對(duì)多線(xiàn)程、多任務(wù)管理,需要的朋友可以參考下2015-07-07
C# 實(shí)現(xiàn)階乘 (遞歸,非遞歸) 實(shí)現(xiàn)代碼
C# 實(shí)現(xiàn)階乘 (遞歸,非遞歸) 實(shí)現(xiàn)代碼,需要的朋友可以參考一下2013-05-05
C#通過(guò)Win32API設(shè)置客戶(hù)端系統(tǒng)時(shí)間的方法詳解
在日常工作中,有時(shí)可能會(huì)需要獲取或修改客戶(hù)端電腦的系統(tǒng)時(shí)間,比如軟件設(shè)置了Licence有效期,本文以一個(gè)簡(jiǎn)單的小例子,簡(jiǎn)述如何通過(guò)C#獲取和設(shè)置客戶(hù)端電腦的系統(tǒng)時(shí)間,僅供學(xué)習(xí)分享使用,如有不足之處,還請(qǐng)指正,需要的朋友可以參考下2024-06-06
C#實(shí)現(xiàn)的封裝CURD到SqlHelper類(lèi)用法簡(jiǎn)單分析
這篇文章主要介紹了C#實(shí)現(xiàn)的封裝CURD到SqlHelper類(lèi)用法,涉及數(shù)據(jù)庫(kù)相關(guān)配置方法及SqlHelper類(lèi)的簡(jiǎn)單使用技巧,代碼中包含了較為詳盡的注釋便于理解,需要的朋友可以參考下2017-11-11
C#開(kāi)發(fā)windows服務(wù)實(shí)現(xiàn)自動(dòng)從FTP服務(wù)器下載文件
這篇文章主要為大家詳細(xì)介紹了C#開(kāi)發(fā)windows服務(wù)實(shí)現(xiàn)自動(dòng)從FTP服務(wù)器下載文件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03
DevExpress實(shí)現(xiàn)GridControl根據(jù)列選中一行
這篇文章主要介紹了DevExpress實(shí)現(xiàn)GridControl根據(jù)列選中一行,比較實(shí)用的功能,需要的朋友可以參考下2014-08-08

