C# WinForm国际化实现的简单方法
发布时间:2023-08-07 04:29:34
软件行业发展到今天,国际化问题一直都占据非常重要的位置,而且应该越来越被重视。对于开发人员而言,在编写程序之前,国际化问题是首先要考虑的一个问题,也许有时候这个问题已经在设计者的考虑范围之内,但终归要开发人员去做实现的。因此,如何实现国际化,是开发人员必须掌握的一项基本技能。
今天,这里要讲的就是,在利用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);
}
/// <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
归结起来,要在C#的WinForm中实现国际化,至少需要做好以下几点:
(1)准备所需资源文件(如本文中提到的英文和中文资源文件);
(2)引入命名空间(包括:System.Reflection、System.Resources、System.Threading和System.Globalization);
(3)实例化资源管理器(即ResourceManager);
(4)设置当前进程的语言区域;
(5)通过资源管理器从指定的资源文件中获取所需值。
通过上述的方法即可简单实现国际化。


猜你喜欢
- 1 仿射变换仿射变换:一种二维坐标到二维坐标的线性变换,它保持二维图像的平直性与平行性,即变换后直线依然是直线,平行的线依然平行。packa
- CollectionUtils.isNotEmpty()不存在问题org.apache.commons.collections.Collec
- 一、简介在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,
- 一、安装JDK1.卸载旧版本或者系统自带的JDK(1)列出所有已安装的JDKrpm -qa | grep jdk(2)卸载不需要的JDKyu
- 本文实例讲述了C#中list用法。分享给大家供大家参考,具体如下:protected void Page_Load(object sende
- 这个功能一共有两部分组成,第一部分是窗体代码,另外的一部分是一个辅助方法。直接贴出代码,以供大家参考:using System;using
- 本文实例为大家分享了Java实现聊天室界面的具体代码,供大家参考,具体内容如下服务器端:package Server; impor
- 前言:仿微信通讯录搜索功能,通过汉字或拼音首字母找到匹配的联系人并显示匹配的位置一:先看效果图字母索引搜索匹配二:功能分析1:汉字转拼音通讯
- 前言在实际开发当中,对于某些关键业务,我们通常需要记录该操作的内容,一个操作调一次记录方法,每次还得去收集参数等等,会造成大量代码重复。 我
- 前言今天我们继续聊聊在SprinBoot中如何集成参数校验Validator,以及参数校验的高阶技巧(自定义校验,分组校验)。&ld
- 背景公司的开发框架集成了附件本地存储,阿里云,华为云等,现项目有要求附件存储与应用部署环境不能是同一台服务器,也不能使用云存储,经过技术选型
- 本文实例为大家分享了Java网络编程TCP程序设计的具体代码,供大家参考,具体内容如下[1] TCP编程的主要步骤客户端(client):1
- 一款书籍阅读器,需要以下功能才能说的上比较完整:文字页面展示,即书页;页面之间的跳转动画,即翻页动作;能够在每一页上记录阅读进度,即书签;能
- 本文实例讲述了C#创建自签名认证文件的方法。分享给大家供大家参考。具体如下:using System;using System.Runtim
- 开发环境:IntelliJ IDEA 2019.2.2Spring Boot版本:2.1.8一、发布REST服务1、IDEA新建一个名称为r
- 方式一 通过Map.keySet使用iterator遍历@Testpublic void testHashMap1() { Map<I
- android电话管理器(TelephonyManger)实例:TelephonyManger是管理电话状态、网络信息的服务类。添加权限:&
- 安卓应用闪退后总会出现一个“抱歉,App已经停止运行”的弹窗,这样的用户体验并不好。很多大厂的App都去除了这个弹窗,因此本文主要介绍如何去
- 前言:回顾上一节服务器配置的内容,我们已经可以自己完成公众号服务器的配置。配置完成之后,我们就可以通过调用的方式,完成对消息管理的处理。当用
- String replace replaceFirst repaceAll区别replace(char oldChar, char newC