LanguageSourceData_Languages.cs 10 KB

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