winform使用本地化,中英文切换

article/2025/9/7 22:27:24

在有些软件中,需要中英文切换的功能,甚至其他语言切换的功能,都可以使用winform自带的本地化功能。一共有2种方法。

第一种方法

1.首先建立一个项目,拖几个控件上去,如图所示。

2.点击Form1的属性,设置以下2项

此时,窗体就会变成带有英语的字样

3.这个时候,我们选择窗体界面上的控件,对控件的Text属性,进行英文填写,如图所示

4.如果想要切换到中文模式,也就是我们的默认模式,点击Form1的属性,把语言设置成默认,就是我们一开始的中文模式。如果要增加其他语言模式,重复第3步即可

在此界面上,修改中文模式的字体,如图所示

5.当我们修改完中文(默认)和英文模式后,在项目中,会出现2个文件,带en的就是英文,另一个就是中文。

 当我们分别打开后,也可以在这个里面进行修改

6.回到主界面中,分别写入radioButton的2个事件

 7.代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;namespace WindowsFormsApp3
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void radioButton2_CheckedChanged(object sender, EventArgs e){Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh");//中文是zhComponentResourceManager resources = new ComponentResourceManager(typeof(Form1));resources.ApplyResources(label1, "label1");  //这里的第一个参数界面的控件的Name,第二参数,如果选择的zh,就是Form1.resx文件中label1.Textresources.ApplyResources(button1, "button1");//这里的第一个参数界面的控件的Name,第二参数,如果选择的zh,就是Form1.resx文件中button1.Textresources.ApplyResources(this, "$this");}private void radioButton1_CheckedChanged(object sender, EventArgs e){Thread.CurrentThread.CurrentUICulture = new CultureInfo("en"); //英文是en// Reapplies resources.ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1));resources.ApplyResources(label1, "label1");   //这里的第一个参数界面的控件的Name,第二参数,如果选择的en,就是Form1.en.resx文件中label1.Textresources.ApplyResources(button1, "button1"); //这里的第一个参数界面的控件的Name,第二参数,如果选择的en,就是Form1.en.resx文件中button1.Textresources.ApplyResources(this, "$this");}}
}

8.效果

拓展1

我们也可以使用1个按钮进行切换

界面增加一个按钮,在按钮中写入以下代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;namespace WindowsFormsApp3
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void radioButton2_CheckedChanged(object sender, EventArgs e){Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh");//中文是zhComponentResourceManager resources = new ComponentResourceManager(typeof(Form1));resources.ApplyResources(label1, "label1");  //这里的第一个参数界面的控件的Name,第二参数,如果选择的zh,就是Form1.resx文件中label1.Textresources.ApplyResources(button1, "button1");//这里的第一个参数界面的控件的Name,第二参数,如果选择的zh,就是Form1.resx文件中button1.Textresources.ApplyResources(this, "$this");}private void radioButton1_CheckedChanged(object sender, EventArgs e){Thread.CurrentThread.CurrentUICulture = new CultureInfo("en"); //英文是en// Reapplies resources.ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1));resources.ApplyResources(label1, "label1");   //这里的第一个参数界面的控件的Name,第二参数,如果选择的en,就是Form1.en.resx文件中label1.Textresources.ApplyResources(button1, "button1"); //这里的第一个参数界面的控件的Name,第二参数,如果选择的en,就是Form1.en.resx文件中button1.Textresources.ApplyResources(this, "$this");}private void button2_Click(object sender, EventArgs e){int currentLcid = Thread.CurrentThread.CurrentUICulture.LCID;currentLcid = (currentLcid == 2052) ? 1033 : 2052;//这里的2052是中文   1033是英文Thread.CurrentThread.CurrentUICulture = new CultureInfo(currentLcid); ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1));resources.ApplyResources(label1, "label1");   resources.ApplyResources(button1, "button1"); resources.ApplyResources(this, "$this");}}
}

拓展2

