123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- using System.Text.RegularExpressions;
- using System.Threading;
- using UnityEngine;
- namespace I2.Loc
- {
- public static class LocalizationManager
- {
- public static void InitializeIfNeeded()
- {
- if (string.IsNullOrEmpty(LocalizationManager.mCurrentLanguage) || LocalizationManager.Sources.Count == 0)
- {
- LocalizationManager.UpdateSources();
- LocalizationManager.SelectStartupLanguage();
- }
- }
- public static string GetVersion()
- {
- return "2.8.6 f2";
- }
- public static int GetRequiredWebServiceVersion()
- {
- return 5;
- }
- public static string GetWebServiceURL(LanguageSource source = null)
- {
- if (source != null && !string.IsNullOrEmpty(source.Google_WebServiceURL))
- {
- return source.Google_WebServiceURL;
- }
- LocalizationManager.InitializeIfNeeded();
- for (int i = 0; i < LocalizationManager.Sources.Count; i++)
- {
- if (LocalizationManager.Sources[i] != null && !string.IsNullOrEmpty(LocalizationManager.Sources[i].Google_WebServiceURL))
- {
- return LocalizationManager.Sources[i].Google_WebServiceURL;
- }
- }
- return string.Empty;
- }
- public static string CurrentLanguage
- {
- get
- {
- LocalizationManager.InitializeIfNeeded();
- return LocalizationManager.mCurrentLanguage;
- }
- set
- {
- LocalizationManager.InitializeIfNeeded();
- string supportedLanguage = LocalizationManager.GetSupportedLanguage(value);
- if (!string.IsNullOrEmpty(supportedLanguage) && LocalizationManager.mCurrentLanguage != supportedLanguage)
- {
- LocalizationManager.SetLanguageAndCode(supportedLanguage, LocalizationManager.GetLanguageCode(supportedLanguage), true, false);
- }
- }
- }
- public static string CurrentLanguageCode
- {
- get
- {
- LocalizationManager.InitializeIfNeeded();
- return LocalizationManager.mLanguageCode;
- }
- set
- {
- LocalizationManager.InitializeIfNeeded();
- if (LocalizationManager.mLanguageCode != value)
- {
- string languageFromCode = LocalizationManager.GetLanguageFromCode(value, true);
- if (!string.IsNullOrEmpty(languageFromCode))
- {
- LocalizationManager.SetLanguageAndCode(languageFromCode, value, true, false);
- }
- }
- }
- }
- public static string CurrentRegion
- {
- get
- {
- string currentLanguage = LocalizationManager.CurrentLanguage;
- int num = currentLanguage.IndexOfAny("/\\".ToCharArray());
- if (num > 0)
- {
- return currentLanguage.Substring(num + 1);
- }
- num = currentLanguage.IndexOfAny("[(".ToCharArray());
- int num2 = currentLanguage.LastIndexOfAny("])".ToCharArray());
- if (num > 0 && num != num2)
- {
- return currentLanguage.Substring(num + 1, num2 - num - 1);
- }
- return string.Empty;
- }
- set
- {
- string text = LocalizationManager.CurrentLanguage;
- int num = text.IndexOfAny("/\\".ToCharArray());
- if (num > 0)
- {
- LocalizationManager.CurrentLanguage = text.Substring(num + 1) + value;
- return;
- }
- num = text.IndexOfAny("[(".ToCharArray());
- int num2 = text.LastIndexOfAny("])".ToCharArray());
- if (num > 0 && num != num2)
- {
- text = text.Substring(num);
- }
- LocalizationManager.CurrentLanguage = text + "(" + value + ")";
- }
- }
- public static string CurrentRegionCode
- {
- get
- {
- string currentLanguageCode = LocalizationManager.CurrentLanguageCode;
- int num = currentLanguageCode.IndexOfAny(" -_/\\".ToCharArray());
- return (num >= 0) ? currentLanguageCode.Substring(num + 1) : string.Empty;
- }
- set
- {
- string text = LocalizationManager.CurrentLanguageCode;
- int num = text.IndexOfAny(" -_/\\".ToCharArray());
- if (num > 0)
- {
- text = text.Substring(0, num);
- }
- LocalizationManager.CurrentLanguageCode = text + "-" + value;
- }
- }
- public static CultureInfo CurrentCulture
- {
- get
- {
- return LocalizationManager.mCurrentCulture;
- }
- }
- public static void SetLanguageAndCode(string LanguageName, string LanguageCode, bool RememberLanguage = true, bool Force = false)
- {
- if (LocalizationManager.mCurrentLanguage != LanguageName || LocalizationManager.mLanguageCode != LanguageCode || Force)
- {
- if (RememberLanguage)
- {
- PersistentStorage.SetSetting_String("I2 Language", LanguageName);
- }
- LocalizationManager.mCurrentLanguage = LanguageName;
- LocalizationManager.mLanguageCode = LanguageCode;
- LocalizationManager.mCurrentCulture = LocalizationManager.CreateCultureForCode(LanguageCode);
- if (LocalizationManager.mChangeCultureInfo)
- {
- LocalizationManager.SetCurrentCultureInfo();
- }
- LocalizationManager.IsRight2Left = LocalizationManager.IsRTL(LocalizationManager.mLanguageCode);
- LocalizationManager.HasJoinedWords = GoogleLanguages.LanguageCode_HasJoinedWord(LocalizationManager.mLanguageCode);
- LocalizationManager.LocalizeAll(Force);
- }
- }
- private static CultureInfo CreateCultureForCode(string code)
- {
- CultureInfo result;
- try
- {
- result = CultureInfo.CreateSpecificCulture(code);
- }
- catch (Exception)
- {
- result = CultureInfo.InvariantCulture;
- }
- return result;
- }
- public static void EnableChangingCultureInfo(bool bEnable)
- {
- if (!LocalizationManager.mChangeCultureInfo && bEnable)
- {
- LocalizationManager.SetCurrentCultureInfo();
- }
- LocalizationManager.mChangeCultureInfo = bEnable;
- }
- private static void SetCurrentCultureInfo()
- {
- Thread.CurrentThread.CurrentCulture = LocalizationManager.mCurrentCulture;
- }
- private static void SelectStartupLanguage()
- {
- if (LocalizationManager.Sources.Count == 0)
- {
- return;
- }
- string setting_String = PersistentStorage.GetSetting_String("I2 Language", string.Empty);
- string currentDeviceLanguage = LocalizationManager.GetCurrentDeviceLanguage();
- if (!string.IsNullOrEmpty(setting_String) && LocalizationManager.HasLanguage(setting_String, true, false, true))
- {
- LocalizationManager.SetLanguageAndCode(setting_String, LocalizationManager.GetLanguageCode(setting_String), true, false);
- return;
- }
- if (!LocalizationManager.Sources[0].IgnoreDeviceLanguage)
- {
- string supportedLanguage = LocalizationManager.GetSupportedLanguage(currentDeviceLanguage);
- if (!string.IsNullOrEmpty(supportedLanguage))
- {
- LocalizationManager.SetLanguageAndCode(supportedLanguage, LocalizationManager.GetLanguageCode(supportedLanguage), false, false);
- return;
- }
- }
- int i = 0;
- int count = LocalizationManager.Sources.Count;
- while (i < count)
- {
- if (LocalizationManager.Sources[i].mLanguages.Count > 0)
- {
- for (int j = 0; j < LocalizationManager.Sources[i].mLanguages.Count; j++)
- {
- if (LocalizationManager.Sources[i].mLanguages[j].IsEnabled())
- {
- LocalizationManager.SetLanguageAndCode(LocalizationManager.Sources[i].mLanguages[j].Name, LocalizationManager.Sources[i].mLanguages[j].Code, false, false);
- return;
- }
- }
- }
- i++;
- }
- }
- public static bool HasLanguage(string Language, bool AllowDiscartingRegion = true, bool Initialize = true, bool SkipDisabled = true)
- {
- if (Initialize)
- {
- LocalizationManager.InitializeIfNeeded();
- }
- int i = 0;
- int count = LocalizationManager.Sources.Count;
- while (i < count)
- {
- if (LocalizationManager.Sources[i].GetLanguageIndex(Language, false, SkipDisabled) >= 0)
- {
- return true;
- }
- i++;
- }
- if (AllowDiscartingRegion)
- {
- int j = 0;
- int count2 = LocalizationManager.Sources.Count;
- while (j < count2)
- {
- if (LocalizationManager.Sources[j].GetLanguageIndex(Language, true, SkipDisabled) >= 0)
- {
- return true;
- }
- j++;
- }
- }
- return false;
- }
- public static string GetSupportedLanguage(string Language)
- {
- string languageCode = GoogleLanguages.GetLanguageCode(Language, false);
- if (!string.IsNullOrEmpty(languageCode))
- {
- int i = 0;
- int count = LocalizationManager.Sources.Count;
- while (i < count)
- {
- int languageIndexFromCode = LocalizationManager.Sources[i].GetLanguageIndexFromCode(languageCode, true);
- if (languageIndexFromCode >= 0)
- {
- return LocalizationManager.Sources[i].mLanguages[languageIndexFromCode].Name;
- }
- i++;
- }
- int j = 0;
- int count2 = LocalizationManager.Sources.Count;
- while (j < count2)
- {
- int languageIndexFromCode2 = LocalizationManager.Sources[j].GetLanguageIndexFromCode(languageCode, false);
- if (languageIndexFromCode2 >= 0)
- {
- return LocalizationManager.Sources[j].mLanguages[languageIndexFromCode2].Name;
- }
- j++;
- }
- }
- int k = 0;
- int count3 = LocalizationManager.Sources.Count;
- while (k < count3)
- {
- int languageIndex = LocalizationManager.Sources[k].GetLanguageIndex(Language, false, true);
- if (languageIndex >= 0)
- {
- return LocalizationManager.Sources[k].mLanguages[languageIndex].Name;
- }
- k++;
- }
- int l = 0;
- int count4 = LocalizationManager.Sources.Count;
- while (l < count4)
- {
- int languageIndex2 = LocalizationManager.Sources[l].GetLanguageIndex(Language, true, true);
- if (languageIndex2 >= 0)
- {
- return LocalizationManager.Sources[l].mLanguages[languageIndex2].Name;
- }
- l++;
- }
- return string.Empty;
- }
- public static string GetLanguageCode(string Language)
- {
- if (LocalizationManager.Sources.Count == 0)
- {
- LocalizationManager.UpdateSources();
- }
- int i = 0;
- int count = LocalizationManager.Sources.Count;
- while (i < count)
- {
- int languageIndex = LocalizationManager.Sources[i].GetLanguageIndex(Language, true, true);
- if (languageIndex >= 0)
- {
- return LocalizationManager.Sources[i].mLanguages[languageIndex].Code;
- }
- i++;
- }
- return string.Empty;
- }
- public static string GetLanguageFromCode(string Code, bool exactMatch = true)
- {
- if (LocalizationManager.Sources.Count == 0)
- {
- LocalizationManager.UpdateSources();
- }
- int i = 0;
- int count = LocalizationManager.Sources.Count;
- while (i < count)
- {
- int languageIndexFromCode = LocalizationManager.Sources[i].GetLanguageIndexFromCode(Code, exactMatch);
- if (languageIndexFromCode >= 0)
- {
- return LocalizationManager.Sources[i].mLanguages[languageIndexFromCode].Name;
- }
- i++;
- }
- return string.Empty;
- }
- public static List<string> GetAllLanguages(bool SkipDisabled = true)
- {
- if (LocalizationManager.Sources.Count == 0)
- {
- LocalizationManager.UpdateSources();
- }
- List<string> Languages = new List<string>();
- int i = 0;
- int count = LocalizationManager.Sources.Count;
- while (i < count)
- {
- Languages.AddRange(from x in LocalizationManager.Sources[i].GetLanguages(SkipDisabled)
- where !Languages.Contains(x)
- select x);
- i++;
- }
- return Languages;
- }
- public static List<string> GetAllLanguagesCode(bool allowRegions = true, bool SkipDisabled = true)
- {
- List<string> Languages = new List<string>();
- int i = 0;
- int count = LocalizationManager.Sources.Count;
- while (i < count)
- {
- Languages.AddRange(from x in LocalizationManager.Sources[i].GetLanguagesCode(allowRegions, SkipDisabled)
- where !Languages.Contains(x)
- select x);
- i++;
- }
- return Languages;
- }
- public static bool IsLanguageEnabled(string Language)
- {
- int i = 0;
- int count = LocalizationManager.Sources.Count;
- while (i < count)
- {
- if (!LocalizationManager.Sources[i].IsLanguageEnabled(Language))
- {
- return false;
- }
- i++;
- }
- return true;
- }
- private static void LoadCurrentLanguage()
- {
- for (int i = 0; i < LocalizationManager.Sources.Count; i++)
- {
- int languageIndex = LocalizationManager.Sources[i].GetLanguageIndex(LocalizationManager.mCurrentLanguage, true, false);
- LocalizationManager.Sources[i].LoadLanguage(languageIndex, true, true, true, false);
- }
- }
- public static void ApplyLocalizationParams(ref string translation)
- {
- LocalizationManager.ApplyLocalizationParams(ref translation, (string p) => LocalizationManager.GetLocalizationParam(p, null));
- }
- public static void ApplyLocalizationParams(ref string translation, GameObject root)
- {
- LocalizationManager.ApplyLocalizationParams(ref translation, (string p) => LocalizationManager.GetLocalizationParam(p, root));
- }
- public static void ApplyLocalizationParams(ref string translation, Dictionary<string, object> parameters)
- {
- LocalizationManager.ApplyLocalizationParams(ref translation, delegate(string p)
- {
- object result = null;
- if (parameters.TryGetValue(p, out result))
- {
- return result;
- }
- return null;
- });
- }
- public static void ApplyLocalizationParams(ref string translation, LocalizationManager._GetParam getParam)
- {
- if (translation == null)
- {
- return;
- }
- string text = null;
- int num = translation.Length;
- int num2 = 0;
- while (num2 >= 0 && num2 < translation.Length)
- {
- int num3 = translation.IndexOf("{[", num2);
- if (num3 < 0)
- {
- break;
- }
- int num4 = translation.IndexOf("]}", num3);
- if (num4 < 0)
- {
- break;
- }
- int num5 = translation.IndexOf("{[", num3 + 1);
- if (num5 > 0 && num5 < num4)
- {
- num2 = num5;
- }
- else
- {
- int num6 = (translation[num3 + 2] != '#') ? 2 : 3;
- string param = translation.Substring(num3 + num6, num4 - num3 - num6);
- string text2 = (string)getParam(param);
- if (text2 != null)
- {
- string text3;
- if (LocalizationManager.TryGetTranslation(text2, out text3, true, 0, true, false, null, null))
- {
- text2 = text3;
- }
- string oldValue = translation.Substring(num3, num4 - num3 + 2);
- translation = translation.Replace(oldValue, text2);
- int n = 0;
- if (int.TryParse(text2, out n))
- {
- text = GoogleLanguages.GetPluralType(LocalizationManager.CurrentLanguageCode, n).ToString();
- }
- num2 = num3 + text2.Length;
- }
- else
- {
- num2 = num4 + 2;
- }
- }
- }
- if (text != null)
- {
- string text4 = "[i2p_" + text + "]";
- int num7 = translation.IndexOf(text4, StringComparison.OrdinalIgnoreCase);
- if (num7 < 0)
- {
- num7 = 0;
- }
- else
- {
- num7 += text4.Length;
- }
- num = translation.IndexOf("[i2p_", num7 + 1, StringComparison.OrdinalIgnoreCase);
- if (num < 0)
- {
- num = translation.Length;
- }
- translation = translation.Substring(num7, num - num7);
- }
- }
- internal static string GetLocalizationParam(string ParamName, GameObject root)
- {
- if (root)
- {
- MonoBehaviour[] components = root.GetComponents<MonoBehaviour>();
- int i = 0;
- int num = components.Length;
- while (i < num)
- {
- ILocalizationParamsManager localizationParamsManager = components[i] as ILocalizationParamsManager;
- if (localizationParamsManager != null && components[i].enabled)
- {
- string parameterValue = localizationParamsManager.GetParameterValue(ParamName);
- if (parameterValue != null)
- {
- return parameterValue;
- }
- }
- i++;
- }
- }
- int j = 0;
- int count = LocalizationManager.ParamManagers.Count;
- while (j < count)
- {
- string parameterValue = LocalizationManager.ParamManagers[j].GetParameterValue(ParamName);
- if (parameterValue != null)
- {
- return parameterValue;
- }
- j++;
- }
- return null;
- }
- private static string GetPluralType(MatchCollection matches, string langCode, LocalizationManager._GetParam getParam)
- {
- int i = 0;
- int count = matches.Count;
- while (i < count)
- {
- Match match = matches[i];
- string value = match.Groups[match.Groups.Count - 1].Value;
- string text = (string)getParam(value);
- if (text != null)
- {
- int n = 0;
- if (int.TryParse(text, out n))
- {
- return GoogleLanguages.GetPluralType(langCode, n).ToString();
- }
- }
- i++;
- }
- return null;
- }
- public static string ApplyRTLfix(string line)
- {
- return LocalizationManager.ApplyRTLfix(line, 0, true);
- }
- public static string ApplyRTLfix(string line, int maxCharacters, bool ignoreNumbers)
- {
- if (string.IsNullOrEmpty(line))
- {
- return line;
- }
- char c = line[0];
- if (c == '!' || c == '.' || c == '?')
- {
- line = line.Substring(1) + c;
- }
- int num = -1;
- int num2 = 0;
- int num3 = 40000;
- num2 = 0;
- List<string> list = new List<string>();
- while (I2Utils.FindNextTag(line, num2, out num, out num2))
- {
- string str = "@@" + (char)(num3 + list.Count) + "@@";
- list.Add(line.Substring(num, num2 - num + 1));
- line = line.Substring(0, num) + str + line.Substring(num2 + 1);
- num2 = num + 5;
- }
- line = line.Replace("\r\n", "\n");
- line = I2Utils.SplitLine(line, maxCharacters);
- line = RTLFixer.Fix(line, true, !ignoreNumbers);
- for (int i = 0; i < list.Count; i++)
- {
- int length = line.Length;
- for (int j = 0; j < length; j++)
- {
- if (line[j] == '@' && line[j + 1] == '@' && (int)line[j + 2] >= num3 && line[j + 3] == '@' && line[j + 4] == '@')
- {
- int num4 = (int)line[j + 2] - num3;
- if (num4 % 2 == 0)
- {
- num4++;
- }
- else
- {
- num4--;
- }
- if (num4 >= list.Count)
- {
- num4 = list.Count - 1;
- }
- line = line.Substring(0, j) + list[num4] + line.Substring(j + 5);
- break;
- }
- }
- }
- return line;
- }
- public static string FixRTL_IfNeeded(string text, int maxCharacters = 0, bool ignoreNumber = false)
- {
- if (LocalizationManager.IsRight2Left)
- {
- return LocalizationManager.ApplyRTLfix(text, maxCharacters, ignoreNumber);
- }
- return text;
- }
- public static bool IsRTL(string Code)
- {
- return Array.IndexOf<string>(LocalizationManager.LanguagesRTL, Code) >= 0;
- }
- public static bool UpdateSources()
- {
- LocalizationManager.UnregisterDeletededSources();
- LocalizationManager.RegisterSourceInResources();
- LocalizationManager.RegisterSceneSources();
- return LocalizationManager.Sources.Count > 0;
- }
- private static void UnregisterDeletededSources()
- {
- for (int i = LocalizationManager.Sources.Count - 1; i >= 0; i--)
- {
- if (LocalizationManager.Sources[i] == null)
- {
- LocalizationManager.RemoveSource(LocalizationManager.Sources[i]);
- }
- }
- }
- private static void RegisterSceneSources()
- {
- LanguageSource[] array = (LanguageSource[])Resources.FindObjectsOfTypeAll(typeof(LanguageSource));
- int i = 0;
- int num = array.Length;
- while (i < num)
- {
- if (!LocalizationManager.Sources.Contains(array[i]))
- {
- LocalizationManager.AddSource(array[i]);
- }
- i++;
- }
- }
- private static void RegisterSourceInResources()
- {
- foreach (string name in LocalizationManager.GlobalSources)
- {
- GameObject asset = ResourceManager.pInstance.GetAsset<GameObject>(name);
- LanguageSource languageSource = (!asset) ? null : asset.GetComponent<LanguageSource>();
- if (languageSource && !LocalizationManager.Sources.Contains(languageSource))
- {
- languageSource.mIsGlobalSource = true;
- LocalizationManager.AddSource(languageSource);
- }
- }
- }
- internal static void AddSource(LanguageSource Source)
- {
- if (LocalizationManager.Sources.Contains(Source))
- {
- return;
- }
- LocalizationManager.Sources.Add(Source);
- if (Source.HasGoogleSpreadsheet() && Source.GoogleUpdateFrequency != LanguageSource.eGoogleUpdateFrequency.Never)
- {
- Source.Import_Google_FromCache();
- bool justCheck = false;
- if (Source.GoogleUpdateDelay > 0f)
- {
- CoroutineManager.Start(LocalizationManager.Delayed_Import_Google(Source, Source.GoogleUpdateDelay, justCheck));
- }
- else
- {
- Source.Import_Google(false, justCheck);
- }
- }
- for (int i = 0; i < Source.mLanguages.Count<LanguageData>(); i++)
- {
- Source.mLanguages[i].SetLoaded(true);
- }
- if (Source.mDictionary.Count == 0)
- {
- Source.UpdateDictionary(true);
- }
- }
- private static IEnumerator Delayed_Import_Google(LanguageSource source, float delay, bool justCheck)
- {
- yield return new WaitForSeconds(delay);
- source.Import_Google(false, justCheck);
- yield break;
- }
- internal static void RemoveSource(LanguageSource Source)
- {
- LocalizationManager.Sources.Remove(Source);
- }
- public static bool IsGlobalSource(string SourceName)
- {
- return Array.IndexOf<string>(LocalizationManager.GlobalSources, SourceName) >= 0;
- }
- public static LanguageSource GetSourceContaining(string term, bool fallbackToFirst = true)
- {
- if (!string.IsNullOrEmpty(term))
- {
- int i = 0;
- int count = LocalizationManager.Sources.Count;
- while (i < count)
- {
- if (LocalizationManager.Sources[i].GetTermData(term, false) != null)
- {
- return LocalizationManager.Sources[i];
- }
- i++;
- }
- }
- return (!fallbackToFirst || LocalizationManager.Sources.Count <= 0) ? null : LocalizationManager.Sources[0];
- }
- public static UnityEngine.Object FindAsset(string value)
- {
- int i = 0;
- int count = LocalizationManager.Sources.Count;
- while (i < count)
- {
- UnityEngine.Object @object = LocalizationManager.Sources[i].FindAsset(value);
- if (@object)
- {
- return @object;
- }
- i++;
- }
- return null;
- }
- public static string GetCurrentDeviceLanguage()
- {
- if (string.IsNullOrEmpty(LocalizationManager.mCurrentDeviceLanguage))
- {
- LocalizationManager.DetectDeviceLanguage();
- }
- return LocalizationManager.mCurrentDeviceLanguage;
- }
- private static void DetectDeviceLanguage()
- {
- LocalizationManager.mCurrentDeviceLanguage = Application.systemLanguage.ToString();
- if (LocalizationManager.mCurrentDeviceLanguage == "ChineseSimplified")
- {
- LocalizationManager.mCurrentDeviceLanguage = "Chinese (Simplified)";
- }
- if (LocalizationManager.mCurrentDeviceLanguage == "ChineseTraditional")
- {
- LocalizationManager.mCurrentDeviceLanguage = "Chinese (Traditional)";
- }
- }
- public static void RegisterTarget(ILocalizeTargetDescriptor desc)
- {
- if (LocalizationManager.mLocalizeTargets.FindIndex((ILocalizeTargetDescriptor x) => x.Name == desc.Name) != -1)
- {
- return;
- }
- for (int i = 0; i < LocalizationManager.mLocalizeTargets.Count; i++)
- {
- if (LocalizationManager.mLocalizeTargets[i].Priority > desc.Priority)
- {
- LocalizationManager.mLocalizeTargets.Insert(i, desc);
- return;
- }
- }
- LocalizationManager.mLocalizeTargets.Add(desc);
- }
- public static event LocalizationManager.OnLocalizeCallback OnLocalizeEvent;
- public static string GetTranslation(string Term, bool FixForRTL = true, int maxLineLengthForRTL = 0, bool ignoreRTLnumbers = true, bool applyParameters = false, GameObject localParametersRoot = null, string overrideLanguage = null)
- {
- string result = null;
- LocalizationManager.TryGetTranslation(Term, out result, FixForRTL, maxLineLengthForRTL, ignoreRTLnumbers, applyParameters, localParametersRoot, overrideLanguage);
- return result;
- }
- public static string GetTermTranslation(string Term, bool FixForRTL = true, int maxLineLengthForRTL = 0, bool ignoreRTLnumbers = true, bool applyParameters = false, GameObject localParametersRoot = null, string overrideLanguage = null)
- {
- return LocalizationManager.GetTranslation(Term, FixForRTL, maxLineLengthForRTL, ignoreRTLnumbers, applyParameters, localParametersRoot, overrideLanguage);
- }
- public static bool TryGetTranslation(string Term, out string Translation, bool FixForRTL = true, int maxLineLengthForRTL = 0, bool ignoreRTLnumbers = true, bool applyParameters = false, GameObject localParametersRoot = null, string overrideLanguage = null)
- {
- Translation = null;
- if (string.IsNullOrEmpty(Term))
- {
- return false;
- }
- LocalizationManager.InitializeIfNeeded();
- int i = 0;
- int count = LocalizationManager.Sources.Count;
- while (i < count)
- {
- if (LocalizationManager.Sources[i].TryGetTranslation(Term, out Translation, overrideLanguage, null, false, false))
- {
- if (applyParameters)
- {
- LocalizationManager.ApplyLocalizationParams(ref Translation, localParametersRoot);
- }
- if (LocalizationManager.IsRight2Left && FixForRTL)
- {
- Translation = LocalizationManager.ApplyRTLfix(Translation, maxLineLengthForRTL, ignoreRTLnumbers);
- }
- return true;
- }
- i++;
- }
- return false;
- }
- public static string GetAppName(string languageCode)
- {
- if (!string.IsNullOrEmpty(languageCode))
- {
- for (int i = 0; i < LocalizationManager.Sources.Count; i++)
- {
- if (!string.IsNullOrEmpty(LocalizationManager.Sources[i].mTerm_AppName))
- {
- int languageIndexFromCode = LocalizationManager.Sources[i].GetLanguageIndexFromCode(languageCode, false);
- if (languageIndexFromCode >= 0)
- {
- TermData termData = LocalizationManager.Sources[i].GetTermData(LocalizationManager.Sources[i].mTerm_AppName, false);
- if (termData != null)
- {
- string translation = termData.GetTranslation(languageIndexFromCode, null);
- if (!string.IsNullOrEmpty(translation))
- {
- return translation;
- }
- }
- }
- }
- }
- }
- return Application.productName;
- }
- public static void LocalizeAll(bool Force = false)
- {
- LocalizationManager.LoadCurrentLanguage();
- if (!Application.isPlaying)
- {
- LocalizationManager.DoLocalizeAll(Force);
- return;
- }
- LocalizationManager.mLocalizeIsScheduledWithForcedValue = (LocalizationManager.mLocalizeIsScheduledWithForcedValue || Force);
- if (LocalizationManager.mLocalizeIsScheduled)
- {
- return;
- }
- CoroutineManager.Start(LocalizationManager.Coroutine_LocalizeAll());
- }
- private static IEnumerator Coroutine_LocalizeAll()
- {
- LocalizationManager.mLocalizeIsScheduled = true;
- yield return null;
- LocalizationManager.mLocalizeIsScheduled = false;
- bool force = LocalizationManager.mLocalizeIsScheduledWithForcedValue;
- LocalizationManager.mLocalizeIsScheduledWithForcedValue = false;
- LocalizationManager.DoLocalizeAll(force);
- yield break;
- }
- private static void DoLocalizeAll(bool Force = false)
- {
- Localize[] array = (Localize[])Resources.FindObjectsOfTypeAll(typeof(Localize));
- int i = 0;
- int num = array.Length;
- while (i < num)
- {
- Localize localize = array[i];
- localize.OnLocalize(Force);
- i++;
- }
- if (LocalizationManager.OnLocalizeEvent != null)
- {
- LocalizationManager.OnLocalizeEvent();
- }
- ResourceManager.pInstance.CleanResourceCache();
- }
- public static List<string> GetCategories()
- {
- List<string> list = new List<string>();
- int i = 0;
- int count = LocalizationManager.Sources.Count;
- while (i < count)
- {
- LocalizationManager.Sources[i].GetCategories(false, list);
- i++;
- }
- return list;
- }
- public static List<string> GetTermsList(string Category = null)
- {
- if (LocalizationManager.Sources.Count == 0)
- {
- LocalizationManager.UpdateSources();
- }
- if (LocalizationManager.Sources.Count == 1)
- {
- return LocalizationManager.Sources[0].GetTermsList(Category);
- }
- HashSet<string> hashSet = new HashSet<string>();
- int i = 0;
- int count = LocalizationManager.Sources.Count;
- while (i < count)
- {
- hashSet.UnionWith(LocalizationManager.Sources[i].GetTermsList(Category));
- i++;
- }
- return new List<string>(hashSet);
- }
- public static TermData GetTermData(string term)
- {
- LocalizationManager.InitializeIfNeeded();
- int i = 0;
- int count = LocalizationManager.Sources.Count;
- while (i < count)
- {
- TermData termData = LocalizationManager.Sources[i].GetTermData(term, false);
- if (termData != null)
- {
- return termData;
- }
- i++;
- }
- return null;
- }
- private static string mCurrentLanguage;
- private static string mLanguageCode;
- private static CultureInfo mCurrentCulture;
- private static bool mChangeCultureInfo = false;
- public static bool IsRight2Left = false;
- public static bool HasJoinedWords = false;
- public static List<ILocalizationParamsManager> ParamManagers = new List<ILocalizationParamsManager>();
- private static string[] LanguagesRTL = new string[]
- {
- "ar-DZ",
- "ar",
- "ar-BH",
- "ar-EG",
- "ar-IQ",
- "ar-JO",
- "ar-KW",
- "ar-LB",
- "ar-LY",
- "ar-MA",
- "ar-OM",
- "ar-QA",
- "ar-SA",
- "ar-SY",
- "ar-TN",
- "ar-AE",
- "ar-YE",
- "he",
- "ur",
- "ji"
- };
- public static List<LanguageSource> Sources = new List<LanguageSource>();
- public static string[] GlobalSources = new string[]
- {
- "I2Languages"
- };
- private static string mCurrentDeviceLanguage;
- public static List<ILocalizeTargetDescriptor> mLocalizeTargets = new List<ILocalizeTargetDescriptor>();
- private static bool mLocalizeIsScheduled = false;
- private static bool mLocalizeIsScheduledWithForcedValue = false;
- public static bool HighlightLocalizedTargets = false;
- public delegate object _GetParam(string param);
- public delegate void OnLocalizeCallback();
- }
- }
|