C#多语言切换

第一步建立工程

第二步:拖拽控件

第三步选择窗体

第四步修改窗体localizable 属性为 true  如下图

第五步 修改language 属性 如下图

 第六步:修改各个控件的字符名称

会出现下面的资源文件(注意之前是没有资源文件的)

 第七步:修改语言为英文

修改各个控件的文字生成新的资源文件(新增加的,可以对比上面的图)

 实现效果:

启动后:

选择英文

选择中文

  

 实现代码:

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 WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //参考讲解
        //https://www.bilibili.com/video/BV1DM4y1n7Ds/?spm_id_from=333.1007.top_right_bar_window_custom_collection.content.click

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        ComponentResourceManager resource = new ComponentResourceManager(typeof(Form1));
        private void button2_Click(object sender, EventArgs e)
        {
            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("zh-CN");
            ApplyResource();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");
            ApplyResource();
        }
        void ApplyResource() 
        {
            foreach(Control item in this.Controls)
            {
                if(item != null)
                {
                    resource.ApplyResources(item,item.Name);
                }
            }
        }
    }
}

题记:

主要是如何生成最初的资源文件:

以及如何应用资源文件

特此记录

anlog

2023年6月20日

动态信息需要代码单独调用显示

void ApplyResource() 
        {
            foreach(Control item in this.Controls)
            {
                if(item != null)
                {
                    resource.ApplyResources(item,item.Name);
                }
            }
            button4.Text = bt4_ZhuangT? resource.GetString("BT4_Open"):resource.GetString("BT4_Close");
            this.Text = resource.GetString("BT4_Open") + " -- " + resource.GetString("BT4_Close");
        }
        bool bt4_ZhuangT = true;

        private void button4_Click(object sender, EventArgs e)
        {
            //bt4_ZhuangT = !bt4_ZhuangT;
            if( bt4_ZhuangT) { bt4_ZhuangT = false; } else { bt4_ZhuangT = true; } 

            ApplyResource();
        }

关键语句

button4.Text = bt4_ZhuangT? resource.GetString("BT4_Open"):resource.GetString("BT4_Close");

需要资源文件内配套字符串

 另外英文也需要单独定义

 这三处配合使用

特此记录

anlog

2023年6月20日

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值