【30分钟学习】二种简单实用的方法实现多语言解决方案(源码在附件)
2013-08-02 15:46 随风浪迹天涯 阅读(2071) 评论(4) 编辑 收藏 举报方案一。使用.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.
出处:http://www.cnblogs.com/damonlan
Q Q:*********
E_mail:Damon_lan@163.com or Dongcai.lan@hp.com
本博文欢迎大家浏览和转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,在『参考』的文章中,我会表明参考的文章来源,尊重他人版权。若您发现我侵犯了您的版权,请及时与我联系。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探