如图,将Form的Localizable设为True,然后选择需要的Language比如英语
这时更改窗体控件的文本,Form下会出现.en-GB.resx资源文件,再更改Form的Language属性为中文简体,更改窗体控件的文本,Form下会出现.zn-CN.resx资源文件
修改的控件的文本存在.en-GB.resx和.zn-CN.resx中,如下图:
如何在程序启动时可以先设置语言,如下:
static void Main()
{
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(false)
//设置语言环境为英文
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-GB")
Application.Run(new Form1())
}
如何在程序运行中切换语言,如下
private void englishToolStripMenuItem_Click(object sender, EventArgs e)
{
ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1));
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-GB");
ApplyResource();
}
private void chineseToolStripMenuItem_Click(object sender, EventArgs e)
{
ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1));
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-CN");
ApplyResource();
}
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);
}
}
res.ApplyResources(this, "$this");
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
程序运行效果如下:
初始
点击Chinese
切换语言后
深一步理解
注意到两种语言下的窗体控件布局不一样,这是为何?
在Localizable为False时,.Designer.cs的InitializeCopmponent()方法如下图:
而当Localizable为True时,.Designer.cs的InitializeCopmponent()方法如下图:
可见,设置属性的代码没了,多了下图红线圈起来的代码。
控件的其他如字体大小,位置等属性也在.en-GB.resx和.zn-CN.resx中,注意红圈部分选为其他就可看见
以前控件的属性写在.Designer.cs,现在是用ApplyResources方法去加载资源文件的内容来设置控件。