【30分钟学习】二种简单实用的方法实现多语言解决方案(源码在附件)

来源:互联网 发布:天天聚财网网络借贷 编辑:程序博客网 时间:2024/05/21 09:16

方案一。使用.resources资源文件

先给张我们结果图吧

1.新建2个txt的文本文件

中文和英文各一个,把需要替换的句子写到里面去!~。格式如下,

000=输入名字001=输入密码002=输入邮箱003=提交004=重置005=英文006=汉语

然后,进行保存。这里需要特别的注意,保存的格式是:

文件名+“.语言区域性”

比如常见的 中文是zh-CN,英文是en-US

本文的是:resource.zh-CN.txt和resource.en-US.txt

具体的“语言区域性”可以在这里进行查找:猛击查看语言区域性啊

2.生成相应的资源文件

使用语言VS资源生成工具(Resource generator (Resgen)) 进行生成资源文件。打开VS的命令窗口 如下图:

语法如下:resgen +文件名

比如 我现在使用:resgen resource.zh-CN.txt

生成之后,我们能得到2个资源文件,如下所示:

现在,我们把这2个文件放到debug目录下面的Resource文件中

3.编码工作

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Resources;using System.Globalization;using System.Threading;namespace MyMutilLanguage1{  public partial class Form1 : Form  {    public Form1()    {      InitializeComponent();      this.ApplyResource();//默认设置    }    private string strResourcePath = Application.StartupPath + "//Resource";//寻找放资源文件的路径    private string defaultCulture = "zh-CN";//默认使用中文    private static ResourceManager rm;    public void ApplyResource()    {      this.SetCulture();      this.SetResource();      this.SetUIChange();    }    /// <summary>    /// 设置区域语言设置    /// </summary>    private void SetCulture()    {      CultureInfo culture = new CultureInfo(defaultCulture);      Thread.CurrentThread.CurrentCulture = culture;      Thread.CurrentThread.CurrentUICulture = culture;    }    private void SetResource()    {      rm = ResourceManager.CreateFileBasedResourceManager("resource", strResourcePath, null);      //可以使用 ResourceManager 类在运行时检索“嵌入的资源”(即已经编译到应用程序或类库中的资源)。ResourceManager 类的每个实例都与一个程序集关联并且管理对嵌入到该程序集中的资源的检索。      //参考:Tinysun的文章--ResourceManager使用  http://www.blogjava.net/tinysun/archive/2009/03/30/262969.html    }    private void SetUIChange()    {      //通过rm.GetString获取      label1.Text = rm.GetString("000");      label2.Text = rm.GetString("001");      label3.Text = rm.GetString("002");      button1.Text = rm.GetString("003");      button2.Text = rm.GetString("004");      radioButton1.Text = rm.GetString("005");      radioButton2.Text = rm.GetString("006");    }    private void radioButton1_CheckedChanged(object sender, EventArgs e)    {      defaultCulture = "en-US";      this.ApplyResource();    }    private void radioButton2_CheckedChanged(object sender, EventArgs e)    {      defaultCulture = "zh-CN";      this.ApplyResource();    }  }}

代码都特别好理解。

结果如下:

中文版:

英文版:

好了,就这么简单。

方案二。使用Localizable实现多语言

这种方法也是利用资源文件,但是方法更简单,但是扩展性不太好。

1.在Form界面修改中英文字体

更改Form1属性的Language和Localizable属性。(因为Form1 需要设置中英文切换,所以对它进行更改)。

Language==>Chinese(Simplified)

Localizable==>True

这个时候,你需要把你界面上空间名字更改成中文字,然后save,会在Form1的目录自动生成一个文件:Form1.zh-Hans.resx。

对英文的处理也是这样,把

Language==>English (United States)

Localizable==>True

这样,就会在Form的目录下面生成2个资源文件:

2.编码工作

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Threading;using System.Resources;using System.Globalization;namespace MyMutilLanguage2{  public partial class Form1 : Form  {    public Form1()    {      InitializeComponent();      this.ApplyResource();    }    private string defaultCulture = "zh-CN";//默认使用中文    private static ComponentResourceManager resource;    public void ApplyResource()    {      this.SetCulture();      this.SetResource();    }    /// <summary>    /// 设置区域语言设置    /// </summary>    private void SetCulture()    {      CultureInfo culture = new CultureInfo(defaultCulture);      Thread.CurrentThread.CurrentCulture = culture;      Thread.CurrentThread.CurrentUICulture = culture;    }    private void SetResource()    {      resource = new ComponentResourceManager(typeof(Form1));      resource.ApplyResources(this, "$this");      foreach (Control c in this.Controls)        resource.ApplyResources(c, c.Name);    }    private void radioButton1_CheckedChanged(object sender, EventArgs e)    {      defaultCulture = "en-US";      this.ApplyResource();    }    private void radioButton2_CheckedChanged(object sender, EventArgs e)    {      defaultCulture = "zh-CN";      this.ApplyResource();    }  }}

代码改变不算太大,最重要的就是:

resource = new ComponentResourceManager(typeof(Form1));      resource.ApplyResources(this, "$this");      foreach (Control c in this.Controls)        resource.ApplyResources(c, c.Name);

ComponetResourceManager也是访问资源文件。

3.结果和上面的结果一样,我就不截图了。

三。参考资料

1.Globalization of Windows Applications in 20 Minutes Using C#

2.Winform中多国语言窗体的设计以及.NET中资源文件的使用 

3.

源码下载 源码下载


原创粉丝点击