方案一。使用.resources資源文件
先給張我們結果圖吧
1.新建2個txt的文本文件
中文和英文各一個,把需要替換的句子寫到里面去!~。格式如下,
000=輸入名字 001=輸入密碼 002=輸入郵箱 003=提交 004=重置 005=英文 006=漢語
然后,進行保存。這里需要特別的注意,保存的格式是:
文件名+“.語言區域性”
比如常見的 中文是zh-CN,英文是en-US
本文的是:resource.zh-CN.txt和resource.en-US.txt
具體的“語言區域性”可以在這里進行查找:猛擊查看語言區域性啊
2.生成相應的資源文件
使用語言VS資源生成工具(Resource generator (Resgen)) 進行生成資源文件。打開VS的命令窗口 如下圖:
語法如下:resgen +文件名
比如 我現在使用:resgen resource.zh-CN.txt
生成之后,我們能得到2個資源文件,如下所示:
現在,我們把這2個文件放到debug目錄下面的Resource文件中
3.編碼工作
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Resources; using System.Globalization; using System.Threading; namespace MyMutilLanguage1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); this.ApplyResource();//默認設置 } private string strResourcePath = Application.StartupPath + "//Resource";//尋找放資源文件的路徑 private string defaultCulture = "zh-CN";//默認使用中文 private static ResourceManager rm; public void ApplyResource() { this.SetCulture(); this.SetResource(); this.SetUIChange(); } /// <summary> /// 設置區域語言設置 /// </summary> private void SetCulture() { CultureInfo culture = new CultureInfo(defaultCulture); Thread.CurrentThread.CurrentCulture = culture; Thread.CurrentThread.CurrentUICulture = culture; } private void SetResource() { rm = ResourceManager.CreateFileBasedResourceManager("resource", strResourcePath, null); //可以使用 ResourceManager 類在運行時檢索“嵌入的資源”(即已經編譯到應用程序或類庫中的資源)。ResourceManager 類的每個實例都與一個程序集關聯並且管理對嵌入到該程序集中的資源的檢索。 //參考:Tinysun的文章--ResourceManager使用 http://www.blogjava.net/tinysun/archive/2009/03/30/262969.html } private void SetUIChange() { //通過rm.GetString獲取 label1.Text = rm.GetString("000"); label2.Text = rm.GetString("001"); label3.Text = rm.GetString("002"); button1.Text = rm.GetString("003"); button2.Text = rm.GetString("004"); radioButton1.Text = rm.GetString("005"); radioButton2.Text = rm.GetString("006"); } private void radioButton1_CheckedChanged(object sender, EventArgs e) { defaultCulture = "en-US"; this.ApplyResource(); } private void radioButton2_CheckedChanged(object sender, EventArgs e) { defaultCulture = "zh-CN"; this.ApplyResource(); } } }
代碼都特別好理解。
結果如下:
中文版:
英文版:
好了,就這么簡單。
方案二。使用Localizable實現多語言
這種方法也是利用資源文件,但是方法更簡單,但是擴展性不太好。
1.在Form界面修改中英文字體
更改Form1屬性的Language和Localizable屬性。(因為Form1 需要設置中英文切換,所以對它進行更改)。
Language==>Chinese(Simplified)
Localizable==>True
這個時候,你需要把你界面上空間名字更改成中文字,然后save,會在Form1的目錄自動生成一個文件:Form1.zh-Hans.resx。
對英文的處理也是這樣,把
Language==>English (United States)
Localizable==>True
這樣,就會在Form的目錄下面生成2個資源文件:
2.編碼工作
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; using System.Resources; using System.Globalization; namespace MyMutilLanguage2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); this.ApplyResource(); } private string defaultCulture = "zh-CN";//默認使用中文 private static ComponentResourceManager resource; public void ApplyResource() { this.SetCulture(); this.SetResource(); } /// <summary> /// 設置區域語言設置 /// </summary> private void SetCulture() { CultureInfo culture = new CultureInfo(defaultCulture); Thread.CurrentThread.CurrentCulture = culture; Thread.CurrentThread.CurrentUICulture = culture; } private void SetResource() { resource = new ComponentResourceManager(typeof(Form1)); resource.ApplyResources(this, "$this"); foreach (Control c in this.Controls) resource.ApplyResources(c, c.Name); } private void radioButton1_CheckedChanged(object sender, EventArgs e) { defaultCulture = "en-US"; this.ApplyResource(); } private void radioButton2_CheckedChanged(object sender, EventArgs e) { defaultCulture = "zh-CN"; this.ApplyResource(); } } }
代碼改變不算太大,最重要的就是:
resource = new ComponentResourceManager(typeof(Form1)); resource.ApplyResources(this, "$this"); foreach (Control c in this.Controls) resource.ApplyResources(c, c.Name);
ComponetResourceManager也是訪問資源文件。
3.結果和上面的結果一樣,我就不截圖了。
三。參考資料
1.Globalization of Windows Applications in 20 Minutes Using C#
2.Winform中多國語言窗體的設計以及.NET中資源文件的使用
3.