C# winform MessageBox的本地化,语言切换

之前文章说了本地化,只是界面操作的本地化。

如果遇到内置的MessageBox,其实也可以使用本地化,当然你用其他的办法也行。

1.建立一个程序

2.窗体选择语言设置,分别进行ms   ms1的设置。中文就设置中文,英文就设置英文 

 

3.代码

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

        private void button1_Click(object sender, EventArgs e)
        {
            ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1));
            string ms = resources.GetString("ms");//获取ms的值
            MessageBox.Show(ms);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1));
            string ms = resources.GetString("ms");//获取ms的值
            string ms1 = resources.GetString("ms1");//获取ms1的值
            MessageBox.Show(ms, ms1);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int currentLcid = Thread.CurrentThread.CurrentUICulture.LCID;
            currentLcid = (currentLcid == 2052) ? 1033 : 2052;//这里的2052是中文   1033是英文
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(currentLcid);//设置当前的语言
        }
    }
}

4.效果

注意: 增加ms  ms1的时候,要在所有界面本地化都做完后,再增加,否则会清空加的ms,ms1。

拓展1

上面的方法的确很鸡肋,“注意”说的非常好。

所以我们建立资源来解决这个问题。

1.延续之前的程序,建立一个中文的资源Resource.resx,一个英文的资源Resource_en.resx,分别把ms,ms1加进去。

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 WindowsFormsApp5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        ComponentResourceManager resources = null;
        private void button1_Click(object sender, EventArgs e)
        {
            if (resources == null)
                return;
            string ms = resources.GetString("ms");//获取ms的值
            MessageBox.Show(ms);

            //ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1));
            //string ms = resources.GetString("ms");//获取ms的值
            //MessageBox.Show(ms);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (resources == null)
                return;
            string ms = resources.GetString("ms");//获取ms的值
            string ms1 = resources.GetString("ms1");//获取ms1的值
            MessageBox.Show(ms, ms1);

            //ComponentResourceManager resources = new ComponentResourceManager(typeof(Form1));
            //string ms = resources.GetString("ms");//获取ms的值
            //string ms1 = resources.GetString("ms1");//获取ms1的值
            //MessageBox.Show(ms, ms1);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int currentLcid = Thread.CurrentThread.CurrentUICulture.LCID;
            currentLcid = (currentLcid == 2052) ? 1033 : 2052;//这里的2052是中文   1033是英文
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(currentLcid);//设置当前的语言

            if (currentLcid==2052)//中文
            {
                resources= new ComponentResourceManager(typeof(Resource.Resource));
            }
            else//英文
            {
                resources = new ComponentResourceManager(typeof(Resource.Resource_en));
            }
        }
    }
}

3.效果

拓展2

1.延续前面的程序,建立一个AllResource类

Resource文件

Resource_en文件

 AllResource的代码

using System.ComponentModel;
using System.Globalization;
using System.Threading;

namespace WindowsFormsApp5.Resource
{
    public class AllResource
    {
        private static ComponentResourceManager resources;
        private static CultureInfo ci;

        public static ComponentResourceManager Resources { get => resources; set => resources = value; }

        public static void GetResource()
        {
            ci = System.Threading.Thread.CurrentThread.CurrentUICulture;
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ci.Name);//设置当前的语言
            if (ci.Name == "zh-CN")
            {
                resources = new ComponentResourceManager(typeof(Resource));
            }
            else
            {
                resources = new ComponentResourceManager(typeof(Resource_en));
            }
        }
    }
}

2.在程序的入口处写入

 代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApp5.Resource;

namespace WindowsFormsApp5
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en");//设置英文语言
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-CN");//设置中文语言
            AllResource.GetResource();//调用
            Application.Run(new Form1());
        }
    }
}

3.调用的时候

 

 代码

AllResource.Resources.GetString("ms");

拓展3

在拓展2的基础上,增加LanguageSwitch独立类,可以生成一个dll

 public static class LanguageSwitch
    {
        public static string SetLanguage()
        {
            //return "zh-CN";        //中文
            return "en";             //英文
        }
    }

调用

  public static void GetResource()
        {
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(LanguageSwitch.LanguageSwitch.SetLanguage());//设置当前的语言
            ci = System.Threading.Thread.CurrentThread.CurrentUICulture;
            if (ci.Name == "zh-CN")
            {
                resources = new ComponentResourceManager(typeof(Resource));
            }
            else
            {
                resources = new ComponentResourceManager(typeof(Resource_en));
            }
        }

这个相当于走配置文件,只是这个不能让用户修改。

来源:C# winform MessageBox的本地化,语言切换_winform messagbox_故里2130的博客-CSDN博客

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

故里2130

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

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

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

打赏作者

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

抵扣说明:

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

余额充值