winform 中英文切换

winform 中英文切换

我们可以通过一个按钮或者单选框之类的控件来触发切换事件

这里我用按钮点击事件实现切换效果

界面如下:

在这里插入图片描述

1.新建两个资源文件,一个中文,一个英文

在这里插入图片描述
下面是资源文件的具体内容

在这里插入图片描述
在这里插入图片描述
注意: 这里不能错,否则无效,要把项目名拼写注意一下。

2.详细代码实现

using System.ComponentModel;
using System.Globalization;

namespace multiLanguage
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnZh_Click(object sender, EventArgs e)
        {
            if (Thread.CurrentThread.CurrentCulture.Name == "zh")
                return;
            Thread.CurrentThread.CurrentCulture = new CultureInfo("zh");//中文
            ApplyResource(this);//传入当前的界面
        }

        private void btnEn_Click(object sender, EventArgs e)
        {
            if (Thread.CurrentThread.CurrentCulture.Name == "en")
                return;
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en");//英文
            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);
                }
            }
        }
    }
}

点击中文按钮

在这里插入图片描述
点击英文按钮

在这里插入图片描述

代码中对相同资源做了判断,要是不改变,就不会刷新,变更了资源才会刷新界面。

3. 配置文件

添加一个配置文件
在这里插入图片描述
在程序入口进行读取

在这里插入图片描述

using Microsoft.Extensions.Configuration;
using System.Globalization;

namespace multiLanguage
{
    internal static class Program
    {
        /// <summary>
        ///  The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            ApplicationConfiguration.Initialize();

            IConfiguration configuration = new ConfigurationBuilder()
                           .SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
                           .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                           .Build();

            string setting1 = configuration["AppSettings:CultureInfo"]??"zh";

            Thread.CurrentThread.CurrentUICulture = new CultureInfo(setting1); 
            Application.Run(new Form1());
        }
    }
}

这样就实现了用配置文件控制输出了。

在这里需要注意,配置文件记得选上如果新则复制,.net core的环境下不要用App.Config了,ConfigurationManager.AppSettings["Setting1"]; 这个方法不好使了,这个是.net framework环境下用的了,需要使用appsetting.json

在这里插入图片描述

  • 9
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

GeGe&YoYo

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

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

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

打赏作者

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

抵扣说明:

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

余额充值