LocalizationManager_Translation.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using UnityEditor;
  5. using UnityEngine;
  6. namespace I2.Loc
  7. {
  8. public static partial class LocalizationManager
  9. {
  10. #region Variables: Misc
  11. public delegate void OnLocalizeCallback();
  12. public static event OnLocalizeCallback OnLocalizeEvent;
  13. static bool mLocalizeIsScheduled;
  14. static bool mLocalizeIsScheduledWithForcedValue;
  15. public static bool HighlightLocalizedTargets = false;
  16. #endregion
  17. public static string GetTranslation(string Term, bool FixForRTL = true, int maxLineLengthForRTL = 0, bool ignoreRTLnumbers = true, bool applyParameters = false, GameObject localParametersRoot = null, string overrideLanguage = null, bool allowLocalizedParameters=true)
  18. {
  19. string Translation = null;
  20. TryGetTranslation(Term, out Translation, FixForRTL, maxLineLengthForRTL, ignoreRTLnumbers, applyParameters, localParametersRoot, overrideLanguage, allowLocalizedParameters);
  21. return Translation;
  22. }
  23. public static string GetTermTranslation(string Term, bool FixForRTL = true, int maxLineLengthForRTL = 0, bool ignoreRTLnumbers = true, bool applyParameters = false, GameObject localParametersRoot = null, string overrideLanguage = null, bool allowLocalizedParameters=true)
  24. {
  25. return GetTranslation(Term, FixForRTL, maxLineLengthForRTL, ignoreRTLnumbers, applyParameters, localParametersRoot, overrideLanguage, allowLocalizedParameters);
  26. }
  27. 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, bool allowLocalizedParameters=true)
  28. {
  29. Translation = null;
  30. if (string.IsNullOrEmpty(Term))
  31. return false;
  32. InitializeIfNeeded();
  33. for (int i = 0, imax = Sources.Count; i < imax; ++i)
  34. {
  35. if (Sources[i].TryGetTranslation(Term, out Translation, overrideLanguage))
  36. {
  37. if (applyParameters)
  38. ApplyLocalizationParams(ref Translation, localParametersRoot, allowLocalizedParameters);
  39. if (IsRight2Left && FixForRTL)
  40. Translation = ApplyRTLfix(Translation, maxLineLengthForRTL, ignoreRTLnumbers);
  41. return true;
  42. }
  43. }
  44. return false;
  45. }
  46. public static T GetTranslatedObject<T>( string AssetName, Localize optionalLocComp=null) where T : Object
  47. {
  48. if (optionalLocComp != null)
  49. {
  50. return optionalLocComp.FindTranslatedObject<T>(AssetName);
  51. }
  52. T obj = FindAsset(AssetName) as T;
  53. if (obj)
  54. return obj;
  55. obj = ResourceManager.pInstance.GetAsset<T>(AssetName);
  56. return obj;
  57. }
  58. public static T GetTranslatedObjectByTermName<T>( string Term, Localize optionalLocComp=null) where T : Object
  59. {
  60. string translation = GetTranslation(Term, FixForRTL: false);
  61. return GetTranslatedObject<T>(translation);
  62. }
  63. public static string GetAppName(string languageCode)
  64. {
  65. if (!string.IsNullOrEmpty(languageCode))
  66. {
  67. for (int i = 0; i < Sources.Count; ++i)
  68. {
  69. if (string.IsNullOrEmpty(Sources[i].mTerm_AppName))
  70. continue;
  71. int langIdx = Sources[i].GetLanguageIndexFromCode(languageCode, false);
  72. if (langIdx < 0)
  73. continue;
  74. var termData = Sources[i].GetTermData(Sources[i].mTerm_AppName);
  75. if (termData == null)
  76. continue;
  77. var appName = termData.GetTranslation(langIdx);
  78. if (!string.IsNullOrEmpty(appName))
  79. return appName;
  80. }
  81. }
  82. return Application.productName;
  83. }
  84. public static void LocalizeAll(bool Force = false)
  85. {
  86. LoadCurrentLanguage();
  87. if (!Application.isPlaying)
  88. {
  89. DoLocalizeAll(Force);
  90. return;
  91. }
  92. mLocalizeIsScheduledWithForcedValue |= Force;
  93. if (mLocalizeIsScheduled)
  94. {
  95. return;
  96. }
  97. CoroutineManager.Start(Coroutine_LocalizeAll());
  98. }
  99. static IEnumerator Coroutine_LocalizeAll()
  100. {
  101. mLocalizeIsScheduled = true;
  102. yield return null;
  103. mLocalizeIsScheduled = false;
  104. var force = mLocalizeIsScheduledWithForcedValue;
  105. mLocalizeIsScheduledWithForcedValue = false;
  106. DoLocalizeAll(force);
  107. }
  108. static void DoLocalizeAll(bool Force = false)
  109. {
  110. Localize[] Locals = (Localize[])Resources.FindObjectsOfTypeAll( typeof(Localize) );
  111. for (int i=0, imax=Locals.Length; i<imax; ++i)
  112. {
  113. Localize local = Locals[i];
  114. //if (ObjectExistInScene (local.gameObject))
  115. local.OnLocalize(Force);
  116. }
  117. if (OnLocalizeEvent != null)
  118. OnLocalizeEvent ();
  119. //ResourceManager.pInstance.CleanResourceCache();
  120. #if UNITY_EDITOR
  121. RepaintInspectors();
  122. #endif
  123. }
  124. #if UNITY_EDITOR
  125. static void RepaintInspectors()
  126. {
  127. var assemblyEditor = Assembly.GetAssembly(typeof(Editor));
  128. var typeInspectorWindow = assemblyEditor.GetType("UnityEditor.InspectorWindow");
  129. if (typeInspectorWindow != null)
  130. {
  131. typeInspectorWindow.GetMethod("RepaintAllInspectors", BindingFlags.NonPublic | BindingFlags.Static).Invoke(null, null);
  132. }
  133. }
  134. #endif
  135. public static List<string> GetCategories ()
  136. {
  137. List<string> Categories = new List<string> ();
  138. for (int i=0, imax=Sources.Count; i<imax; ++i)
  139. Sources[i].GetCategories(false, Categories);
  140. return Categories;
  141. }
  142. public static List<string> GetTermsList ( string Category = null )
  143. {
  144. if (Sources.Count==0)
  145. UpdateSources();
  146. if (Sources.Count==1)
  147. return Sources[0].GetTermsList(Category);
  148. HashSet<string> Terms = new HashSet<string> ();
  149. for (int i=0, imax=Sources.Count; i<imax; ++i)
  150. Terms.UnionWith( Sources[i].GetTermsList(Category) );
  151. return new List<string>(Terms);
  152. }
  153. public static TermData GetTermData( string term )
  154. {
  155. InitializeIfNeeded();
  156. TermData data;
  157. for (int i=0, imax=Sources.Count; i<imax; ++i)
  158. {
  159. data = Sources[i].GetTermData(term);
  160. if (data!=null)
  161. return data;
  162. }
  163. return null;
  164. }
  165. public static TermData GetTermData(string term, out LanguageSourceData source)
  166. {
  167. InitializeIfNeeded();
  168. TermData data;
  169. for (int i = 0, imax = Sources.Count; i < imax; ++i)
  170. {
  171. data = Sources[i].GetTermData(term);
  172. if (data != null)
  173. {
  174. source = Sources[i];
  175. return data;
  176. }
  177. }
  178. source = null;
  179. return null;
  180. }
  181. }
  182. }