CLanguageManager.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using System.Reflection;
  6. using System.Data;
  7. using CasunCommon.SystemLog;
  8. using PMSUI.Config;
  9. namespace PMSUI
  10. {
  11. public class CLanguageManager
  12. {
  13. private static Dictionary<string, string> m_DisplayLanguageDic = new Dictionary<string, string>();
  14. private static string m_DisplayLanguage = "";
  15. /// <summary>
  16. /// 记录当前语言切换关键字
  17. /// </summary>
  18. private static List<string> m_LanguageKey = new List<string>();
  19. public static List<string> LanguageKey
  20. {
  21. get
  22. {
  23. return m_LanguageKey;
  24. }
  25. set
  26. {
  27. m_LanguageKey = value;
  28. }
  29. }
  30. /// <summary>
  31. /// 初始化关键字
  32. /// </summary>
  33. private static void InitLanguageKey()
  34. {
  35. List<string> ltemID = new List<string>();
  36. //DataTable dt = CasunModelDB.CDBManager.Instance.PMS_sys_LanguageKey.GetAllList().Tables[0];
  37. DataTable dt = PmsCoreDB.DBHost.Instance.LanguageKey.GetAllList().Tables[0];
  38. foreach (DataRow r in dt.Rows)
  39. {
  40. string strkey = r.ItemArray[0].ToString();
  41. ltemID.Add(strkey);
  42. }
  43. m_LanguageKey = ltemID;
  44. }
  45. /// <summary>
  46. /// 初始化语言
  47. /// </summary>
  48. private static bool InitDisplayLanaguage()
  49. {
  50. try
  51. {
  52. string strwhere = " Language = '" + m_DisplayLanguage + "'";
  53. //List<CasunModelDB.Model.PMS_sys_LanguageVo> smodelList = CasunModelDB.CDBManager.Instance.PMS_sys_Language.GetModelList(strwhere);
  54. List<PmsCoreDB.Model.PMS_sys_Language> smodelList = PmsCoreDB.DBHost.Instance.Language.GetModelList(strwhere);
  55. //Dictionary<string, string> m_DisplayLanguage = new Dictionary<string, string>();
  56. m_DisplayLanguageDic.Clear();
  57. foreach (PmsCoreDB.Model.PMS_sys_Language smodel in smodelList)
  58. {
  59. if (!m_DisplayLanguageDic.ContainsKey(smodel.Keyword))
  60. {
  61. m_DisplayLanguageDic.Add(smodel.Keyword, smodel.Displayword);
  62. }
  63. }
  64. }
  65. catch(Exception e)
  66. {
  67. string strlog = "初始化语言异常:" + e.Message + e.StackTrace;
  68. CLog.Instance.WriteLog("Error", strlog);
  69. return false;
  70. }
  71. return true;
  72. }
  73. #region SetLang
  74. /// <summary>
  75. /// 设置当前程序的界面语言
  76. /// </summary>
  77. /// <param name="language">language:zh-CN, en-US</param>
  78. /// <param name="form">窗体实例</param>
  79. public static void SetLang(Form form)
  80. {
  81. if (form != null)
  82. {
  83. if (m_DisplayLanguage != CConfigManager.gs_globalConfig.gs_DisplayLanguage)
  84. {
  85. m_DisplayLanguage = CConfigManager.gs_globalConfig.gs_DisplayLanguage;
  86. InitDisplayLanaguage();
  87. InitLanguageKey();
  88. }
  89. AppLang(form);
  90. foreach (Control c in form.Controls)
  91. {
  92. if(c is ComboBox)
  93. {
  94. continue;
  95. }
  96. if(c is TextBox)
  97. {
  98. continue;
  99. }
  100. if(c is DataGridView)
  101. {
  102. continue;
  103. }
  104. if(c is DateTimePicker)
  105. {
  106. continue;
  107. }
  108. if(c is Form)
  109. {
  110. continue;
  111. }
  112. AppLang(c);
  113. }
  114. }
  115. }
  116. #endregion
  117. /// <summary>
  118. /// 语言切换
  119. /// </summary>
  120. /// <param name="strKeyword"></param>
  121. /// <returns></returns>
  122. public static string LanguageChange(string strKeyword)
  123. {
  124. if (strKeyword == "")
  125. {
  126. return strKeyword;
  127. }
  128. if (m_DisplayLanguageDic.ContainsKey(strKeyword))
  129. {
  130. return m_DisplayLanguageDic[strKeyword] == "" ? strKeyword : m_DisplayLanguageDic[strKeyword];
  131. }
  132. else
  133. {
  134. AddKeyword(strKeyword);
  135. return strKeyword;
  136. }
  137. }
  138. /// <summary>
  139. /// 添加关键字
  140. /// </summary>
  141. /// <param name="Keyword"></param>
  142. /// <returns></returns>
  143. public static bool AddKeyword(string Keyword)
  144. {
  145. if (!PmsCoreDB.DBHost.Instance.LanguageKey.Exists(Keyword))
  146. {
  147. m_LanguageKey.Add(Keyword);
  148. PmsCoreDB.Model.PMS_sys_LanguageKey sLanguageKey = new PmsCoreDB.Model.PMS_sys_LanguageKey();
  149. sLanguageKey.Keyword = Keyword;
  150. PmsCoreDB.DBHost.Instance.LanguageKey.Add(sLanguageKey);
  151. }
  152. m_DisplayLanguageDic.Add(Keyword, "");
  153. List<string> LanguageList = PmsCoreDB.DBHost.Instance.Language.GetLanguage();
  154. foreach (string Language in LanguageList)
  155. {
  156. if (!PmsCoreDB.DBHost.Instance.Language.Exists(Language, Keyword))
  157. {
  158. PmsCoreDB.Model.PMS_sys_Language slanguage = new PmsCoreDB.Model.PMS_sys_Language();
  159. slanguage.Language = Language;
  160. slanguage.Keyword = Keyword;
  161. PmsCoreDB.DBHost.Instance.Language.Add(slanguage);
  162. }
  163. }
  164. return true;
  165. }
  166. public static bool DeleteKeyword(string Keyword)
  167. {
  168. PmsCoreDB.DBHost.Instance.LanguageKey.Delete(Keyword);
  169. m_DisplayLanguageDic.Remove(Keyword);
  170. PmsCoreDB.DBHost.Instance.Language.DeleteKeyword(Keyword);
  171. return true;
  172. }
  173. #region AppLang for control
  174. /// <summary>
  175. /// 递归遍历窗体所有控件,针对其设置当前界面语言
  176. /// </summary>
  177. /// <param name="control"></param>
  178. /// <param name="resources"></param>
  179. private static void AppLang(Control control)
  180. {
  181. if (control is MenuStrip)
  182. {
  183. MenuStrip ms = control as MenuStrip;
  184. foreach (ToolStripMenuItem c in ms.Items)
  185. {
  186. AllToolStripMenuItems(c);
  187. c.Text = LanguageChange(c.Text);
  188. }
  189. }
  190. else if(control is Form)
  191. {
  192. control.Text = LanguageChange(control.Text);
  193. }
  194. else if(control is Panel)
  195. {
  196. Panel pane = control as Panel;
  197. foreach (Control c in pane.Controls)
  198. {
  199. AppLang(c);
  200. }
  201. }
  202. else if (control is SplitContainer)
  203. {
  204. SplitContainer SC = control as SplitContainer;
  205. foreach (Control c in SC.Panel1.Controls)
  206. {
  207. AppLang(c);
  208. }
  209. foreach (Control c in SC.Panel2.Controls)
  210. {
  211. AppLang(c);
  212. }
  213. }
  214. else if (control is ToolStripContainer)
  215. {
  216. ToolStripContainer tpc = control as ToolStripContainer;
  217. foreach (Control c in tpc.Controls)
  218. {
  219. AppLang(c);
  220. }
  221. }
  222. else if (control is GroupBox)
  223. {
  224. GroupBox GP = control as GroupBox;
  225. control.Text = LanguageChange(control.Text);
  226. foreach (Control c in GP.Controls)
  227. {
  228. AppLang(c);
  229. }
  230. }
  231. else if (control is TreeViewEx)
  232. {
  233. TreeViewEx TVE = control as TreeViewEx;
  234. foreach (TreeNode c in TVE.Nodes)
  235. {
  236. AllNodes(c);
  237. c.Text = LanguageChange(c.Text);
  238. }
  239. }
  240. else
  241. {
  242. control.Text = LanguageChange(control.Text);
  243. }
  244. }
  245. #endregion
  246. #region 遍历TreeNode的所有节点
  247. public static void AllNodes(TreeNode tn)
  248. {
  249. //1.将当前节点显示到lable上
  250. foreach (TreeNode c in tn.Nodes)
  251. {
  252. //string strText;
  253. //if (CLanguageManager.LanguageChange(c.Text, out strText))
  254. //{
  255. // c.Text = strText;
  256. //}
  257. c.Text = LanguageChange(c.Text);
  258. AllNodes(c);
  259. }
  260. }
  261. #endregion
  262. # region 遍历MenuItem的所有节点
  263. /// <summary>
  264. /// 遍历菜单的所有节点
  265. /// </summary>
  266. /// <param name="Item"></param>
  267. private static void AllToolStripMenuItems(ToolStripMenuItem Item)
  268. {
  269. for (int i = 0; i < Item.DropDownItems.Count; i++)
  270. {
  271. if (Item.DropDownItems[i] is ToolStripSeparator)
  272. {
  273. continue;
  274. }
  275. else
  276. {
  277. ToolStripMenuItem c = Item.DropDownItems[i] as ToolStripMenuItem;
  278. //string strText;
  279. //if (CLanguageManager.LanguageChange(c.Text, out strText))
  280. //{
  281. // c.Text = strText;
  282. //}
  283. c.Text = LanguageChange(c.Text);
  284. AllToolStripMenuItems(c);
  285. }
  286. }
  287. }
  288. #endregion
  289. }
  290. }