Localization.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public static class Localization
  5. {
  6. public static Dictionary<string, string[]> dictionary
  7. {
  8. get
  9. {
  10. if (!Localization.localizationHasBeenSet)
  11. {
  12. Localization.language = PlayerPrefs.GetString("Language", "English");
  13. }
  14. return Localization.mDictionary;
  15. }
  16. set
  17. {
  18. Localization.localizationHasBeenSet = (value != null);
  19. Localization.mDictionary = value;
  20. }
  21. }
  22. public static string[] knownLanguages
  23. {
  24. get
  25. {
  26. if (!Localization.localizationHasBeenSet)
  27. {
  28. Localization.LoadDictionary(PlayerPrefs.GetString("Language", "English"));
  29. }
  30. return Localization.mLanguages;
  31. }
  32. }
  33. public static string language
  34. {
  35. get
  36. {
  37. if (string.IsNullOrEmpty(Localization.mLanguage))
  38. {
  39. string[] knownLanguages = Localization.knownLanguages;
  40. Localization.mLanguage = PlayerPrefs.GetString("Language", (knownLanguages == null) ? "English" : knownLanguages[0]);
  41. Localization.LoadAndSelect(Localization.mLanguage);
  42. }
  43. return Localization.mLanguage;
  44. }
  45. set
  46. {
  47. if (Localization.mLanguage != value)
  48. {
  49. Localization.mLanguage = value;
  50. Localization.LoadAndSelect(value);
  51. }
  52. }
  53. }
  54. private static bool LoadDictionary(string value)
  55. {
  56. byte[] array = null;
  57. if (!Localization.localizationHasBeenSet)
  58. {
  59. if (Localization.loadFunction == null)
  60. {
  61. TextAsset textAsset = Resources.Load<TextAsset>("Localization");
  62. if (textAsset != null)
  63. {
  64. array = textAsset.bytes;
  65. }
  66. }
  67. else
  68. {
  69. array = Localization.loadFunction("Localization");
  70. }
  71. Localization.localizationHasBeenSet = true;
  72. }
  73. if (Localization.LoadCSV(array, false))
  74. {
  75. return true;
  76. }
  77. if (string.IsNullOrEmpty(value))
  78. {
  79. return false;
  80. }
  81. if (Localization.loadFunction == null)
  82. {
  83. TextAsset textAsset2 = Resources.Load<TextAsset>(value);
  84. if (textAsset2 != null)
  85. {
  86. array = textAsset2.bytes;
  87. }
  88. }
  89. else
  90. {
  91. array = Localization.loadFunction(value);
  92. }
  93. if (array != null)
  94. {
  95. Localization.Set(value, array);
  96. return true;
  97. }
  98. return false;
  99. }
  100. private static bool LoadAndSelect(string value)
  101. {
  102. if (!string.IsNullOrEmpty(value))
  103. {
  104. if (Localization.mDictionary.Count == 0 && !Localization.LoadDictionary(value))
  105. {
  106. return false;
  107. }
  108. if (Localization.SelectLanguage(value))
  109. {
  110. return true;
  111. }
  112. }
  113. if (Localization.mOldDictionary.Count > 0)
  114. {
  115. return true;
  116. }
  117. Localization.mOldDictionary.Clear();
  118. Localization.mDictionary.Clear();
  119. if (string.IsNullOrEmpty(value))
  120. {
  121. PlayerPrefs.DeleteKey("Language");
  122. }
  123. return false;
  124. }
  125. public static void Load(TextAsset asset)
  126. {
  127. ByteReader byteReader = new ByteReader(asset);
  128. Localization.Set(asset.name, byteReader.ReadDictionary());
  129. }
  130. public static void Set(string languageName, byte[] bytes)
  131. {
  132. ByteReader byteReader = new ByteReader(bytes);
  133. Localization.Set(languageName, byteReader.ReadDictionary());
  134. }
  135. public static bool LoadCSV(TextAsset asset, bool merge = false)
  136. {
  137. return Localization.LoadCSV(asset.bytes, asset, merge);
  138. }
  139. public static bool LoadCSV(byte[] bytes, bool merge = false)
  140. {
  141. return Localization.LoadCSV(bytes, null, merge);
  142. }
  143. private static bool LoadCSV(byte[] bytes, TextAsset asset, bool merge = false)
  144. {
  145. if (bytes == null)
  146. {
  147. return false;
  148. }
  149. ByteReader byteReader = new ByteReader(bytes);
  150. BetterList<string> betterList = byteReader.ReadCSV();
  151. if (betterList.size < 2)
  152. {
  153. return false;
  154. }
  155. if (!merge || betterList.size - 1 != Localization.mLanguage.Length)
  156. {
  157. merge = false;
  158. Localization.mDictionary.Clear();
  159. }
  160. betterList[0] = "KEY";
  161. Localization.mLanguages = new string[betterList.size - 1];
  162. for (int i = 0; i < Localization.mLanguages.Length; i++)
  163. {
  164. Localization.mLanguages[i] = betterList[i + 1];
  165. }
  166. while (betterList != null)
  167. {
  168. Localization.AddCSV(betterList, !merge);
  169. betterList = byteReader.ReadCSV();
  170. }
  171. return true;
  172. }
  173. private static bool SelectLanguage(string language)
  174. {
  175. Localization.mLanguageIndex = -1;
  176. if (Localization.mDictionary.Count == 0)
  177. {
  178. return false;
  179. }
  180. string[] array;
  181. if (Localization.mDictionary.TryGetValue("KEY", out array))
  182. {
  183. for (int i = 0; i < array.Length; i++)
  184. {
  185. if (array[i] == language)
  186. {
  187. Localization.mOldDictionary.Clear();
  188. Localization.mLanguageIndex = i;
  189. Localization.mLanguage = language;
  190. PlayerPrefs.SetString("Language", Localization.mLanguage);
  191. UIRoot.Broadcast("OnLocalize");
  192. return true;
  193. }
  194. }
  195. }
  196. return false;
  197. }
  198. private static void AddCSV(BetterList<string> values, bool warnOnDuplicate = true)
  199. {
  200. if (values.size < 2)
  201. {
  202. return;
  203. }
  204. string text = values[0];
  205. if (string.IsNullOrEmpty(text))
  206. {
  207. return;
  208. }
  209. string[] array = new string[values.size - 1];
  210. for (int i = 1; i < values.size; i++)
  211. {
  212. array[i - 1] = values[i];
  213. }
  214. if (Localization.mDictionary.ContainsKey(text))
  215. {
  216. Localization.mDictionary[text] = array;
  217. if (warnOnDuplicate)
  218. {
  219. Debug.LogWarning("Localization key '" + text + "' is already present");
  220. }
  221. }
  222. else
  223. {
  224. try
  225. {
  226. Localization.mDictionary.Add(text, array);
  227. }
  228. catch (Exception ex)
  229. {
  230. Debug.LogError("Unable to add '" + text + "' to the Localization dictionary.\n" + ex.Message);
  231. }
  232. }
  233. }
  234. public static void Set(string languageName, Dictionary<string, string> dictionary)
  235. {
  236. Localization.mLanguage = languageName;
  237. PlayerPrefs.SetString("Language", Localization.mLanguage);
  238. Localization.mOldDictionary = dictionary;
  239. Localization.localizationHasBeenSet = false;
  240. Localization.mLanguageIndex = -1;
  241. Localization.mLanguages = new string[]
  242. {
  243. languageName
  244. };
  245. UIRoot.Broadcast("OnLocalize");
  246. }
  247. public static string Get(string key)
  248. {
  249. if (!Localization.localizationHasBeenSet)
  250. {
  251. Localization.language = PlayerPrefs.GetString("Language", "English");
  252. }
  253. string[] array;
  254. string result;
  255. if (Localization.mLanguageIndex != -1 && Localization.mDictionary.TryGetValue(key, out array))
  256. {
  257. if (Localization.mLanguageIndex < array.Length)
  258. {
  259. return array[Localization.mLanguageIndex];
  260. }
  261. }
  262. else if (Localization.mOldDictionary.TryGetValue(key, out result))
  263. {
  264. return result;
  265. }
  266. return key;
  267. }
  268. public static string Format(string key, params object[] parameters)
  269. {
  270. return string.Format(Localization.Get(key), parameters);
  271. }
  272. [Obsolete("Localization is now always active. You no longer need to check this property.")]
  273. public static bool isActive
  274. {
  275. get
  276. {
  277. return true;
  278. }
  279. }
  280. [Obsolete("Use Localization.Get instead")]
  281. public static string Localize(string key)
  282. {
  283. return Localization.Get(key);
  284. }
  285. public static bool Exists(string key)
  286. {
  287. if (!Localization.localizationHasBeenSet)
  288. {
  289. Localization.language = PlayerPrefs.GetString("Language", "English");
  290. }
  291. return Localization.mDictionary.ContainsKey(key) || Localization.mOldDictionary.ContainsKey(key);
  292. }
  293. public static Localization.LoadFunction loadFunction;
  294. public static bool localizationHasBeenSet = false;
  295. private static string[] mLanguages = null;
  296. private static Dictionary<string, string> mOldDictionary = new Dictionary<string, string>();
  297. private static Dictionary<string, string[]> mDictionary = new Dictionary<string, string[]>();
  298. private static int mLanguageIndex = -1;
  299. private static string mLanguage;
  300. public delegate byte[] LoadFunction(string path);
  301. }