TranslationHelper.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. using System.Collections.Generic;
  2. using FairyGUI.Utils;
  3. namespace FairyGUI
  4. {
  5. public class TranslationHelper
  6. {
  7. public static Dictionary<string, Dictionary<string, string>> strings;
  8. public static void LoadFromXML(XML source)
  9. {
  10. strings = new Dictionary<string, Dictionary<string, string>>();
  11. XMLList.Enumerator et = source.GetEnumerator("string");
  12. while (et.MoveNext())
  13. {
  14. XML cxml = et.Current;
  15. string key = cxml.GetAttribute("name");
  16. string text = cxml.text;
  17. int i = key.IndexOf("-");
  18. if (i == -1)
  19. continue;
  20. string key2 = key.Substring(0, i);
  21. string key3 = key.Substring(i + 1);
  22. Dictionary<string, string> col;
  23. if (!strings.TryGetValue(key2, out col))
  24. {
  25. col = new Dictionary<string, string>();
  26. strings[key2] = col;
  27. }
  28. col[key3] = text;
  29. }
  30. }
  31. public static void TranslateComponent(PackageItem item)
  32. {
  33. if (TranslationHelper.strings == null)
  34. return;
  35. Dictionary<string, string> strings;
  36. if (!TranslationHelper.strings.TryGetValue(item.owner.id + item.id, out strings))
  37. return;
  38. string elementId, value;
  39. ByteBuffer buffer = item.rawData;
  40. buffer.Seek(0, 2);
  41. int childCount = buffer.ReadShort();
  42. for (int i = 0; i < childCount; i++)
  43. {
  44. int dataLen = buffer.ReadShort();
  45. int curPos = buffer.position;
  46. buffer.Seek(curPos, 0);
  47. ObjectType baseType = (ObjectType)buffer.ReadByte();
  48. ObjectType type = baseType;
  49. buffer.Skip(4);
  50. elementId = buffer.ReadS();
  51. if (type == ObjectType.Component)
  52. {
  53. if (buffer.Seek(curPos, 6))
  54. type = (ObjectType)buffer.ReadByte();
  55. }
  56. buffer.Seek(curPos, 1);
  57. if (strings.TryGetValue(elementId + "-tips", out value))
  58. buffer.WriteS(value);
  59. buffer.Seek(curPos, 2);
  60. int gearCnt = buffer.ReadShort();
  61. for (int j = 0; j < gearCnt; j++)
  62. {
  63. int nextPos = buffer.ReadUshort();
  64. nextPos += buffer.position;
  65. if (buffer.ReadByte() == 6) //gearText
  66. {
  67. buffer.Skip(2);//controller
  68. int valueCnt = buffer.ReadShort();
  69. for (int k = 0; k < valueCnt; k++)
  70. {
  71. string page = buffer.ReadS();
  72. if (page != null)
  73. {
  74. if (strings.TryGetValue(elementId + "-texts_" + k, out value))
  75. buffer.WriteS(value);
  76. else
  77. buffer.Skip(2);
  78. }
  79. }
  80. if (buffer.ReadBool() && strings.TryGetValue(elementId + "-texts_def", out value))
  81. buffer.WriteS(value);
  82. }
  83. buffer.position = nextPos;
  84. }
  85. if (baseType == ObjectType.Component && buffer.version >= 2)
  86. {
  87. buffer.Seek(curPos, 4);
  88. buffer.Skip(2); //pageController
  89. buffer.Skip(4 * buffer.ReadShort());
  90. int cpCount = buffer.ReadShort();
  91. for (int k = 0; k < cpCount; k++)
  92. {
  93. string target = buffer.ReadS();
  94. int propertyId = buffer.ReadShort();
  95. if (propertyId == 0 && strings.TryGetValue(elementId + "-cp-" + target, out value))
  96. buffer.WriteS(value);
  97. else
  98. buffer.Skip(2);
  99. }
  100. }
  101. switch (type)
  102. {
  103. case ObjectType.Text:
  104. case ObjectType.RichText:
  105. case ObjectType.InputText:
  106. {
  107. if (strings.TryGetValue(elementId, out value))
  108. {
  109. buffer.Seek(curPos, 6);
  110. buffer.WriteS(value);
  111. }
  112. if (strings.TryGetValue(elementId + "-prompt", out value))
  113. {
  114. buffer.Seek(curPos, 4);
  115. buffer.WriteS(value);
  116. }
  117. break;
  118. }
  119. case ObjectType.List:
  120. case ObjectType.Tree:
  121. {
  122. buffer.Seek(curPos, 8);
  123. buffer.Skip(2);
  124. int itemCount = buffer.ReadShort();
  125. for (int j = 0; j < itemCount; j++)
  126. {
  127. int nextPos = buffer.ReadUshort();
  128. nextPos += buffer.position;
  129. buffer.Skip(2); //url
  130. if (type == ObjectType.Tree)
  131. buffer.Skip(2);
  132. //title
  133. if (strings.TryGetValue(elementId + "-" + j, out value))
  134. buffer.WriteS(value);
  135. else
  136. buffer.Skip(2);
  137. //selected title
  138. if (strings.TryGetValue(elementId + "-" + j + "-0", out value))
  139. buffer.WriteS(value);
  140. else
  141. buffer.Skip(2);
  142. if (buffer.version >= 2)
  143. {
  144. buffer.Skip(6);
  145. buffer.Skip(buffer.ReadShort() * 4);//controllers
  146. int cpCount = buffer.ReadShort();
  147. for (int k = 0; k < cpCount; k++)
  148. {
  149. string target = buffer.ReadS();
  150. int propertyId = buffer.ReadShort();
  151. if (propertyId == 0 && strings.TryGetValue(elementId + "-" + j + "-" + target, out value))
  152. buffer.WriteS(value);
  153. else
  154. buffer.Skip(2);
  155. }
  156. }
  157. buffer.position = nextPos;
  158. }
  159. break;
  160. }
  161. case ObjectType.Label:
  162. {
  163. if (buffer.Seek(curPos, 6) && (ObjectType)buffer.ReadByte() == type)
  164. {
  165. if (strings.TryGetValue(elementId, out value))
  166. buffer.WriteS(value);
  167. else
  168. buffer.Skip(2);
  169. buffer.Skip(2);
  170. if (buffer.ReadBool())
  171. buffer.Skip(4);
  172. buffer.Skip(4);
  173. if (buffer.ReadBool() && strings.TryGetValue(elementId + "-prompt", out value))
  174. buffer.WriteS(value);
  175. }
  176. break;
  177. }
  178. case ObjectType.Button:
  179. {
  180. if (buffer.Seek(curPos, 6) && (ObjectType)buffer.ReadByte() == type)
  181. {
  182. if (strings.TryGetValue(elementId, out value))
  183. buffer.WriteS(value);
  184. else
  185. buffer.Skip(2);
  186. if (strings.TryGetValue(elementId + "-0", out value))
  187. buffer.WriteS(value);
  188. }
  189. break;
  190. }
  191. case ObjectType.ComboBox:
  192. {
  193. if (buffer.Seek(curPos, 6) && (ObjectType)buffer.ReadByte() == type)
  194. {
  195. int itemCount = buffer.ReadShort();
  196. for (int j = 0; j < itemCount; j++)
  197. {
  198. int nextPos = buffer.ReadUshort();
  199. nextPos += buffer.position;
  200. if (strings.TryGetValue(elementId + "-" + j, out value))
  201. buffer.WriteS(value);
  202. buffer.position = nextPos;
  203. }
  204. if (strings.TryGetValue(elementId, out value))
  205. buffer.WriteS(value);
  206. }
  207. break;
  208. }
  209. }
  210. buffer.position = curPos + dataLen;
  211. }
  212. }
  213. }
  214. }