狠狠色丁香婷婷综合尤物/久久精品综合一区二区三区/中国有色金属学报/国产日韩欧美在线观看 - 国产一区二区三区四区五区tv

LOGO OA教程 ERP教程 模切知識(shí)交流 PMS教程 CRM教程 開(kāi)發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

C#-WinForm中英文實(shí)現(xiàn)國(guó)際化實(shí)現(xiàn)的方法

admin
2021年1月28日 14:49 本文熱度 3607

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

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

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";
            }

            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 = "";

            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.";
            }

            return strCurLanguage;
        }
    }
}

在Form1中的代碼如下:

/**
 * 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


歸結(jié)起來(lái),要在C#的WinForm中實(shí)現(xiàn)國(guó)際化,至少需要做好以下幾點(diǎn):
(1)準(zhǔn)備所需資源文件(如本文中提到的英文和中文資源文件);
(2)引入命名空間(包括:System.Reflection、System.Resources、System.Threading和System.Globalization);
(3)實(shí)例化資源管理器(即ResourceManager);
(4)設(shè)置當(dāng)前進(jìn)程的語(yǔ)言區(qū)域;
(5)通過(guò)資源管理器從指定的資源文件中獲取所需值。
通過(guò)上述的方法即可簡(jiǎn)單實(shí)現(xiàn)國(guó)際化。


該文章在 2021/1/28 14:49:56 編輯過(guò)
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點(diǎn)晴ERP是一款針對(duì)中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國(guó)內(nèi)大量中小企業(yè)的青睞。
點(diǎn)晴PMS碼頭管理系統(tǒng)主要針對(duì)港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場(chǎng)、車隊(duì)、財(cái)務(wù)費(fèi)用、相關(guān)報(bào)表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點(diǎn),圍繞調(diào)度、堆場(chǎng)作業(yè)而開(kāi)發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點(diǎn)晴WMS倉(cāng)儲(chǔ)管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購(gòu)管理,倉(cāng)儲(chǔ)管理,倉(cāng)庫(kù)管理,保質(zhì)期管理,貨位管理,庫(kù)位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號(hào)管理軟件。
點(diǎn)晴免費(fèi)OA是一款軟件和通用服務(wù)都免費(fèi),不限功能、不限時(shí)間、不限用戶的免費(fèi)OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved