之前文章说了本地化,只是界面操作的本地化。
如果遇到内置的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博客