Winform下的语言国际化,几行代码轻松实现
最近做了一些关于winform的项目,需要用到winform的语言国际化,在初使化的时候用起来非常方便。可以参考一下:
核心逻辑:
预览效果演示:
OK,以下是核心代码和操作流程
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WFInfor
{
public class LanguageHelper
{
#region SetAllLang
/// <summary>
/// Set language
/// </summary>
///
private static void SetAllLang(string lang)
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);
Form frm = null;
string name = "MainForm";
frm = (Form)Assembly.Load("CameraTest").CreateInstance(name);
if (frm != null)
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager();
resources.ApplyResources(frm, "$this");
AppLang(frm, resources);
}
}
#endregion
#region SetLang
/// <summary>
///
/// </summary>
///
///
///
public static void SetLang(string lang, Form form, Type formType)
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);
if (form != null)
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(formType);
resources.ApplyResources(form, "$this");
AppLang(form, resources);
}
}
#endregion
#region AppLang for control
/// <summary>
/// loop set the propery of the control
/// </summary>
///
///
private static void AppLang(Control control, System.ComponentModel.ComponentResourceManager resources)
{
if (control is MenuStrip)
{
resources.ApplyResources(control, control.Name);
MenuStrip ms = (MenuStrip)control;
if (ms.Items.Count > 0)
{
foreach (ToolStripMenuItem c in ms.Items)
{
AppLang(c, resources);
}
}
}
foreach (Control c in control.Controls)
{
resources.ApplyResources(c, c.Name);
AppLang(c, resources);
}
}
#endregion
#region AppLang for menuitem
/// <summary>
/// set the toolscript
/// </summary>
///
///
private static void AppLang(ToolStripMenuItem item, System.ComponentModel.ComponentResourceManager resources)
{
if (item is ToolStripMenuItem)
{
resources.ApplyResources(item, item.Name);
ToolStripMenuItem tsmi = (ToolStripMenuItem)item;
if (tsmi.DropDownItems.Count > 0)
{
foreach (ToolStripMenuItem c in tsmi.DropDownItems)
{
AppLang(c, resources);
}
}
}
}
#endregion
}
}
注意:格式必须是 Form名.语言名.rexs
我添加了两个,一个中文,一个英文
中文资源文件修改:
英文资源文件修改:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WFInfor
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
private void cbxchose_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbxchose.SelectedItem.ToString() == "中文")
{
LanguageHelper.SetLang("zh-Cn", this, typeof(FrmMain));
}
else if (cbxchose.SelectedItem.ToString() == "English")
{
LanguageHelper.SetLang("en-Us", this, typeof(FrmMain));
}
else
{
}
}
}
====》 DEMO下载《====
- 原文作者:大鱼
- 原文链接:https://brucedone.com/archives/149/
- 版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议. 进行许可,非商业转载请注明出处(作者,原文链接),商业转载请联系作者获得授权。