SetLanguage.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.Windows.Forms;
  3. using System.ComponentModel;
  4. using System.Threading;
  5. using MuchInfo.Chart.Data.EnumTypes; /// 线程空间
  6. namespace IndexFormula.Finance.Win
  7. {
  8. /// <summary>
  9. /// 语言库 class
  10. /// </summary>
  11. public class SetLanguage
  12. {
  13. /// <summary>
  14. /// 当前语言类型
  15. /// </summary>
  16. public static ChartLanguageType currentLanguage;
  17. /// <summary>
  18. /// 当前语言名称
  19. /// </summary>
  20. public static string languageName = string.Empty;
  21. public static void SetcurrentLanguage(ChartLanguageType language)
  22. {
  23. currentLanguage = language;
  24. switch (language)
  25. {
  26. case ChartLanguageType.SimplifiedChinese:
  27. languageName = "zh-Hans";
  28. break;
  29. case ChartLanguageType.TraditionalChinese:
  30. languageName = "zh-Hant";
  31. break;
  32. case ChartLanguageType.English:
  33. languageName = "en-us";
  34. break;
  35. default :
  36. languageName = "zh-Hans";
  37. break;
  38. }
  39. PluginManager.htAssemblyClear();
  40. PluginManager.Load(Environment.CurrentDirectory + "\\Plugins\\" + languageName);
  41. }
  42. /// <summary>
  43. /// 设置当前程序的界面语言
  44. /// </summary>
  45. /// <param name="lang">语言 </param>
  46. /// <param name="form">窗体</param>
  47. /// <param name="frmtype">窗体类型</param>
  48. private static void SetLang(string lang, Form form, Type frmtype)
  49. {
  50. languageName = lang;
  51. Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(languageName);
  52. if (form != null)
  53. {
  54. ComponentResourceManager resources = new ComponentResourceManager(frmtype);
  55. resources.ApplyResources(form, "$this");
  56. AppLang(form, resources);
  57. }
  58. }
  59. /// <summary>
  60. /// 遍历窗体所有控件,针对其设置当前界面语言
  61. /// </summary>
  62. /// <param name="contrl"></param>
  63. /// <param name="resoureces"></param>
  64. private static void AppLang(Control control, ComponentResourceManager resources)
  65. {
  66. resources.ApplyResources(control.Controls, control.Name);
  67. if (control is MenuStrip)
  68. {
  69. //将资源应用与对应的属性
  70. resources.ApplyResources(control, control.Name);
  71. MenuStrip ms = (MenuStrip)control;
  72. if (ms.Items.Count > 0)
  73. {
  74. foreach (ToolStripMenuItem c in ms.Items)
  75. {
  76. //调用 遍历菜单 设置语言
  77. AppLang(c, resources);
  78. }
  79. }
  80. }
  81. foreach (Control c in control.Controls)
  82. {
  83. resources.ApplyResources(c, c.Name);
  84. AppLang(c, resources);
  85. }
  86. }
  87. /// <summary>
  88. /// 遍历菜单
  89. /// </summary>
  90. /// <param name="item"></param>
  91. /// <param name="resources"></param>
  92. private static void AppLang(ToolStripMenuItem item, System.ComponentModel.ComponentResourceManager resources)
  93. {
  94. if (item is ToolStripMenuItem)
  95. {
  96. resources.ApplyResources(item, item.Name);
  97. ToolStripMenuItem tsmi = (ToolStripMenuItem)item;
  98. if (tsmi.DropDownItems.Count > 0)
  99. {
  100. foreach (ToolStripMenuItem c in tsmi.DropDownItems)
  101. {
  102. //if (tsmi != ToolStripSeparator)
  103. //{ }
  104. AppLang(c, resources);
  105. }
  106. }
  107. }
  108. }
  109. }
  110. }