实现多语言的方法可能有使用资源文件,或者配置xml两种方法吧。
没时间研究过多,学习了一下使用资源文件的方法,成功了。


在.net2.0 中,m$ 为我们提供了一种简单方便的方法, 使用资源文件

1.新建一个 Winform. 应用程序, 新建一 Form. ,名为 Form1,添加一个菜单一个按钮。如图

2. 设置 Form1 的 Localizable 属性为 true, 设置该属性后,.net 将根据不同的语言,为应用程序生成不同的资源文件
3.设置各个控件的文本(系统默认语言下)
4.更改 Form1 的 Language 属性为想要支持的另一种语言,此例中我们选用 English
5.重新设置各个控件的文本
注:此时.net 将为 Form1 生成另一个资源文件,在本例中名为 Form1.en.resx
当你需要添加新的控件时,需要切换到default语言。
6. 如果有其它的语言要设置,请重复第4,第5步
7.编写代码 (需要消息框多语言支持的话,就用form做消息框吧。同时也做成多语言支持。)

<!--Code highlighting produced by Actipro CodeHighlighter (freeware) using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Threading;
using System.Globalization;

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

private void button1_Click( object sender, EventArgs e)
{
Msg msg
= new Msg();
msg.ShowDialog();
}

private void 退出ToolStripMenuItem_Click( object sender, EventArgs e)
{
Application.Exit();
}

private void 中文ToolStripMenuItem_Click( object sender, EventArgs e)
{
// 更改当前线程的 CultureInfo
// zh-CN 为中文,更多的关于 Culture 的字符串请查 MSDN
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo( " zh-CN " );
// 对当前窗体应用更改后的资源
ApplyResource();
}

private void 英文ToolStripMenuItem_Click( object sender, EventArgs e)
{
// 更改当前线程的 CultureInfo
// en 为英文,更多的关于 Culture 的字符串请查 MSDN
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo( " en " );
// 对当前窗体应用更改后的资源
ApplyResource();
}
///
/// 应用资源
/// ApplyResources 的第一个参数为要设置的控件
/// 第二个参数为在资源文件中的ID,默认为控件的名称
///
private void ApplyResource()
{
System.ComponentModel.ComponentResourceManager res
= new ComponentResourceManager( typeof (Form1));
foreach (Control ctl in Controls)
{
res.ApplyResources(ctl, ctl.Name);
}

// 菜单
foreach (ToolStripMenuItem item in this .menuStrip1.Items)
{
res.ApplyResources(item, item.Name);
foreach (ToolStripMenuItem subItem in item.DropDownItems)
{
res.ApplyResources(subItem, subItem.Name);
}
}

// Caption
res.ApplyResources( this , " $this " );
}
}
}

判断操作系统语言的方法:

<!--Code highlighting produced by Actipro CodeHighlighter (freeware) private void Form1_Load( object sender, EventArgs e)
{
// 不需要判断操作系统的语言,使用资源文件会自动选择。
// if (System.Globalization.CultureInfo.InstalledUICulture.Name == "zh-CN")
// {
// Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("zh-CN");
// // 对当前窗体应用更改后的资源
// ApplyResource();
// }
// else
// {
// Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en");
// // 对当前窗体应用更改后的资源
// ApplyResource();
// }
}

源码下载: http://files.cnblogs.com/greatverve/GlobalResource.rar

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12639172/viewspace-676735/,如需转载,请注明出处,否则将追究法律责任。

广播电视节目制作经营许可证(京) 字第1234号 中国互联网协会会员