如果界面中,有大量的控件,那么可以写一个循环去设置

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;namespace WindowsFormsApp3
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void radioButton2_CheckedChanged(object sender, EventArgs e){Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh");//中文是zhComponentResourceManager resources = new ComponentResourceManager(typeof(Form1));resources.ApplyResources(label1, "label1");  //这里的第一个参数界面的控件的Name,第二参数,如果选择的zh,就是Form1.resx文件中label1.Textresources.ApplyResources(button1, "button1");//这里的第一个参数界面的控件的Name,第二参数,如果选择的zh,就是Form1.resx文件中button1.Textresources.ApplyResources(this, "$this");}private void radioButton1_CheckedChanged(object sender, EventArgs e){Thread.CurrentThread.CurrentUICulture = new CultureInfo("en"); //英文是en// Reapplies resources.ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1));resources.ApplyResources(label1, "label1");   //这里的第一个参数界面的控件的Name,第二参数,如果选择的en,就是Form1.en.resx文件中label1.Textresources.ApplyResources(button1, "button1"); //这里的第一个参数界面的控件的Name,第二参数,如果选择的en,就是Form1.en.resx文件中button1.Textresources.ApplyResources(this, "$this");}private void button2_Click(object sender, EventArgs e){int currentLcid = Thread.CurrentThread.CurrentUICulture.LCID;currentLcid = (currentLcid == 2052) ? 1033 : 2052;//这里的2052是中文   1033是英文Thread.CurrentThread.CurrentUICulture = new CultureInfo(currentLcid);ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1));//resources.ApplyResources(label1, "label1");//resources.ApplyResources(button1, "button1");//resources.ApplyResources(this, "$this");foreach (Control ct in this.Controls)//循环当前界面所有的控件{resources.ApplyResources(ct, ct.Name);if (ct.HasChildren){resources.ApplyResources(ct, ct.Name);}}}}
}

第二种方法

这个是从全局的视角出发

1.建立一个项目,界面如图,这里我们点击English和中文按钮来切换中英文 

2.右键建立一个Resource文件夹,在Resource文件夹中,建立一个中文资源文件和一个英文资源文件

 3.打开对应的英文资源文件,看到名称和值。值就是对应的英文,名称分为3部分

Form1.button1.Text。

Form1是窗体

button1是窗体里面控件的名称

Text是控件文本

 注意:这里不能错,否则无效,还可以增加其他界面的值,有几个界面就写几个界面,格式要保持一样就行了。中文资源文件也按照英文资源文件一样操作。

修改好后的文件是

 4.此时我们回到主界面中,在2个按钮中增加对应的代码

代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;namespace WindowsFormsApp4
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void button2_Click(object sender, EventArgs e){Thread.CurrentThread.CurrentCulture = new CultureInfo("en");//英文ApplyResource(this);//传入当前的界面}private void button3_Click(object sender, EventArgs e){Thread.CurrentThread.CurrentCulture = new CultureInfo("zh");//中文ApplyResource(this);//传入当前的界面}ComponentResourceManager crm;public void ApplyResource(Control control){switch (Thread.CurrentThread.CurrentCulture.Name){case "en":crm = new ComponentResourceManager(typeof(Resource.Resource_en));break;case "zh":crm = new ComponentResourceManager(typeof(Resource.Resource_zh));break;default:crm = new ComponentResourceManager(typeof(Resource.Resource_zh));break;}applyControl(control.GetType().Name, control);//调用}//递归应用到控件private void applyControl(string topName, Control control){foreach (Control ctl in control.Controls){crm.ApplyResources(ctl, topName + "." + ctl.Name, Thread.CurrentThread.CurrentCulture);if (ctl.HasChildren){applyControl(topName, ctl);}}}}
}

5.效果

拓展

用这个办法,会比上面更加的简单,使用配置文件。

1.上面修改英文资源文件和中文资源文件的方法不变,这里不说了。 

2.在App.config文件中配置如下代码

<?xml version="1.0" encoding="utf-8" ?>
<configuration><startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" /></startup><appSettings><!--en:英文,zh:中文--><add key="CultureInfo" value="en"/></appSettings>
</configuration>

3.在程序的入口处,写入以下代码

