LocalizationManager_Translation.cs 7.2 KB

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