C# WinForm 中英文實現, 國際化實現的簡單方法

來源:http://www.jb51.net/article/45675.htm,今天看到了借鑑過了,保存一下,下次開發直接用嘻嘻ui

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

using System.Reflection;
using System.Resources;
using System.Threading;
using System.Globalization;spa

namespace GlobalizationTest
{
    class ResourceCulture
    {
        /// <summary>
        /// Set current culture by name
        /// </summary>
        /// <param name="name">name</param>
        public static void SetCurrentCulture(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                name = "en-US";
            }.net

            Thread.CurrentThread.CurrentCulture = new CultureInfo(name);
        }設計

        /// <summary>
        /// Get string by id
        /// </summary>
        /// <param name="id">id</param>
        /// <returns>current language string</returns>
        public static string GetString(string id)
        {
            string strCurLanguage = "";orm

            try
            {
                ResourceManager rm = new ResourceManager("GlobalizationTest.Resource", Assembly.GetExecutingAssembly());
                CultureInfo ci = Thread.CurrentThread.CurrentCulture;
                strCurLanguage = rm.GetString(id, ci);
            }
            catch
            {
                strCurLanguage = "No id:" + id + ", please add.";
            }htm

            return strCurLanguage;
        }
    }
}進程

在Form1中的代碼以下:ci

/**
 * This project is just a example to show how to do the globalization in C# winform.
 * You and rebuild and/or modify it by yourself if you want.
 * Specially, this project was created in Visual Studio 2010.
 *
 * Project Name : GlobalizationTest
 * Create Date  : April 29th, 2010
 * */資源

using System;
using System.Windows.Forms;

namespace GlobalizationTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Set the resource culture
        /// </summary>
        private void SetResourceCulture()
        {
            // Set the form title text
            this.Text = ResourceCulture.GetString("Form1_frmText");

            // Set the groupbox text
            this.gbLanguageView.Text = ResourceCulture.GetString("Form1_gbLanguageViewText");
            this.gbLanguageSelection.Text = ResourceCulture.GetString("Form1_gbLanguageSelectionText");

            // Set the label text
            this.lblCurLanguageText.Text = ResourceCulture.GetString("Form1_lblCurLanguageText");
            this.lblNameText.Text = ResourceCulture.GetString("Form1_lblNameText");
            this.lblPhoneText.Text = ResourceCulture.GetString("Form1_lblPhoneText");   

            // Set the button text
            this.btnMsgShow.Text = ResourceCulture.GetString("Form1_btnMsgShowText");

            // Set radiobutton text
            this.rbEnglish.Text = ResourceCulture.GetString("Language_EnglishText");
            this.rbChinese.Text = ResourceCulture.GetString("Language_ChineseText");

            // Set the current language text
            if (rbEnglish.Checked)
            {
                this.lblCurLanguage.Text = ResourceCulture.GetString("Language_EnglishText");
            }
            else if (rbChinese.Checked)
            {
                this.lblCurLanguage.Text = ResourceCulture.GetString("Language_ChineseText");
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Set the default language
            ResourceCulture.SetCurrentCulture("en-US");

            this.SetResourceCulture();
        }

        private void btnMsgShow_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(txtName.Text))
            {
                MessageBox.Show(ResourceCulture.GetString("Form1_msgbox_nameText"), ResourceCulture.GetString("Form1_msgbox_TitleText"),
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            if (string.IsNullOrEmpty(txtPhone.Text))
            {
                MessageBox.Show(ResourceCulture.GetString("Form1_msgbox_phoneText"), ResourceCulture.GetString("Form1_msgbox_TitleText"),
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            MessageBox.Show(ResourceCulture.GetString("Form1_msgbox_InfoText") + txtName.Text + ", " + txtPhone.Text,
                ResourceCulture.GetString("Form1_msgbox_TitleText"), MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void rbEnglish_CheckedChanged(object sender, EventArgs e)
        {
            ResourceCulture.SetCurrentCulture("en-US");
            this.SetResourceCulture();
        }

        private void rbChinese_CheckedChanged(object sender, EventArgs e)
        {
            ResourceCulture.SetCurrentCulture("zh-CN");
            this.SetResourceCulture();
        }
    }
}

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

圖1

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

相關文章
相關標籤/搜索