C# 杂项一一测试界面实现中英文切换

我们在做项目的时候,经常会通过菜单栏来实现测试界面的中英文切换,如下所示:

C# 杂项一一测试界面实现中英文切换_第1张图片

当程序切换到中文时,界面显示中文,切换到英文时,界面显示英文

1.首先我们为菜单按钮添加响应事件:

private void SetMenuClickEvent()
{
	//option
	toolStripMenuItem_option_language_cn.Click += new System.EventHandler(Menu_Option_Click_language);
	toolStripMenuItem_option_language_en.Click += new System.EventHandler(Menu_Option_Click_language);
}

2.事件实现:根据不同的菜单选择不同的语言,并设置测试界面的语言

private void Menu_Option_Click_language(object sender, EventArgs e)
{
	ToolStripMenuItem item = sender as ToolStripMenuItem;
	ToolStripMenuItem[] arr = new ToolStripMenuItem[] { toolStripMenuItem_option_language_en,
									toolStripMenuItem_option_language_cn};
	System.Globalization.CultureInfo  cti = null;

	if (item == toolStripMenuItem_option_language_en)
	{
		SetMenuImage(arr, 0);
		cti = new System.Globalization.CultureInfo("en");
	}
	else if(item == toolStripMenuItem_option_language_cn)
	{
		SetMenuImage(arr, 1);
		cti = new System.Globalization.CultureInfo("zh-CHS");
	}
	else
	{
		return;
	}

	System.Threading.Thread.CurrentThread.CurrentCulture = cti;
	System.Threading.Thread.CurrentThread.CurrentUICulture = cti;

	SetLanguage();
}
private void SetLanguage()
{
	SetMenuLanguage();
	SetCtrlLanguage();
}
private void SetMenuLanguage()
{
	if (InvokeRequired)
	{
		BeginInvoke(new Action(SetMenuLanguage));
		return;
	}

	toolStripMenuItem_device.Text = Resources.ResourceManager.GetString("Menu_device");
	toolStripMenuItem_device_setting.Text = Resources.ResourceManager.GetString("Menu_device_setting");

	toolStripMenuItem_option.Text = Resources.ResourceManager.GetString("Menu_option");

	toolStripMenuItem_help.Text = Resources.ResourceManager.GetString("Menu_help");
	toolStripMenuItem_help_about.Text = Resources.ResourceManager.GetString("Menu_help_about");
	toolStripMenuItem_help_userGuid.Text = Resources.ResourceManager.GetString("Menu_help_userGuid");
	toolStripMenuItem_help_log.Text = Resources.ResourceManager.GetString("Menu_help_log");

	toolStripMenuItem_readFile.Text = Resources.ResourceManager.GetString("Menu_readFile");
	toolStripMenuItem_readFile_readCsv.Text = Resources.ResourceManager.GetString("Menu_readFile_readCsv");
}

上述,Resources是工程下的一个资源文件,我们在资源文件中添加对应的中英文对照字符串,如下所示:

C# 杂项一一测试界面实现中英文切换_第2张图片

C# 杂项一一测试界面实现中英文切换_第3张图片

当然,我们也可以通过获取其他Form上的Resources资源文件来实现中英文字符串的切换

private void SetCtrlLanguage()
{
	ResourceManager rm = new ResourceManager(typeof(Form_Gadgets));
	this.Text = rm.GetString("$this.Text");
	Ready.Text = rm.GetString("Ready.Text");
	tabPage_AuxMic.Text = rm.GetString("tabPage_AuxMic.Text");
	tabPage_Earpiece.Text = rm.GetString("tabPage_Earpiece.Text");
	tabPage_MainMic.Text = rm.GetString("tabPage_MainMic.Text");
	tabPage_Speaker.Text = rm.GetString("tabPage_Speaker.Text");
	tabPage_Vibrator.Text = rm.GetString("tabPage_Vibrator.Text");
}

C# 杂项一一测试界面实现中英文切换_第4张图片

这样,我们就可以通过切换菜单栏来实现界面中英文切换了。

C# 杂项一一测试界面实现中英文切换_第5张图片

C# 杂项一一测试界面实现中英文切换_第6张图片

你可能感兴趣的:(C#,杂项)