标签:
看了网上实现实时切换的方式,感觉真是累啊! 自己写一个吧! 自夸一句:简单,优雅,高效。 如果有更好的方式,可以跟我讲一声。
实现是其于C# 本身的本地化方式之上.....比如你已经在C# 上实现了二种语言,且无问题。
1. 语种管理器
public enum LanguageEnum
{
LanguageCN,
LanguageEN,
}
class ManageLanguage
{
public static ManageLanguage Instance = new ManageLanguage();
ArrayList objectList = new ArrayList();
/// <summary>
/// 注册FORM
/// </summary>
/// <param name="item"></param>
public void RegObject(FormBase item)
{
if (objectList.Contains(item) != true)
{
objectList.Add(item);
}
}
/// <summary>
/// 设置语言
/// </summary>
/// <param name="lg">语言种类</param>
public void SetLanguage(LanguageEnum lg)
{
switch (lg)
{
case LanguageEnum.LanguageCN:
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-CN");
CallBackLanguage();
break;
case LanguageEnum.LanguageEN:
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("EN");
CallBackLanguage();
break;
}
}
/// <summary>
/// 遍历注册过的FORM,切换语言
/// </summary>
void CallBackLanguage()
{
//遍历所有Form,以切换其语言
foreach (FormBase form in objectList)
{
form.Language();
}
}
}
2. 语种切换接口
public partial class FormBase : Form
{
public FormBase()
{
InitializeComponent();
ManageLanguage.Instance.RegObject(this);
}
/// <summary>
/// 语言切换的接口
/// </summary>
public virtual void Language()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(this.GetType());
ArrayList list = new ArrayList();
FindControls(list, this);
this.Text = resources.GetString("$this.Text");
foreach (Control ctl in list)
{
resources.ApplyResources(ctl, ctl.Name);
}
}
/// <summary>
/// 把可以本地化的控件放入LIST
/// </summary>
/// <param name="list"></param>
/// <param name="ctl"></param>
private void FindControls(ArrayList list, Control ctl)
{
//容器不可以本地化
if (ctl is ContainerControl)
{
// MessageBox.Show(ctl.Name);
}
else
{
list.Add(ctl);
}
if (ctl.HasChildren)
{
foreach (Control c in ctl.Controls)
{
if (c is Form)
{
}
else
{
FindControls(list, c);
}
}
}
}
}
3. 使用时,只需要继承就可以了
public partial class FormMain : FormBase, ICaseView
{
}
标签:
原文地址:http://www.cnblogs.com/signal/p/4236024.html