两种实现方式:1配置xml文件实现中英文切换
2语言资源文件实现中英文切换
1配置xml文件实现中英文切换
以Winform登录界面为示例:
首先建立一个中文的xml文件,一个英文的xml文件,一个App.config配置文件
English.xml核心代码
<?xml version="1.0" encoding="utf-8" ?>
<English>
<Login_UserName>UserName:</Login_UserName>
<Login_UserPwd>Password:</Login_UserPwd>
<Login_Login>Login</Login_Login>
</English>
Chinese.xml核心代码
<?xml version="1.0" encoding="utf-8" ?>
<Chinese>
<Login_UserName>用户名:</Login_UserName>
<Login_UserPwd>密码:</Login_UserPwd>
<Login_Login>登录</Login_Login>
</Chinese>
App.Config核心代码
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<!-- Language的值只能是Chinese(中文),English(英文) -->
<add key="Language" value="English"/>
</appSettings>
</configuration>
中英文切换核心代码实现
新建一个 GlobalData类
public class GlobalData
{
/// <summary>
/// 系统语言(Chinese(中文),English(英文)。。。)
/// </summary>
public static string SystemLanguage = System.Configuration.ConfigurationManager.AppSettings["Language"];
// 在调用ConfigurationManager时,要加载System.Configuration.dll
private static Language globalLanguage;
public static Language GlobalLanguage
{
get
{
if (globalLanguage == null)
{
globalLanguage = new Language();
return globalLanguage;
}
return globalLanguage;
}
}
}
再建立一个 Language 类,主要作用是初始化时加载xml语言配置文件
public class Language
{
#region 登陆界面
public string Login_UserName = "";
public string Login_UserPwd = "";
public string Login_Login = "";
#endregion
protected Dictionary<string, string> DicLanguage = new Dictionary<string, string>();
public Language()
{
XmlLoad(GlobalData.SystemLanguage);
BindLanguageText();
}
/// <summary>
/// 读取XML放到内存
/// </summary>
/// <param name="language"></param>
protected void XmlLoad(string language)
{
try
{
XmlDocument doc = new XmlDocument();
//这里要注意,程序的根目录下面要有一个Languages的文件夹,里面放的东西就是那两个
//语言配置文件
string address = AppDomain.CurrentDomain.BaseDirectory + "Languages\\" + language + ".xml";
doc.Load(address);
XmlElement root = doc.DocumentElement;
XmlNodeList nodeLst1 = root.ChildNodes;
foreach (XmlNode item in nodeLst1)
{
DicLanguage.Add(item.Name, item.InnerText);
}
}
catch (Exception ex)
{
throw ex;
}
}
public void BindLanguageText()
{
Login_UserName = DicLanguage["Login_UserName"];
Login_UserPwd = DicLanguage["Login_UserPwd"];
Login_Login = DicLanguage["Login_Login"];
}
}
初始化调用
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
SetLanguage(); //调用
}
private void SetLanguage()
{
this.lbUserName.Text = GlobalData.GlobalLanguage.Login_UserName;
this.lbPwd.Text = GlobalData.GlobalLanguage.Login_UserPwd;
this.btnLogin.Text = GlobalData.GlobalLanguage.Login_Login;
}
}
运行即可.
有一个不好的地方就是,每次切换完都要重启系统。
扩展:更新xlm的值
例如上面的xml例子:
<?xml version="1.0" encoding="utf-8" ?>
<Chinese>
<Login_UserName>用户名:</Login_UserName>
<Login_UserPwd>密码:</Login_UserPwd>
<Login_Login>登录</Login_Login>
</Chinese>
把Login_UserName 节点里面的用户名,更新为账户
//获取配置文件路径
string address = AppDomain.CurrentDomain.BaseDirectory + "Languages\\" + language + ".xml";
XmlDocument doc = new XmlDocument();
doc.Load(address);
XmlNode xn = doc.SelectSingleNode("//Login_UserName");
string transDate = xn.InnerText;//获取当前的值
xn.InnerText = "账户"; //更新新值
doc.Save(address); //保存
另外一种实现方式
2语言资源文件实现中英文切换
效果
一,添加LanguageHelper类
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>
/// <param name="lang">language:zh-CN, en-US</param>
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>
/// <param name="lang">language:zh-CN, en-US</param>
/// <param name="form">the form you need to set</param>
/// <param name="formType">the type of the form </param>
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>
/// <param name="control"></param>
/// <param name="resources"></param>
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>
/// <param name="item"></param>
/// <param name="resources"></param>
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
{
}
}
}