123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- public static class Localization
- {
- public static Dictionary<string, string[]> dictionary
- {
- get
- {
- if (!Localization.localizationHasBeenSet)
- {
- Localization.language = PlayerPrefs.GetString("Language", "English");
- }
- return Localization.mDictionary;
- }
- set
- {
- Localization.localizationHasBeenSet = (value != null);
- Localization.mDictionary = value;
- }
- }
- public static string[] knownLanguages
- {
- get
- {
- if (!Localization.localizationHasBeenSet)
- {
- Localization.LoadDictionary(PlayerPrefs.GetString("Language", "English"));
- }
- return Localization.mLanguages;
- }
- }
- public static string language
- {
- get
- {
- if (string.IsNullOrEmpty(Localization.mLanguage))
- {
- string[] knownLanguages = Localization.knownLanguages;
- Localization.mLanguage = PlayerPrefs.GetString("Language", (knownLanguages == null) ? "English" : knownLanguages[0]);
- Localization.LoadAndSelect(Localization.mLanguage);
- }
- return Localization.mLanguage;
- }
- set
- {
- if (Localization.mLanguage != value)
- {
- Localization.mLanguage = value;
- Localization.LoadAndSelect(value);
- }
- }
- }
- private static bool LoadDictionary(string value)
- {
- byte[] array = null;
- if (!Localization.localizationHasBeenSet)
- {
- if (Localization.loadFunction == null)
- {
- TextAsset textAsset = Resources.Load<TextAsset>("Localization");
- if (textAsset != null)
- {
- array = textAsset.bytes;
- }
- }
- else
- {
- array = Localization.loadFunction("Localization");
- }
- Localization.localizationHasBeenSet = true;
- }
- if (Localization.LoadCSV(array, false))
- {
- return true;
- }
- if (string.IsNullOrEmpty(value))
- {
- return false;
- }
- if (Localization.loadFunction == null)
- {
- TextAsset textAsset2 = Resources.Load<TextAsset>(value);
- if (textAsset2 != null)
- {
- array = textAsset2.bytes;
- }
- }
- else
- {
- array = Localization.loadFunction(value);
- }
- if (array != null)
- {
- Localization.Set(value, array);
- return true;
- }
- return false;
- }
- private static bool LoadAndSelect(string value)
- {
- if (!string.IsNullOrEmpty(value))
- {
- if (Localization.mDictionary.Count == 0 && !Localization.LoadDictionary(value))
- {
- return false;
- }
- if (Localization.SelectLanguage(value))
- {
- return true;
- }
- }
- if (Localization.mOldDictionary.Count > 0)
- {
- return true;
- }
- Localization.mOldDictionary.Clear();
- Localization.mDictionary.Clear();
- if (string.IsNullOrEmpty(value))
- {
- PlayerPrefs.DeleteKey("Language");
- }
- return false;
- }
- public static void Load(TextAsset asset)
- {
- ByteReader byteReader = new ByteReader(asset);
- Localization.Set(asset.name, byteReader.ReadDictionary());
- }
- public static void Set(string languageName, byte[] bytes)
- {
- ByteReader byteReader = new ByteReader(bytes);
- Localization.Set(languageName, byteReader.ReadDictionary());
- }
- public static bool LoadCSV(TextAsset asset, bool merge = false)
- {
- return Localization.LoadCSV(asset.bytes, asset, merge);
- }
- public static bool LoadCSV(byte[] bytes, bool merge = false)
- {
- return Localization.LoadCSV(bytes, null, merge);
- }
- private static bool LoadCSV(byte[] bytes, TextAsset asset, bool merge = false)
- {
- if (bytes == null)
- {
- return false;
- }
- ByteReader byteReader = new ByteReader(bytes);
- BetterList<string> betterList = byteReader.ReadCSV();
- if (betterList.size < 2)
- {
- return false;
- }
- if (!merge || betterList.size - 1 != Localization.mLanguage.Length)
- {
- merge = false;
- Localization.mDictionary.Clear();
- }
- betterList[0] = "KEY";
- Localization.mLanguages = new string[betterList.size - 1];
- for (int i = 0; i < Localization.mLanguages.Length; i++)
- {
- Localization.mLanguages[i] = betterList[i + 1];
- }
- while (betterList != null)
- {
- Localization.AddCSV(betterList, !merge);
- betterList = byteReader.ReadCSV();
- }
- return true;
- }
- private static bool SelectLanguage(string language)
- {
- Localization.mLanguageIndex = -1;
- if (Localization.mDictionary.Count == 0)
- {
- return false;
- }
- string[] array;
- if (Localization.mDictionary.TryGetValue("KEY", out array))
- {
- for (int i = 0; i < array.Length; i++)
- {
- if (array[i] == language)
- {
- Localization.mOldDictionary.Clear();
- Localization.mLanguageIndex = i;
- Localization.mLanguage = language;
- PlayerPrefs.SetString("Language", Localization.mLanguage);
- UIRoot.Broadcast("OnLocalize");
- return true;
- }
- }
- }
- return false;
- }
- private static void AddCSV(BetterList<string> values, bool warnOnDuplicate = true)
- {
- if (values.size < 2)
- {
- return;
- }
- string text = values[0];
- if (string.IsNullOrEmpty(text))
- {
- return;
- }
- string[] array = new string[values.size - 1];
- for (int i = 1; i < values.size; i++)
- {
- array[i - 1] = values[i];
- }
- if (Localization.mDictionary.ContainsKey(text))
- {
- Localization.mDictionary[text] = array;
- if (warnOnDuplicate)
- {
- Debug.LogWarning("Localization key '" + text + "' is already present");
- }
- }
- else
- {
- try
- {
- Localization.mDictionary.Add(text, array);
- }
- catch (Exception ex)
- {
- Debug.LogError("Unable to add '" + text + "' to the Localization dictionary.\n" + ex.Message);
- }
- }
- }
- public static void Set(string languageName, Dictionary<string, string> dictionary)
- {
- Localization.mLanguage = languageName;
- PlayerPrefs.SetString("Language", Localization.mLanguage);
- Localization.mOldDictionary = dictionary;
- Localization.localizationHasBeenSet = false;
- Localization.mLanguageIndex = -1;
- Localization.mLanguages = new string[]
- {
- languageName
- };
- UIRoot.Broadcast("OnLocalize");
- }
- public static string Get(string key)
- {
- if (!Localization.localizationHasBeenSet)
- {
- Localization.language = PlayerPrefs.GetString("Language", "English");
- }
- string[] array;
- string result;
- if (Localization.mLanguageIndex != -1 && Localization.mDictionary.TryGetValue(key, out array))
- {
- if (Localization.mLanguageIndex < array.Length)
- {
- return array[Localization.mLanguageIndex];
- }
- }
- else if (Localization.mOldDictionary.TryGetValue(key, out result))
- {
- return result;
- }
- return key;
- }
- public static string Format(string key, params object[] parameters)
- {
- return string.Format(Localization.Get(key), parameters);
- }
- [Obsolete("Localization is now always active. You no longer need to check this property.")]
- public static bool isActive
- {
- get
- {
- return true;
- }
- }
- [Obsolete("Use Localization.Get instead")]
- public static string Localize(string key)
- {
- return Localization.Get(key);
- }
- public static bool Exists(string key)
- {
- if (!Localization.localizationHasBeenSet)
- {
- Localization.language = PlayerPrefs.GetString("Language", "English");
- }
- return Localization.mDictionary.ContainsKey(key) || Localization.mOldDictionary.ContainsKey(key);
- }
- public static Localization.LoadFunction loadFunction;
- public static bool localizationHasBeenSet = false;
- private static string[] mLanguages = null;
- private static Dictionary<string, string> mOldDictionary = new Dictionary<string, string>();
- private static Dictionary<string, string[]> mDictionary = new Dictionary<string, string[]>();
- private static int mLanguageIndex = -1;
- private static string mLanguage;
- public delegate byte[] LoadFunction(string path);
- }
|