123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Windows.Forms;
- using System.Reflection;
- using System.Data;
- using CasunCommon.SystemLog;
- using PMSUI.Config;
- namespace PMSUI
- {
- public class CLanguageManager
- {
- private static Dictionary<string, string> m_DisplayLanguageDic = new Dictionary<string, string>();
- private static string m_DisplayLanguage = "";
-
- /// <summary>
- /// 记录当前语言切换关键字
- /// </summary>
- private static List<string> m_LanguageKey = new List<string>();
- public static List<string> LanguageKey
- {
- get
- {
- return m_LanguageKey;
- }
- set
- {
- m_LanguageKey = value;
- }
- }
- /// <summary>
- /// 初始化关键字
- /// </summary>
- private static void InitLanguageKey()
- {
- List<string> ltemID = new List<string>();
- //DataTable dt = CasunModelDB.CDBManager.Instance.PMS_sys_LanguageKey.GetAllList().Tables[0];
- DataTable dt = PmsCoreDB.DBHost.Instance.LanguageKey.GetAllList().Tables[0];
- foreach (DataRow r in dt.Rows)
- {
- string strkey = r.ItemArray[0].ToString();
- ltemID.Add(strkey);
- }
- m_LanguageKey = ltemID;
- }
- /// <summary>
- /// 初始化语言
- /// </summary>
- private static bool InitDisplayLanaguage()
- {
- try
- {
- string strwhere = " Language = '" + m_DisplayLanguage + "'";
- //List<CasunModelDB.Model.PMS_sys_LanguageVo> smodelList = CasunModelDB.CDBManager.Instance.PMS_sys_Language.GetModelList(strwhere);
- List<PmsCoreDB.Model.PMS_sys_Language> smodelList = PmsCoreDB.DBHost.Instance.Language.GetModelList(strwhere);
- //Dictionary<string, string> m_DisplayLanguage = new Dictionary<string, string>();
- m_DisplayLanguageDic.Clear();
- foreach (PmsCoreDB.Model.PMS_sys_Language smodel in smodelList)
- {
- if (!m_DisplayLanguageDic.ContainsKey(smodel.Keyword))
- {
- m_DisplayLanguageDic.Add(smodel.Keyword, smodel.Displayword);
- }
- }
- }
- catch(Exception e)
- {
- string strlog = "初始化语言异常:" + e.Message + e.StackTrace;
- CLog.Instance.WriteLog("Error", strlog);
- return false;
- }
- return true;
- }
- #region SetLang
- /// <summary>
- /// 设置当前程序的界面语言
- /// </summary>
- /// <param name="language">language:zh-CN, en-US</param>
- /// <param name="form">窗体实例</param>
- public static void SetLang(Form form)
- {
- if (form != null)
- {
- if (m_DisplayLanguage != CConfigManager.gs_globalConfig.gs_DisplayLanguage)
- {
- m_DisplayLanguage = CConfigManager.gs_globalConfig.gs_DisplayLanguage;
- InitDisplayLanaguage();
- InitLanguageKey();
- }
- AppLang(form);
- foreach (Control c in form.Controls)
- {
- if(c is ComboBox)
- {
- continue;
- }
- if(c is TextBox)
- {
- continue;
- }
- if(c is DataGridView)
- {
- continue;
- }
- if(c is DateTimePicker)
- {
- continue;
- }
- if(c is Form)
- {
- continue;
- }
- AppLang(c);
- }
- }
- }
- #endregion
-
- /// <summary>
- /// 语言切换
- /// </summary>
- /// <param name="strKeyword"></param>
- /// <returns></returns>
- public static string LanguageChange(string strKeyword)
- {
- if (strKeyword == "")
- {
- return strKeyword;
- }
- if (m_DisplayLanguageDic.ContainsKey(strKeyword))
- {
- return m_DisplayLanguageDic[strKeyword] == "" ? strKeyword : m_DisplayLanguageDic[strKeyword];
- }
- else
- {
- AddKeyword(strKeyword);
- return strKeyword;
- }
- }
- /// <summary>
- /// 添加关键字
- /// </summary>
- /// <param name="Keyword"></param>
- /// <returns></returns>
- public static bool AddKeyword(string Keyword)
- {
- if (!PmsCoreDB.DBHost.Instance.LanguageKey.Exists(Keyword))
- {
- m_LanguageKey.Add(Keyword);
- PmsCoreDB.Model.PMS_sys_LanguageKey sLanguageKey = new PmsCoreDB.Model.PMS_sys_LanguageKey();
- sLanguageKey.Keyword = Keyword;
- PmsCoreDB.DBHost.Instance.LanguageKey.Add(sLanguageKey);
- }
- m_DisplayLanguageDic.Add(Keyword, "");
- List<string> LanguageList = PmsCoreDB.DBHost.Instance.Language.GetLanguage();
- foreach (string Language in LanguageList)
- {
- if (!PmsCoreDB.DBHost.Instance.Language.Exists(Language, Keyword))
- {
- PmsCoreDB.Model.PMS_sys_Language slanguage = new PmsCoreDB.Model.PMS_sys_Language();
- slanguage.Language = Language;
- slanguage.Keyword = Keyword;
- PmsCoreDB.DBHost.Instance.Language.Add(slanguage);
- }
- }
- return true;
- }
- public static bool DeleteKeyword(string Keyword)
- {
- PmsCoreDB.DBHost.Instance.LanguageKey.Delete(Keyword);
- m_DisplayLanguageDic.Remove(Keyword);
- PmsCoreDB.DBHost.Instance.Language.DeleteKeyword(Keyword);
- return true;
- }
- #region AppLang for control
- /// <summary>
- /// 递归遍历窗体所有控件,针对其设置当前界面语言
- /// </summary>
- /// <param name="control"></param>
- /// <param name="resources"></param>
- private static void AppLang(Control control)
- {
- if (control is MenuStrip)
- {
- MenuStrip ms = control as MenuStrip;
- foreach (ToolStripMenuItem c in ms.Items)
- {
- AllToolStripMenuItems(c);
- c.Text = LanguageChange(c.Text);
- }
- }
- else if(control is Form)
- {
- control.Text = LanguageChange(control.Text);
- }
- else if(control is Panel)
- {
- Panel pane = control as Panel;
- foreach (Control c in pane.Controls)
- {
- AppLang(c);
- }
- }
- else if (control is SplitContainer)
- {
- SplitContainer SC = control as SplitContainer;
- foreach (Control c in SC.Panel1.Controls)
- {
- AppLang(c);
- }
- foreach (Control c in SC.Panel2.Controls)
- {
- AppLang(c);
- }
- }
- else if (control is ToolStripContainer)
- {
- ToolStripContainer tpc = control as ToolStripContainer;
-
- foreach (Control c in tpc.Controls)
- {
- AppLang(c);
- }
- }
- else if (control is GroupBox)
- {
- GroupBox GP = control as GroupBox;
- control.Text = LanguageChange(control.Text);
- foreach (Control c in GP.Controls)
- {
- AppLang(c);
- }
- }
- else if (control is TreeViewEx)
- {
- TreeViewEx TVE = control as TreeViewEx;
- foreach (TreeNode c in TVE.Nodes)
- {
- AllNodes(c);
- c.Text = LanguageChange(c.Text);
- }
- }
- else
- {
- control.Text = LanguageChange(control.Text);
- }
- }
- #endregion
- #region 遍历TreeNode的所有节点
- public static void AllNodes(TreeNode tn)
- {
- //1.将当前节点显示到lable上
- foreach (TreeNode c in tn.Nodes)
- {
- //string strText;
- //if (CLanguageManager.LanguageChange(c.Text, out strText))
- //{
- // c.Text = strText;
- //}
- c.Text = LanguageChange(c.Text);
- AllNodes(c);
- }
- }
- #endregion
- # region 遍历MenuItem的所有节点
- /// <summary>
- /// 遍历菜单的所有节点
- /// </summary>
- /// <param name="Item"></param>
- private static void AllToolStripMenuItems(ToolStripMenuItem Item)
- {
- for (int i = 0; i < Item.DropDownItems.Count; i++)
- {
- if (Item.DropDownItems[i] is ToolStripSeparator)
- {
- continue;
- }
- else
- {
- ToolStripMenuItem c = Item.DropDownItems[i] as ToolStripMenuItem;
- //string strText;
- //if (CLanguageManager.LanguageChange(c.Text, out strText))
- //{
- // c.Text = strText;
- //}
- c.Text = LanguageChange(c.Text);
- AllToolStripMenuItems(c);
- }
- }
- }
- #endregion
- }
- }
|