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

在有些软件中,需要中英文切换的功能,甚至其他语言切换的功能,都可以使用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");//中文是zh
            ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1));
            resources.ApplyResources(label1, "label1");  //这里的第一个参数界面的控件的Name,第二参数,如果选择的zh,就是Form1.resx文件中label1.Text
            resources.ApplyResources(button1, "button1");//这里的第一个参数界面的控件的Name,第二参数,如果选择的zh,就是Form1.resx文件中button1.Text
            resources.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.Text
            resources.ApplyResources(button1, "button1"); //这里的第一个参数界面的控件的Name,第二参数,如果选择的en,就是Form1.en.resx文件中button1.Text
            resources.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");//中文是zh
            ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1));
            resources.ApplyResources(label1, "label1");  //这里的第一个参数界面的控件的Name,第二参数,如果选择的zh,就是Form1.resx文件中label1.Text
            resources.ApplyResources(button1, "button1");//这里的第一个参数界面的控件的Name,第二参数,如果选择的zh,就是Form1.resx文件中button1.Text
            resources.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.Text
            resources.ApplyResources(button1, "button1"); //这里的第一个参数界面的控件的Name,第二参数,如果选择的en,就是Form1.en.resx文件中button1.Text
            resources.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");//中文是zh
            ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1));
            resources.ApplyResources(label1, "label1");  //这里的第一个参数界面的控件的Name,第二参数,如果选择的zh,就是Form1.resx文件中label1.Text
            resources.ApplyResources(button1, "button1");//这里的第一个参数界面的控件的Name,第二参数,如果选择的zh,就是Form1.resx文件中button1.Text
            resources.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.Text
            resources.ApplyResources(button1, "button1"); //这里的第一个参数界面的控件的Name,第二参数,如果选择的en,就是Form1.en.resx文件中button1.Text
            resources.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文件配套多个界面的问题。

 

来源:winform使用本地化,中英文切换_winform 中英文切换_故里2130的博客-CSDN博客

  • 14
    点赞
  • 73
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 40
    评论
Winform 通过本地 txt 文件实现中英文切换,可以按照以下步骤进行: 1. 创建多个 txt 文件,每个文件对应一种语言,例如,中文语言的 txt 文件命名为 chinese.txt,英文语言的 txt 文件命名为 english.txt。在每个 txt 文件中,按照一定的格式,存储需要翻译的字符串,例如: ``` # chinese.txt title=欢迎使用我的程序 button_ok=确定 button_cancel=取消 # english.txt title=Welcome to my program button_ok=OK button_cancel=Cancel ``` 2. 在程序中读取指定的 txt 文件,获取对应语言的字符串资源。可以通过 StreamReader 类来读取 txt 文件中的内容,然后使用字符串分割函数或正则表达式来解析出每个字符串资源的名称和对应的值。 3. 将读取到的字符串资源,存储到一个全局的字典中。在字典中,使用字符串资源的名称作为键,字符串资源的值作为值。例如,可以定义一个静态的 Dictionary<string, string> 类型的变量,用来存储读取到的字符串资源。 4. 在程序中使用字典中的字符串资源,替换原来的字符串。可以通过遍历所有的控件,然后使用 Find 方法查找每个控件中需要替换的字符串,然后使用字典中对应的值来替换原来的字符串。 5. 在程序中提供切换语言的功能,例如,可以在设置界面中添加一个下拉框,让用户选择需要使用的语言。当用户选择切换语言时,重新读取对应的 txt 文件,并更新全局的字典中的字符串资源。 通过以上步骤,就可以实现 Winform 通过本地 txt 文件实现中英文切换。需要注意的是,这种方法只适用于小型的 Winform 程序,对于大型的 Winform 程序,建议使用资源文件来进行多语言支持。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 40
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

故里2130

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值