C# Winform 国际化

时间:2023-03-09 19:37:27
C# Winform 国际化

1、在Form的language属性选择中文,来制作中文界面

C# Winform 国际化

保存后,设置界面标题会变成如下所示,并且会出现一个zh-CN的资源文件,打开resx文件可看到相应内容

C# Winform 国际化

C# Winform 国际化

C# Winform 国际化

2、将Form的language属性改为英文,其余步骤如第1步,即可生成英文资源文件

C# Winform 国际化

3、添加代码完成切换

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading; namespace LanguageDemo
{
public partial class Form1 : Form
{ ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1));
// 遍历控件,并根据资源文件替换相应属性
private void ApplyResource()
{
foreach (Control ctl in this.Controls)
{
resources.ApplyResources(ctl, ctl.Name);
}
this.ResumeLayout(false);
this.PerformLayout();
resources.ApplyResources(this, "$this");
} public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
string s = System.Globalization.CultureInfo.InstalledUICulture.Name; //获取当前系统的语言
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(s);
ApplyResource();
} private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == )
{
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-CN");
}
else
{
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-GB");
}
ApplyResource();
}
}
}

原文:https://blog.****.net/jack____wen/article/details/79614712