C# 多語言實時切換的實現

 

看了網上實現實時切換的方式,感受真是累啊!  本身寫一個吧!   自詡一句:簡單,優雅,高效。 若是有更好的方式,能夠跟我講一聲。this

 

實現是其於C# 自己的本地化方式之上.....好比你已經在C# 上實現了二種語言,且無問題。orm

1. 語種管理器繼承

 


public enum LanguageEnum
{
LanguageCN,
LanguageEN,
}接口


class ManageLanguage
{
public static ManageLanguage Instance = new ManageLanguage();it

ArrayList objectList = new ArrayList();io

/// <summary>
/// 註冊FORM
/// </summary>
/// <param name="item"></param>
public void RegObject(FormBase item)
{
if (objectList.Contains(item) != true)
{
objectList.Add(item);
}
}form

/// <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;
}
}class

/// <summary>
/// 遍歷註冊過的FORM,切換語言
/// </summary>
void CallBackLanguage()
{
//遍歷全部Form,以切換其語言
foreach (FormBase form in objectList)
{
form.Language();
}
}容器

 


}object

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

{

 

}

相關文章
相關標籤/搜索