LanguageSourceData_Languages.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. using System;
  2. using UnityEngine;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5. using Object = UnityEngine.Object;
  6. namespace I2.Loc
  7. {
  8. public partial class LanguageSourceData
  9. {
  10. #region Language
  11. public int GetLanguageIndex( string language, bool AllowDiscartingRegion = true, bool SkipDisabled = true)
  12. {
  13. // First look for an exact match
  14. for (int i=0, imax=mLanguages.Count; i<imax; ++i)
  15. if ((!SkipDisabled || mLanguages[i].IsEnabled()) && string.Compare(mLanguages[i].Name, language, StringComparison.OrdinalIgnoreCase)==0)
  16. return i;
  17. // Then allow matching "English (Canada)" to "english"
  18. if (AllowDiscartingRegion)
  19. {
  20. int MostSimilar = -1;
  21. int BestSimilitud = 0;
  22. for (int i=0, imax=mLanguages.Count; i<imax; ++i)
  23. if (!SkipDisabled || mLanguages[i].IsEnabled())
  24. {
  25. int commonWords = GetCommonWordInLanguageNames(mLanguages[i].Name, language);
  26. if (commonWords>BestSimilitud)
  27. {
  28. BestSimilitud = commonWords;
  29. MostSimilar = i;
  30. }
  31. //if (AreTheSameLanguage(mLanguages[i].Name, language))
  32. // return i;
  33. }
  34. if (MostSimilar>=0)
  35. return MostSimilar;
  36. }
  37. return -1;
  38. }
  39. public LanguageData GetLanguageData(string language, bool AllowDiscartingRegion = true)
  40. {
  41. int idx = GetLanguageIndex(language, AllowDiscartingRegion, false);
  42. return (idx < 0) ? null : mLanguages[idx];
  43. }
  44. // TODO: Fix IsCurrentLanguage when current=English and there are two variants in the source (English Canada, English US)
  45. public bool IsCurrentLanguage( int languageIndex )
  46. {
  47. return LocalizationManager.CurrentLanguage == mLanguages[languageIndex].Name;
  48. }
  49. public int GetLanguageIndexFromCode( string Code, bool exactMatch=true, bool ignoreDisabled = false)
  50. {
  51. for (int i = 0, imax = mLanguages.Count; i < imax; ++i)
  52. {
  53. if (ignoreDisabled && !mLanguages[i].IsEnabled())
  54. continue;
  55. if (string.Compare(mLanguages[i].Code, Code, StringComparison.OrdinalIgnoreCase) == 0)
  56. return i;
  57. }
  58. if (!exactMatch)
  59. {
  60. // Find any match without using the Regions
  61. for (int i = 0, imax = mLanguages.Count; i < imax; ++i)
  62. {
  63. if (ignoreDisabled && !mLanguages[i].IsEnabled())
  64. continue;
  65. if (string.Compare(mLanguages[i].Code, 0, Code, 0, 2, StringComparison.OrdinalIgnoreCase) == 0)
  66. return i;
  67. }
  68. }
  69. return -1;
  70. }
  71. public static int GetCommonWordInLanguageNames(string Language1, string Language2)
  72. {
  73. if (string.IsNullOrEmpty (Language1) || string.IsNullOrEmpty (Language2))
  74. return 0;
  75. var separators = "( )-/\\".ToCharArray();
  76. string[] Words1 = Language1.ToLower().Split(separators);
  77. string[] Words2 = Language2.ToLower().Split(separators);
  78. int similitud = 0;
  79. foreach (var word in Words1)
  80. if (!string.IsNullOrEmpty(word) && Words2.Contains(word))
  81. similitud++;
  82. foreach (var word in Words2)
  83. if (!string.IsNullOrEmpty(word) && Words1.Contains(word))
  84. similitud++;
  85. return similitud;
  86. }
  87. public static bool AreTheSameLanguage(string Language1, string Language2)
  88. {
  89. Language1 = GetLanguageWithoutRegion(Language1);
  90. Language2 = GetLanguageWithoutRegion(Language2);
  91. return (string.Compare(Language1, Language2, StringComparison.OrdinalIgnoreCase)==0);
  92. }
  93. public static string GetLanguageWithoutRegion(string Language)
  94. {
  95. int Index = Language.IndexOfAny("(/\\[,{".ToCharArray());
  96. if (Index<0)
  97. return Language;
  98. else
  99. return Language.Substring(0, Index).Trim();
  100. }
  101. public void AddLanguage(string LanguageName)
  102. {
  103. AddLanguage(LanguageName, GoogleLanguages.GetLanguageCode(LanguageName));
  104. }
  105. public void AddLanguage( string LanguageName, string LanguageCode )
  106. {
  107. if (GetLanguageIndex(LanguageName, false)>=0)
  108. return;
  109. LanguageData Lang = new LanguageData();
  110. Lang.Name = LanguageName;
  111. Lang.Code = LanguageCode;
  112. mLanguages.Add (Lang);
  113. int NewSize = mLanguages.Count;
  114. for (int i=0, imax=mTerms.Count; i<imax; ++i)
  115. {
  116. Array.Resize(ref mTerms[i].Languages, NewSize);
  117. Array.Resize(ref mTerms[i].Flags, NewSize);
  118. }
  119. Editor_SetDirty();
  120. }
  121. public void RemoveLanguage( string LanguageName )
  122. {
  123. int LangIndex = GetLanguageIndex(LanguageName, false, false);
  124. if (LangIndex<0)
  125. return;
  126. int nLanguages = mLanguages.Count;
  127. for (int i=0, imax=mTerms.Count; i<imax; ++i)
  128. {
  129. for (int j=LangIndex+1; j<nLanguages; ++j)
  130. {
  131. mTerms[i].Languages[j-1] = mTerms[i].Languages[j];
  132. mTerms[i].Flags[j-1] = mTerms[i].Flags[j];
  133. }
  134. Array.Resize(ref mTerms[i].Languages, nLanguages-1);
  135. Array.Resize(ref mTerms[i].Flags, nLanguages-1);
  136. }
  137. mLanguages.RemoveAt(LangIndex);
  138. Editor_SetDirty();
  139. }
  140. public List<string> GetLanguages( bool skipDisabled = true)
  141. {
  142. List<string> Languages = new List<string>();
  143. for (int j = 0, jmax = mLanguages.Count; j < jmax; ++j)
  144. {
  145. if (!skipDisabled || mLanguages[j].IsEnabled())
  146. Languages.Add(mLanguages[j].Name);
  147. }
  148. return Languages;
  149. }
  150. public List<string> GetLanguagesCode(bool allowRegions = true, bool skipDisabled = true)
  151. {
  152. List<string> Languages = new List<string>();
  153. for (int j = 0, jmax = mLanguages.Count; j < jmax; ++j)
  154. {
  155. if (skipDisabled && !mLanguages[j].IsEnabled())
  156. continue;
  157. var code = mLanguages[j].Code;
  158. if (!allowRegions && code != null && code.Length > 2)
  159. code = code.Substring(0, 2);
  160. if (!string.IsNullOrEmpty(code) && !Languages.Contains(code))
  161. Languages.Add(code);
  162. }
  163. return Languages;
  164. }
  165. public bool IsLanguageEnabled(string Language)
  166. {
  167. int idx = GetLanguageIndex(Language, false);
  168. return idx >= 0 && mLanguages[idx].IsEnabled();
  169. }
  170. public void EnableLanguage(string Language, bool bEnabled)
  171. {
  172. int idx = GetLanguageIndex(Language, false, false);
  173. if (idx >= 0)
  174. mLanguages[idx].SetEnabled(bEnabled);
  175. }
  176. #endregion
  177. #region Save/Load Language
  178. public bool AllowUnloadingLanguages()
  179. {
  180. #if UNITY_EDITOR
  181. return _AllowUnloadingLanguages==eAllowUnloadLanguages.EditorAndDevice;
  182. #else
  183. return _AllowUnloadingLanguages!=eAllowUnloadLanguages.Never;
  184. #endif
  185. }
  186. string GetSavedLanguageFileName(int languageIndex)
  187. {
  188. if (languageIndex < 0)
  189. return null;
  190. return "LangSource_" + GetSourcePlayerPrefName() + "_" + mLanguages[languageIndex].Name + ".loc";
  191. }
  192. public void LoadLanguage( int languageIndex, bool UnloadOtherLanguages, bool useFallback, bool onlyCurrentSpecialization, bool forceLoad )
  193. {
  194. if (!AllowUnloadingLanguages())
  195. return;
  196. // Some consoles don't allow IO access
  197. if (!PersistentStorage.CanAccessFiles())
  198. return;
  199. if (languageIndex >= 0 && (forceLoad || !mLanguages[languageIndex].IsLoaded()))
  200. {
  201. var tempPath = GetSavedLanguageFileName(languageIndex);
  202. var langData = PersistentStorage.LoadFile(PersistentStorage.eFileType.Temporal, tempPath, false);
  203. if (!string.IsNullOrEmpty(langData))
  204. {
  205. Import_Language_from_Cache(languageIndex, langData, useFallback, onlyCurrentSpecialization);
  206. mLanguages[languageIndex].SetLoaded(true);
  207. }
  208. }
  209. if (UnloadOtherLanguages && I2Utils.IsPlaying())
  210. {
  211. for (int lan = 0; lan < mLanguages.Count; ++lan)
  212. {
  213. if (lan != languageIndex)
  214. UnloadLanguage(lan);
  215. }
  216. }
  217. }
  218. // if forceLoad, then the language is loaded from the cache even if its already loaded
  219. // this is needed to cleanup fallbacks
  220. public void LoadAllLanguages(bool forceLoad=false)
  221. {
  222. for (int i = 0; i < mLanguages.Count; ++i)
  223. {
  224. LoadLanguage(i, false, false, false, forceLoad);
  225. }
  226. }
  227. public void UnloadLanguage(int languageIndex)
  228. {
  229. if (!AllowUnloadingLanguages())
  230. return;
  231. // Some consoles don't allow IO access
  232. if (!PersistentStorage.CanAccessFiles())
  233. return;
  234. if (!I2Utils.IsPlaying() ||
  235. !mLanguages[languageIndex].IsLoaded() ||
  236. !mLanguages[languageIndex].CanBeUnloaded() ||
  237. IsCurrentLanguage(languageIndex) ||
  238. !PersistentStorage.HasFile(PersistentStorage.eFileType.Temporal, GetSavedLanguageFileName(languageIndex)))
  239. {
  240. return;
  241. }
  242. foreach (var termData in mTerms)
  243. {
  244. termData.Languages[languageIndex] = null;
  245. }
  246. mLanguages[languageIndex].SetLoaded(false);
  247. }
  248. public void SaveLanguages( bool unloadAll, PersistentStorage.eFileType fileLocation = PersistentStorage.eFileType.Temporal)
  249. {
  250. if (!AllowUnloadingLanguages())
  251. return;
  252. // Some consoles don't allow IO access
  253. if (!PersistentStorage.CanAccessFiles())
  254. return;
  255. for (int i = 0; i < mLanguages.Count; ++i)
  256. {
  257. var data = Export_Language_to_Cache(i, IsCurrentLanguage(i));
  258. if (string.IsNullOrEmpty(data))
  259. continue;
  260. PersistentStorage.SaveFile(PersistentStorage.eFileType.Temporal, GetSavedLanguageFileName(i), data);
  261. }
  262. if (unloadAll)
  263. {
  264. for (int i = 0; i < mLanguages.Count; ++i)
  265. {
  266. if (unloadAll && !IsCurrentLanguage(i))
  267. UnloadLanguage(i);
  268. }
  269. }
  270. }
  271. public bool HasUnloadedLanguages()
  272. {
  273. for (int i = 0; i < mLanguages.Count; ++i)
  274. {
  275. if (!mLanguages[i].IsLoaded())
  276. return true;
  277. }
  278. return false;
  279. }
  280. #endregion
  281. }
  282. }