zoukankan      html  css  js  c++  java
  • 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 name
    12         /// </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://www.cnblogs.com/roucheng/
    25         /// <summary>
    26         /// Get string by id
    27         /// </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             try
    35             {
    36                 ResourceManager rm = new ResourceManager("GlobalizationTest.Resource", Assembly.GetExecutingAssembly());
    37                 CultureInfo ci = Thread.CurrentThread.CurrentCulture;
    38                 strCurLanguage = rm.GetString(id, ci);
    39             }
    40             catch
    41             {
    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://www.cnblogs.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 GlobalizationTest
    14 {
    15     public partial class Form1 : Form
    16     {
    17         public Form1()
    18         {
    19             InitializeComponent();
    20         }
    21 
    22         /// <summary>
    23         /// Set the resource culture
    24         /// </summary>
    25         private void SetResourceCulture()
    26         {
    27             // Set the form title text
    28             this.Text = ResourceCulture.GetString("Form1_frmText");
    29 
    30             // Set the groupbox text
    31             this.gbLanguageView.Text = ResourceCulture.GetString("Form1_gbLanguageViewText");
    32             this.gbLanguageSelection.Text = ResourceCulture.GetString("Form1_gbLanguageSelectionText");
    33 
    34             // Set the label text
    35             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 text
    40             this.btnMsgShow.Text = ResourceCulture.GetString("Form1_btnMsgShowText");
    41 
    42             // Set radiobutton text
    43             this.rbEnglish.Text = ResourceCulture.GetString("Language_EnglishText");
    44             this.rbChinese.Text = ResourceCulture.GetString("Language_ChineseText");
    45 
    46             // Set the current language text
    47             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 language
    60             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)通过资源管理器从指定的资源文件中获取所需值。
        通过上述的方法即可简单实现国际化。

  • 相关阅读:
    USACO 3.3 A Game
    USACO 3.3 Camelot
    USACO 3.3 Shopping Offers
    USACO 3.3 TEXT Eulerian Tour中的Cows on Parade一点理解
    USACO 3.3 Riding the Fences
    USACO 3.2 Magic Squares
    USACO 3.2 Stringsobits
    USACO 3.2 Factorials
    USACO 3.2 Contact
    USACO 3.1 Humble Numbers
  • 原文地址:https://www.cnblogs.com/roucheng/p/3518068.html
Copyright © 2011-2022 走看看