123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- using UnityEngine;
- using System.Collections.Generic;
- namespace ICTS.Localization
- {
- public class Localization : MonoBehaviour
- {
- static Localization mInstance;
- static public Localization instance
- {
- get
- {
- if (mInstance == null)
- {
- mInstance = Object.FindObjectOfType(typeof(Localization)) as Localization;
- if (mInstance == null)
- {
- GameObject go = new GameObject("_Localization");
- DontDestroyOnLoad(go);
- mInstance = go.AddComponent<Localization>();
- }
- }
- return mInstance;
- }
- }
- /// <summary>
- /// Language the localization manager will start with.
- /// </summary>
- public string startingLanguage = "Icelandic";
- public bool forceStartingLanguage;
- /// <summary>
- /// Available list of languages.
- /// </summary>
- public TextAsset[] languages;
- Dictionary<string, string> mDictionary = new Dictionary<string, string>();
- string mLanguage;
- /// <summary>
- /// Name of the currently active language.
- /// </summary>
- public string currentLanguage
- {
- get
- {
- return mLanguage;
- }
- set
- {
- if (mLanguage != value)
- {
- //AV PATCH
- //startingLanguage = value;
- //END OF AV PATCH
- if (!string.IsNullOrEmpty(value))
- {
- // Check the referenced assets first
- if (languages != null)
- {
- for (int i = 0, imax = languages.Length; i < imax; ++i)
- {
- TextAsset asset = languages[i];
- if (asset != null && asset.name == value)
- {
- Load(asset);
- return;
- }
- }
- }
- // Not a referenced asset -- try to load it dynamically
- TextAsset txt = Resources.Load(value, typeof(TextAsset)) as TextAsset;
- if (txt != null)
- {
- Load(txt);
- return;
- }
- }
- // Either the language is null, or it wasn't found
- mDictionary.Clear();
- PlayerPrefs.DeleteKey("Language");
- }
- }
- }
- /// <summary>
- /// Determine the starting language.
- /// </summary>
- void Awake()
- {
- if (mInstance == null)
- {
- mInstance = this;
- DontDestroyOnLoad(gameObject);
- currentLanguage = PlayerPrefs.GetString("Language", null);
- //AV PATCH
- //If there was nothing set in player prefs
- if (currentLanguage == null)
- {
- AVDebug.Log("No language setting found");
- if (forceStartingLanguage)
- {
- currentLanguage = startingLanguage;
- }
- else
- {
- //Get system language
- string systemLanguage = Application.systemLanguage.ToString();
- AVDebug.Log("Trying system language " + systemLanguage);
- currentLanguage = systemLanguage;
- //If it wasn't found use the startingLanguage
- if (string.IsNullOrEmpty(currentLanguage))
- {
- AVDebug.Log(currentLanguage + " not available. Switching to start language " + startingLanguage);
- currentLanguage = startingLanguage;
- AVDebug.Log("using " + currentLanguage);
- }
- }
- }
- /*
- if (string.IsNullOrEmpty(mLanguage) && (languages != null && languages.Length > 0))
- {
- currentLanguage = languages[0].name;
- }*/
- //END OF AV PATCH
- }
- else
- {
- Destroy(gameObject);
- }
- }
- /// <summary>
- /// Oddly enough... sometimes if there is no OnEnable function in Localization, it can get the Awake call after UILocalize's OnEnable.
- /// </summary>
- void OnEnable()
- {
- if (mInstance == null)
- {
- mInstance = this;
- }
- }
- /// <summary>
- /// Remove the instance reference.
- /// </summary>
- void OnDestroy()
- {
- if (mInstance == this)
- {
- mInstance = null;
- }
- }
- /// <summary>
- /// Load the specified asset and activate the localization.
- /// </summary>
- void Load(TextAsset asset)
- {
- mLanguage = asset.name;
- PlayerPrefs.SetString("Language", mLanguage);
- ByteReader reader = new ByteReader(asset);
- mDictionary = reader.ReadDictionary();
- Broadcast("OnLocalize", this);
- }
- /// <summary>
- /// Localize the specified value.
- /// </summary>
- public string Get(string key)
- {
- #if UNITY_EDITOR
- if (!Application.isPlaying) return key;
- #endif
- string val;
- #if UNITY_IPHONE || UNITY_ANDROID
- if (mDictionary.TryGetValue(key + " Mobile", out val)) return val;
- #endif
- #if UNITY_EDITOR
- if (mDictionary.TryGetValue(key, out val)) return val;
- Debug.LogWarning("Localization key not found: '" + key + "'");
- return key;
- #else
- return (mDictionary.TryGetValue(key, out val)) ? val : key;
- #endif
- }
- /// <summary>
- /// Localize the specified value.
- /// </summary>
- static public string Localize(string key)
- {
- return (instance != null) ? instance.Get(key) : key;
- }
- // <summary>
- /// Broadcast the specified message to the entire UI.
- /// </summary>
- public void Broadcast(string funcName)
- {
- transform.BroadcastMessage(funcName, SendMessageOptions.DontRequireReceiver);
- }
- /// <summary>
- /// Broadcast the specified message to the entire UI.
- /// </summary>
- public void Broadcast(string funcName, Localization param)
- {
- if (param == null)
- {
- Debug.LogError("SendMessage is bugged when you try to pass 'null' in the parameter field. It behaves as if no parameter was specified.");
- }
- else
- {
- transform.BroadcastMessage(funcName, param, SendMessageOptions.DontRequireReceiver);
- }
- }
- }
-
- }
|