Localization.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. namespace ICTS.Localization
  4. {
  5. public class Localization : MonoBehaviour
  6. {
  7. static Localization mInstance;
  8. static public Localization instance
  9. {
  10. get
  11. {
  12. if (mInstance == null)
  13. {
  14. mInstance = Object.FindObjectOfType(typeof(Localization)) as Localization;
  15. if (mInstance == null)
  16. {
  17. GameObject go = new GameObject("_Localization");
  18. DontDestroyOnLoad(go);
  19. mInstance = go.AddComponent<Localization>();
  20. }
  21. }
  22. return mInstance;
  23. }
  24. }
  25. /// <summary>
  26. /// Language the localization manager will start with.
  27. /// </summary>
  28. public string startingLanguage = "Icelandic";
  29. public bool forceStartingLanguage;
  30. /// <summary>
  31. /// Available list of languages.
  32. /// </summary>
  33. public TextAsset[] languages;
  34. Dictionary<string, string> mDictionary = new Dictionary<string, string>();
  35. string mLanguage;
  36. /// <summary>
  37. /// Name of the currently active language.
  38. /// </summary>
  39. public string currentLanguage
  40. {
  41. get
  42. {
  43. return mLanguage;
  44. }
  45. set
  46. {
  47. if (mLanguage != value)
  48. {
  49. //AV PATCH
  50. //startingLanguage = value;
  51. //END OF AV PATCH
  52. if (!string.IsNullOrEmpty(value))
  53. {
  54. // Check the referenced assets first
  55. if (languages != null)
  56. {
  57. for (int i = 0, imax = languages.Length; i < imax; ++i)
  58. {
  59. TextAsset asset = languages[i];
  60. if (asset != null && asset.name == value)
  61. {
  62. Load(asset);
  63. return;
  64. }
  65. }
  66. }
  67. // Not a referenced asset -- try to load it dynamically
  68. TextAsset txt = Resources.Load(value, typeof(TextAsset)) as TextAsset;
  69. if (txt != null)
  70. {
  71. Load(txt);
  72. return;
  73. }
  74. }
  75. // Either the language is null, or it wasn't found
  76. mDictionary.Clear();
  77. PlayerPrefs.DeleteKey("Language");
  78. }
  79. }
  80. }
  81. /// <summary>
  82. /// Determine the starting language.
  83. /// </summary>
  84. void Awake()
  85. {
  86. if (mInstance == null)
  87. {
  88. mInstance = this;
  89. DontDestroyOnLoad(gameObject);
  90. currentLanguage = PlayerPrefs.GetString("Language", null);
  91. //AV PATCH
  92. //If there was nothing set in player prefs
  93. if (currentLanguage == null)
  94. {
  95. AVDebug.Log("No language setting found");
  96. if (forceStartingLanguage)
  97. {
  98. currentLanguage = startingLanguage;
  99. }
  100. else
  101. {
  102. //Get system language
  103. string systemLanguage = Application.systemLanguage.ToString();
  104. AVDebug.Log("Trying system language " + systemLanguage);
  105. currentLanguage = systemLanguage;
  106. //If it wasn't found use the startingLanguage
  107. if (string.IsNullOrEmpty(currentLanguage))
  108. {
  109. AVDebug.Log(currentLanguage + " not available. Switching to start language " + startingLanguage);
  110. currentLanguage = startingLanguage;
  111. AVDebug.Log("using " + currentLanguage);
  112. }
  113. }
  114. }
  115. /*
  116. if (string.IsNullOrEmpty(mLanguage) && (languages != null && languages.Length > 0))
  117. {
  118. currentLanguage = languages[0].name;
  119. }*/
  120. //END OF AV PATCH
  121. }
  122. else
  123. {
  124. Destroy(gameObject);
  125. }
  126. }
  127. /// <summary>
  128. /// Oddly enough... sometimes if there is no OnEnable function in Localization, it can get the Awake call after UILocalize's OnEnable.
  129. /// </summary>
  130. void OnEnable()
  131. {
  132. if (mInstance == null)
  133. {
  134. mInstance = this;
  135. }
  136. }
  137. /// <summary>
  138. /// Remove the instance reference.
  139. /// </summary>
  140. void OnDestroy()
  141. {
  142. if (mInstance == this)
  143. {
  144. mInstance = null;
  145. }
  146. }
  147. /// <summary>
  148. /// Load the specified asset and activate the localization.
  149. /// </summary>
  150. void Load(TextAsset asset)
  151. {
  152. mLanguage = asset.name;
  153. PlayerPrefs.SetString("Language", mLanguage);
  154. ByteReader reader = new ByteReader(asset);
  155. mDictionary = reader.ReadDictionary();
  156. Broadcast("OnLocalize", this);
  157. }
  158. /// <summary>
  159. /// Localize the specified value.
  160. /// </summary>
  161. public string Get(string key)
  162. {
  163. #if UNITY_EDITOR
  164. if (!Application.isPlaying) return key;
  165. #endif
  166. string val;
  167. #if UNITY_IPHONE || UNITY_ANDROID
  168. if (mDictionary.TryGetValue(key + " Mobile", out val)) return val;
  169. #endif
  170. #if UNITY_EDITOR
  171. if (mDictionary.TryGetValue(key, out val)) return val;
  172. Debug.LogWarning("Localization key not found: '" + key + "'");
  173. return key;
  174. #else
  175. return (mDictionary.TryGetValue(key, out val)) ? val : key;
  176. #endif
  177. }
  178. /// <summary>
  179. /// Localize the specified value.
  180. /// </summary>
  181. static public string Localize(string key)
  182. {
  183. return (instance != null) ? instance.Get(key) : key;
  184. }
  185. // <summary>
  186. /// Broadcast the specified message to the entire UI.
  187. /// </summary>
  188. public void Broadcast(string funcName)
  189. {
  190. transform.BroadcastMessage(funcName, SendMessageOptions.DontRequireReceiver);
  191. }
  192. /// <summary>
  193. /// Broadcast the specified message to the entire UI.
  194. /// </summary>
  195. public void Broadcast(string funcName, Localization param)
  196. {
  197. if (param == null)
  198. {
  199. Debug.LogError("SendMessage is bugged when you try to pass 'null' in the parameter field. It behaves as if no parameter was specified.");
  200. }
  201. else
  202. {
  203. transform.BroadcastMessage(funcName, param, SendMessageOptions.DontRequireReceiver);
  204. }
  205. }
  206. }
  207. }