软件行业发展到今天,国际化问题一直都占据非常重要的位置,而且应该越来越被重视。对于开发人员而言,在编写程序之前,国际化问题是首先要考虑的一个问题,也许有时候这个问题已经在设计者的考虑范围之内,但终归要开发人员去做实现的。因此,如何实现国际化,是开发人员必须掌握的一项基本技能。
    今天,这里要讲的就是,在利用C#进行WinForm开发时,国际化是怎么实现的。鉴于时间及篇幅关系,这里仅仅介绍一种简单的国际化实现方法,可能这里提到的方法已经有非常多人提到过,但笔者还是不厌其烦地介绍一下。
    要在C#中实现国际化,需要相关资源文件,比如要在一个软件中支持英文、中文两种语言,那么就必须有这两种语言的资源文件,这在C#中可以采用资源文件(后缀名为.resx)来实现,我们不妨定义英文资源文件名称为Resource.en-US,中文资源文件名称为Resource.zh-CN,两种资源文件所涉及的ID都应该是一样的(这对于其他更多的资源文件均是一样的),只不过是展示的名称不同罢了。
    有了这两种资源文件,接下来就要考虑如何做的问题了。为了适应多处使用的情形,这里笔者单独编写了一个类ResourceCulture,该类包含了一些静态方法,主要作用是用来设置当前语言及返回当前的语言的相关字符串。该类代码如下:

 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);
} //http://www.cnblogs.com/roucheng/
/// <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.
* http://www.cnblogs.com/roucheng/
* 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


图2

归结起来,要在C#的WinForm中实现国际化,至少需要做好以下几点:
(1)准备所需资源文件(如本文中提到的英文和中文资源文件);
(2)引入命名空间(包括:System.Reflection、System.Resources、System.Threading和System.Globalization);
(3)实例化资源管理器(即ResourceManager);
(4)设置当前进程的语言区域;
(5)通过资源管理器从指定的资源文件中获取所需值。
    通过上述的方法即可简单实现国际化。

C# WinForm国际化的简单实现的更多相关文章

  1. 用C#Winform写个简单的批量清空文件内容和删除文件的小工具

    用C#Winform写个简单的批量清空文件内容和删除文件的小工具 本文介绍这个简单得不能再简单的小项目.做这个项目,有以下目的. 1 当然是做个能用的工具 2 学习使用Github 关于用VS2013 ...

  2. WinForm 国际化的一些问题

    国际化 我之前 WinForm 国际化都是凑一些代码搞起(请看文后 Reference). 最近发现还有个官方国际化方法: 首先设置 Form 的 Localizable 属性为 true 选择 Fo ...

  3. C#-用Winform制作一个简单的密码管理工具

    为什么要做? 首先是为了练习一下c#. 想必大家都有过记不起某个平台的账号密码的经历,那种感受着实令人抓狂.那这么多账号密码根本记不住!我之前用python写过一个超级简单(连账号信息都写在代码里那种 ...

  4. winform 下log4net简单应用示例及“缺少log4net引用”的处理方案

    1.添加应用log4net.dll 2.新增log4net.config文件,文件内容如下 <?xml version="1.0" encoding="utf-8& ...

  5. WinForm 国际化开发一例

    1.新建一个WinForm程序(在中文版的Windows下),如下,添加1个Button和1个Label 设置Form1的localizable属性为True(Form1的properties里的De ...

  6. WInform 创建一个简单的WPF应用

    (一)创建一个简单的WPF应用 首先,在这里我要说明的是:这里的例子,都是通过控制台程序来创建WPF应用,而非使用现成的WPF模版.因为WPF模版封装了创建WPF应用所需要的各种基本元素,并不利于我们 ...

  7. ~.NET下国际化i18n简单示例

    因业务需要,后台站点需要出一个国际化的解决方案,偷懒后用微软自带资源文件暂时解决.废话不多说,进入正题. 第一步:在项目菜单下选择添加国际化资源文件夹. 第二部:添加完毕之后,添加各语言版本下的资源信 ...

  8. 1.C#WinForm基础制作简单计算器

    利用c#语言编写简单计算器: 核心知识点: MessageBox.Show(Convert.ToString(comboBox1.SelectedIndex));//下拉序号 MessageBox.S ...

  9. WinForm多线程编程简单Demo

    需要搭建一个可以监控报告生成的CS(WinForm)工具,即CS不断Run,执行获取数据生成报告,经过研究和实践,选择了使用"WinForm多线程编程"的解决方案.当然参考了园中相 ...

随机推荐

  1. 深入浅出JavaScript之原型链&继承

    Javascript语言的继承机制,它没有"子类"和"父类"的概念,也没有"类"(class)和"实例"(instanc ...

  2. .NET平台和C#编程的总结

    第一章   简单认识.NET框架    (1)首先我们得知道 .NET框架具有两个主要组件:公共语言进行时CLR(Common Language Runtime)和框架类库FCL(Framework ...

  3. c++ pair 使用

    1. 包含头文件: #include <utility> 2. pair 的操作: pair<T1,T2> p; pair<T1,T2> p(v1,v2); pai ...

  4. 在开源中国(oschina)git中新建标签(tags)

    我今天提交代码到主干上面,本来想打个标签(tags)的. 因为我以前新建过标签(tags),但是我现在新建的时候不知道入库在哪了.怎么找也找不到了. 从网上找资料也没有,找客服没有人理我,看到一个交流 ...

  5. join Linq

    List<Publisher> Publishers = new List<Publisher>(); Publisher publish1 = new Publisher() ...

  6. bootstrap

    访问Bootstrap中文网,下载bootstrap中文文档,选择用于生产环境的bootstrap. 在官网使用ctrl+f查找想要的内容. 这里记一下Visual Studio Code软件的用法: ...

  7. Hbase安装和错误

    集群规划情况: djt1 active Hmaster djt2 standby Hmaster djt3 HRegionServer 搭建步骤: 第一步:配置conf/regionservers d ...

  8. Mono 3.8发布:性能进一步改进,可伸缩性提升

    9月4日,Mono 3.8.0发布了.该版本的运行时带来了一些性能和可伸缩性方面的改进,同时完成了向Windows平台的移植. Mono遵循Gnome和Linux内核的版本编号策略,这意味着3.8是3 ...

  9. Building the Testing Pipeline

    This essay is a part of my knowledge sharing session slides which are shared for development and qua ...

  10. rsync 笔记之 list

    通过 rsync --list-only 可以列出可用的 文件/目录或者 module 下面两者的含义是完全不同的: rsync --list-only root@192.168.4.140: 使用系 ...