MultiLang.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System.Collections.Generic;
  2. using System.Xml;
  3. namespace OTSDataType
  4. {
  5. public struct StringRes
  6. {
  7. public int key;
  8. public string text;
  9. public string Description;
  10. }
  11. public class ResGroup
  12. {
  13. public int key;
  14. public string text = "";
  15. public string Description = "";
  16. public SortedDictionary<int, StringRes> mapRes = new SortedDictionary<int, StringRes>();
  17. }
  18. public class MultiLang
  19. {
  20. static MultiLang instance =new MultiLang();
  21. private SortedDictionary<int, ResGroup> resGroup = new SortedDictionary<int, ResGroup>();
  22. public MultiLang()
  23. {
  24. LoadStringFromXml();
  25. }
  26. public void GetStringByKey(int grpKey, int itmKey, ref string text, ref string des)
  27. {
  28. text = resGroup[grpKey].mapRes[itmKey].text;
  29. des = resGroup[grpKey].mapRes[itmKey].Description;
  30. }
  31. public string GetStringByKey(int grpKey, int itmKey)
  32. {
  33. return resGroup[grpKey].mapRes[itmKey].text;
  34. }
  35. public string GetGroupTextByKey(int grpKey)
  36. {
  37. return resGroup[grpKey].text;
  38. }
  39. public void GetGroupTextByKey(int grpKey, ref string text, ref string des)
  40. {
  41. text = resGroup[grpKey].text;
  42. des = resGroup[grpKey].Description;
  43. }
  44. public void SetStringByKey(int grpkey, int itmkey, string value, string des)
  45. {
  46. StringRes sr = new StringRes();
  47. sr.text = value;
  48. sr.Description = des;
  49. resGroup[grpkey].mapRes[itmkey] = sr;
  50. }
  51. public bool LoadStringFromXml()
  52. {
  53. XmlDocument xml = new XmlDocument();
  54. xml.Load(".\\Resources\\XMLData\\LanguageDefine.xml");
  55. XmlNode root = xml.SelectSingleNode("Language");
  56. XmlNode root2 = root.SelectSingleNode("DefaultLanguage");
  57. string ss = root2.InnerText;
  58. if (ss == "EN")
  59. {
  60. // xml.Load(".\\Resources\\XMLData\\ResourceForSourceGrid-EN.xml");//载入xml文件
  61. xml.Load(".\\Resources\\Resources\\XMLData\\ResourceForCpp\\ResourceForSTDManage-EN.xml");
  62. }
  63. else if (ss == "ZH")
  64. {
  65. // xml.Load(".\\Resources\\XMLData\\ResourceForSourceGrid-ZH.xml");//载入xml文件
  66. // xml.Load(".\\Resources\\XMLData\\LanguageDefine.xml");
  67. xml.Load(".\\Resources\\XMLData\\ResourceForCpp-ZH.xml");
  68. }
  69. XmlDocument xml1 = new XmlDocument();
  70. XmlNode root3 = xml1.SelectSingleNode("collection");
  71. XmlNode root4 = root3.SelectSingleNode("member");
  72. XmlNodeList childlist = root2.ChildNodes;
  73. for (int i = 0; i < childlist.Count; i++)
  74. {
  75. int colkey = -1;
  76. if (childlist[i].Attributes["grpKey"] != null)
  77. {
  78. colkey = int.Parse(childlist[i].Attributes["grpKey"].Value);
  79. }
  80. string colText = "";
  81. if (childlist[i].Attributes["text"] != null)
  82. {
  83. colText = childlist[i].Attributes["text"].Value;
  84. }
  85. string colDes = "";
  86. if (childlist[i].Attributes["description"] != null)
  87. {
  88. colDes = childlist[i].Attributes["description"].Value;
  89. }
  90. ResGroup rg = new ResGroup();
  91. rg.key = colkey;
  92. rg.text = colText;
  93. rg.Description = colDes;
  94. XmlNodeList childlist2 = childlist[i].ChildNodes;
  95. for (int j = 0; j < childlist2.Count; j++)
  96. {
  97. StringRes sr = new StringRes();
  98. int key = int.Parse(childlist2[j].Attributes["itemKey"].Value);
  99. sr.key = key;
  100. sr.text = childlist2[j].Attributes["itemText"].Value;
  101. if (childlist2[j].Attributes["description"] != null)
  102. {
  103. sr.Description = childlist2[j].Attributes["description"].Value;
  104. }
  105. else
  106. {
  107. sr.Description = "空";
  108. }
  109. //try
  110. //{
  111. rg.mapRes[key] = sr;
  112. //}
  113. //catch (System.Exception ex)
  114. //{
  115. // // MessageBox.Show(ex.ToString());
  116. //}
  117. }
  118. resGroup[colkey] = rg;
  119. }
  120. return true;
  121. }
  122. //public bool SaveStringToXml()
  123. //{
  124. // if (File.Exists(".\\OPTON\\OTSIncA\\SysData\\MultiLangForCpp.xml"))
  125. // {
  126. // XDocument xdoc = XDocument.Load(".\\OPTON\\OTSIncA\\SysData\\MultiLangForCpp.xml");
  127. // IEnumerable<XElement> elements = from ele in xdoc.Descendants("collection") select ele;
  128. // foreach (var ele in elements)
  129. // {
  130. // if (ele != null)
  131. // {
  132. // ele.RemoveAll();
  133. // }
  134. // }
  135. // IEnumerable<XElement> cls = from ele in xdoc.Descendants("collection") select ele;
  136. // foreach (KeyValuePair<int, string> kvp in mapMultiLang)
  137. // {
  138. // XElement EleName = new XElement("member");
  139. // EleName.SetAttributeValue("itemKey", kvp.Key.ToString());
  140. // EleName.SetAttributeValue("itemName", "");
  141. // EleName.SetAttributeValue("itemText", kvp.Value.ToString());
  142. // cls.First().Add(EleName);
  143. // }
  144. // xdoc.Save(".\\OPTON\\OTSIncA\\SysData\\MultiLangForCpp.xml");
  145. // }
  146. // else
  147. // {
  148. // XDocument xdoc = new XDocument();
  149. // XElement root = new XElement("XMLData");
  150. // XElement cls = new XElement("collection");
  151. // foreach (KeyValuePair<int, string> kvp in mapMultiLang)
  152. // {
  153. // XElement EleName = new XElement("member");
  154. // EleName.SetAttributeValue("itemKey", kvp.Key.ToString());
  155. // EleName.SetAttributeValue("itemName", "");
  156. // EleName.SetAttributeValue("itemText", kvp.Value.ToString());
  157. // cls.Add(EleName);
  158. // }
  159. // root.Add(cls);
  160. // xdoc.Add(root);
  161. // xdoc.Save(".\\OPTON\\OTSIncA\\SysData\\MultiLangForCpp.xml");
  162. // }
  163. // return true;
  164. //}
  165. public static MultiLang GetInstance()
  166. {
  167. //static MultiLang instance;
  168. //instance.LoadStringFromXml();
  169. return instance;
  170. }
  171. }
  172. }