国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C# WinForm國際化的簡單實現

2019-11-17 03:24:27
字體:
來源:轉載
供稿:網友

C# WinForm國際化的簡單實現

軟件行業發展到今天,國際化問題一直都占據非常重要的位置,而且應該越來越被重視。對于開發人員而言,在編寫程序之前,國際化問題是首先要考慮的一個問題,也許有時候這個問題已經在設計者的考慮范圍之內,但終歸要開發人員去做實現的。因此,如何實現國際化,是開發人員必須掌握的一項基本技能。 今天,這里要講的就是,在利用C#進行WinForm開發時,國際化是怎么實現的。鑒于時間及篇幅關系,這里僅僅介紹一種簡單的國際化實現方法,可能這里提到的方法已經有非常多人提到過,但筆者還是不厭其煩地介紹一下。 要在C#中實現國際化,需要相關資源文件,比如要在一個軟件中支持英文、中文兩種語言,那么就必須有這兩種語言的資源文件,這在C#中可以采用資源文件(后綴名為.resx)來實現,我們不妨定義英文資源文件名稱為Resource.en-US,中文資源文件名稱為Resource.zh-CN,兩種資源文件所涉及的ID都應該是一樣的(這對于其他更多的資源文件均是一樣的),只不過是展示的名稱不同罷了。 有了這兩種資源文件,接下來就要考慮如何做的問題了。為了適應多處使用的情形,這里筆者單獨編寫了一個類ResourceCulture,該類包含了一些靜態方法,主要作用是用來設置當前語言及返回當前的語言的相關字符串。該類代碼如下:

 1 using System.Reflection; 2 using System.Resources; 3 using System.Threading; 4 using System.Globalization; 5  6 namespace GlobalizationTest 7 { 8     class ResourceCulture 9     {10         /// <summary>11         /// Set current culture by name12         /// </summary>13         /// <param name="name">name</param>14         public static void SetCurrentCulture(string name)15         {16             if (string.IsNullOrEmpty(name))17             {18                 name = "en-US";19             }20 21             Thread.CurrentThread.CurrentCulture = new CultureInfo(name);22         }23 24          //http://m.survivalescaperooms.com/roucheng/25         /// <summary>26         /// Get string by id27         /// </summary>28         /// <param name="id">id</param>29         /// <returns>current language string</returns>30         public static string GetString(string id)31         {32             string strCurLanguage = "";33 34             try35             {36                 ResourceManager rm = new ResourceManager("GlobalizationTest.Resource", Assembly.GetExecutingAssembly());37                 CultureInfo ci = Thread.CurrentThread.CurrentCulture;38                 strCurLanguage = rm.GetString(id, ci);39             }40             catch41             {42                 strCurLanguage = "No id:" + id + ", please add.";43             }44 45             return strCurLanguage;46         }47     }48 }

在Form1中的代碼如下:

 1 /** 2  * This PRoject is just a example to show how to do the globalization in C# winform. 3  * You and rebuild and/or modify it by yourself if you want. 4  * Specially, this project was created in Visual Studio 2010. 5  *  http://m.survivalescaperooms.com/roucheng/ 6  * Project Name : GlobalizationTest 7  * Create Date  : April 29th, 2010 8  * */ 9 10 using System;11 using System.Windows.Forms;12 13 namespace GlobalizationTest14 {15     public partial class Form1 : Form16     {17         public Form1()18         {19             InitializeComponent();20         }21 22         /// <summary>23         /// Set the resource culture24         /// </summary>25         private void SetResourceCulture()26         {27             // Set the form title text28             this.Text = ResourceCulture.GetString("Form1_frmText");29 30             // Set the groupbox text31             this.gbLanguageView.Text = ResourceCulture.GetString("Form1_gbLanguageViewText");32             this.gbLanguageSelection.Text = ResourceCulture.GetString("Form1_gbLanguageSelectionText");33 34             // Set the label text35             this.lblCurLanguageText.Text = ResourceCulture.GetString("Form1_lblCurLanguageText");36             this.lblNameText.Text = ResourceCulture.GetString("Form1_lblNameText");37             this.lblPhoneText.Text = ResourceCulture.GetString("Form1_lblPhoneText");    38 39             // Set the button text40             this.btnMsgShow.Text = ResourceCulture.GetString("Form1_btnMsgShowText");41 42             // Set radiobutton text43             this.rbEnglish.Text = ResourceCulture.GetString("Language_EnglishText");44             this.rbChinese.Text = ResourceCulture.GetString("Language_ChineseText");45 46             // Set the current language text47             if (rbEnglish.Checked)48             {49                 this.lblCurLanguage.Text = ResourceCulture.GetString("Language_EnglishText");50             }51             else if (rbChinese.Checked)52             {53                 this.lblCurLanguage.Text = ResourceCulture.GetString("Language_ChineseText");54             }55         }56 57         private void Form1_Load(object sender, EventArgs e)58         {59             // Set the default language60             ResourceCulture.SetCurrentCulture("en-US");61 62             this.SetResourceCulture();63         }64 65         private void btnMsgShow_Click(object sender, EventArgs e)66         {67             if(string.IsNullOrEmpty(txtName.Text))68             {69                 MessageBox.Show(ResourceCulture.GetString("Form1_msgbox_nameText"), ResourceCulture.GetString("Form1_msgbox_TitleText"), 70                     MessageBoxButtons.OK, MessageBoxIcon.Exclamation);71                 return;72             }73 74             if (string.IsNullOrEmpty(txtPhone.Text))75             {76                 MessageBox.Show(ResourceCulture.GetString("Form1_msgbox_phoneText"), ResourceCulture.GetString("Form1_msgbox_TitleText"), 77                     MessageBoxButtons.OK, MessageBoxIcon.Exclamation);78                 return;79             }80 81             MessageBox.Show(ResourceCulture.GetString("Form1_msgbox_InfoText") + txtName.Text + ", " + txtPhone.Text,82                 ResourceCulture.GetString("Form1_msgbox_TitleText"), MessageBoxButtons.OK, MessageBoxIcon.Information);83         }84 85         private void rbEnglish_CheckedChanged(object sender, EventArgs e)86         {87             ResourceCulture.SetCurrentCulture("en-US");88             this.SetResourceCulture();89         }90 91         private void rbChinese_CheckedChanged(object sender, EventArgs e)92         {93             ResourceCulture.SetCurrentCulture("zh-CN");94             this.SetResourceCulture();95         }96     }97 }

最終的效果如下圖1和圖2所示:圖1

圖2

歸結起來,要在C#的WinForm中實現國際化,至少需要做好以下幾點:(1)準備所需資源文件(如本文中提到的英文和中文資源文件);(2)引入命名空間(包括:System.Reflection、System.Resources、System.Threading和System.Globalization);(3)實例化資源管理器(即ResourceManager);(4)設置當前進程的語言區域;(5)通過資源管理器從指定的資源文件中獲取所需值。 通過上述的方法即可簡單實現國際化。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 图木舒克市| 含山县| 乌兰县| 石景山区| 潼关县| 邢台市| 康保县| 容城县| 高平市| 桐庐县| 精河县| 长乐市| 德保县| 昌都县| 和平区| 海南省| 南郑县| 易门县| 咸丰县| 五常市| 喜德县| 嘉荫县| 民乐县| 溧阳市| 武宣县| 乌鲁木齐市| 洛宁县| 乐山市| 建水县| 吴旗县| 吴川市| 桂平市| 安阳市| 应用必备| 镇赉县| 尖扎县| 泗洪县| 巴东县| 博白县| 南漳县| 万宁市|