代码

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;namespace WindowsFormsApp5
{static class Program{/// <summary>/// 应用程序的主入口点。/// </summary>[STAThread]static void Main(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Thread.CurrentThread.CurrentUICulture = new CultureInfo(ConfigurationManager.AppSettings["CultureInfo"]); //调用配置文件Application.Run(new Form1());}}
}

4. 以后启动软件的时候,只需要修改配置即可。

注意:如果中英文切换的时候,牵扯到字体长度问题,那么直接修改窗体的控件位置就行了。中文就移动中文的位置,英文就移动英文的位置。此功能也可以解决一个cs文件配套多个界面的问题。


http://chatgpt.dhexx.cn/article/K9bH31Wm.shtml

相关文章

基于注意力机制的 中 英机器翻译

数据处理模块 数据介绍 语料介绍一下&#xff1a; data文件夹有如下文件&#xff1a; cn.txt&#xff1a;中文语料&#xff0c;里面的句子都已经分好了词。 en.txt: 英语对齐语料&#xff0c;里面的单词也分词完毕。 cn.text.txt: 中文测试集语料 en.tetx.txt:英语对齐测试…

英文自然语言预处理

目录 1、数据集准备 2、数据集处理 &#xff08;1&#xff09;简单分词、词性还原、停用词过滤 &#xff08;2&#xff09;特征提取&#xff08;TT-IDF、信息增益、卡方检验、互信息、N-Gram等&#xff09; &#xff08;3&#xff09;文本标签向量化 &#xff08;4&…

经验分享 | 英文论文写作工具分享

网站分享 1.Thesaurus 针对英语单词近义词和反义词的询问词汇库&#xff0c;键入一个单词会自动生成多个与之类似的单词&#xff0c;列出各个单词的近义词和反义词。如果论文中太多相同的形容词&#xff08;如important等&#xff09;&#xff0c;就可以通过此网站查找近义词…

英文间隔问题

在中译英的参考文献中&#xff0c;有时候会突然发现英文单词之间间隔不一样&#xff0c;同时还会造成中文间隔不一的问题 需要去段落里面设置一下 点最右下角的箭头 依次点击&#xff0c;勾选允许西文在单词中间换行 完成&#xff01;

集中化监控SQL Server数据库

集中化监控SQL Server数据库 英文原文地址&#xff1a;https://www.simple-talk.com/sql/database-administration/centralize-your-database-monitoring-process/ 名词解释&#xff1a; CMS -- 中央管理服务器&#xff08;Central Management Server&#xff09; MDW -- 管理数…

cntopic库:支持中英文LDA话题分析

cntopic 简单好用的lda话题模型&#xff0c;支持中英文。该库基于gensim和pyLDAvis&#xff0c;实现了lda话题模型及可视化功能。 本文视频讲解已上传到B站(今晚会通过审核)&#xff0c;大家可以关注大邓的 B站账号&#xff1a;大邓和他的python 安装 pip install cntopic使用 …

【英文文本分类实战】之二——数据集挑选与划分

请参考本系列目录&#xff1a;【英文文本分类实战】之一——实战项目总览 下载本实战项目资源&#xff1a;神经网络实现英文文本分类.zip&#xff08;pytorch&#xff09; [1] 数据集平台 在阅读了大量的论文之后&#xff0c;由于每一篇论文都会提出一个模型&#xff0c;十分想…

Window部分软件图标显示不正常

电脑上的软件图标突然就变得不正常显示&#xff0c;网上baidu找了很多&#xff0c;也有很多解决方法&#xff0c;下面记录一种最终解决我问题的方法。 进入到控制面板-显示&#xff0c;将显示比例更改一下&#xff0c;注销电脑&#xff0c;若图标显示正常&#xff0c;再把显示…

电脑软件快捷方式不显示图标

电脑上有的软件快捷方式不显示图标&#xff0c;只显示是图片的样式&#xff0c;图片显示不出来&#xff0c;可能是原图标文件丢失&#xff0c;关联失效&#xff0c;缓存异常等&#xff0c;有时卸载软件重新安装可能会好&#xff0c;有的可能卸载重新安装也解决不了。 以下提供…

2,【electron+vue】 构建桌面应用——常见的功能及问题(修改桌面图标,软件图标,窗口图标,图标不显示问题,影藏默认菜单栏,开机自启,手动或被动关闭应用)

一.修改桌面图标,软件图标或者窗口左上角的图标. 1.首先这些图标必须是 .ico 结尾的图片,如果你将其他格式的图片改成.ico的,也不行哦,至于为什么,我也没深入研究,按着规定来就好. 2.如何获取.ico格式的图片或者说如何将其他格式的图片转换成.ico格式的图片呢,这里推荐一个app…

Win10 底部应用图标显示不正常(空白)

导致此问题一般是因为&#xff1a; 移动了程序目录未正确卸载的情况下重新安装 解决方法 右击任务栏图标&#xff0c;再右键程序&#xff0c;点击属性&#xff0c;更改程序正确位置和图标。

TortoiseSVN文件夹及文件图标不显示解决方法

由于自己的电脑是win7&#xff08;64位&#xff09;的&#xff0c;系统安装TortoiseSVN之后&#xff0c;其他的功能都能正常的使用&#xff0c;但是就是文件夹或文件夹的左下角就是不显示图标&#xff0c;这个问题前一段时间就遇到了&#xff08;那个时候没找到合适的答案&…

解决win10系统下软件图标显示异常的问题

最近遇到的问题&#xff0c;桌面图标&#xff0c;特别是word、excel等office办公软件的桌面图标全部变成txt的图标了&#xff0c;特别是之前安装过WPS软件&#xff0c;更容易出现这个问题。如下图所示&#xff1a; 解决办法&#xff1a; 1.下载软件filetypesman.zip&#x…

win10系统桌面应用图标显示不出来的问题

别慌别慌&#xff0c;只要找到桌面任务栏上右击鼠标&#xff0c;在弹出的菜单中点击【任务管理器】。在【任务管理器】找到【Windows资源管理器】&#xff0c;右击鼠标&#xff0c;选择【重新启动】即可重建图标缓存 CK: https://zhuanlan.zhihu.com/p/121125559

桌面计算机里没有桌面显示不出来怎么办,电脑桌面显示不出来图标 所有软件都可以正常工作 怎么解决?...

苏绯离 回答数&#xff1a;3 | 被采纳数&#xff1a;125 2020-04-18 12:14:50 当你遇到桌面上一个图标都没有的情形时&#xff0c;是由于多种原因引起的&#xff0c;你可按下面方法进行检修。 1、首先右击桌面选排列图标/勾选显示桌面图标。 2、如果故障依旧&#xff0c;打开…

win10某些软件图标显示过小解决方法

win10笔记本电脑分辨率过高&#xff0c;导致某些没有适配win10的软件显示窗口过小&#xff0c;看起来很是不舒服。 本文将介绍一种解决方法&#xff0c;调大显示窗口及图标。 以My Base为例&#xff1a; 1.右键单击图标–>选择“打开文件所在位置” 2.右键单击图标–>…

软件图标显示不正常【win7企业版】

现象&#xff1a; 原因&#xff1a; 图标缓存没有把该软件图标建立起来 解决&#xff1a; 一、 1、找到 IconCache.db 2、你要把电脑隐藏文件打开不然找不到这个文件的&#xff0c;组织—文件夹及搜索选项——查看——显示隐藏文件、文件夹和驱动器 隐藏已知文件的扩展名的√ 去…

解决win10系统桌面应用图标显示不出来的问题

解决win10系统桌面应用图标显示不出来的问题 有时候win10系统桌面应用图标会因为一些原因显示不出来或者显示的不全&#xff08;白色&#xff09;&#xff0c;这时候由于在桌面看的难看&#xff0c;并不好寻找使用的图标。 这里提供一个简单的方法-重置系统图标数据库文件&…

Win10 图标 显示不正常解决办法

解决Win10下图标不能正常显示(如变白色等)的问题。 Win10图标不能正常显示的解决方法 方法一&#xff1a;通过删除IconCache.db文件方法二&#xff1a;直接更改图标样式 方法一&#xff1a;通过删除IconCache.db文件 1.随便 打开 一个 文件夹 &#xff0c;点击 查看 &#xff…

office软件不显示图标

本方法的适用条件: 1. 确保已经正确安装office软件(笔者本人office版本为2016专业版),office各功能可以正确使用。 2. ‘’设置‘’→‘’应用‘’→‘’应用和功能‘’,中确保.xlsx的默认打开程序为excel。(word,ppt同理) 本人遇到的问题: 1. 桌面→鼠标右键,不…