LocalizationManager_Parameters.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4. using UnityEngine;
  5. using Object = UnityEngine.Object;
  6. namespace I2.Loc
  7. {
  8. public static partial class LocalizationManager
  9. {
  10. #region Variables: Misc
  11. public static List<ILocalizationParamsManager> ParamManagers = new List<ILocalizationParamsManager>();
  12. // returns true if this replaces the normal ApplyLocalizationParams
  13. // returns false if after running this function the manager should also run the default ApplyLocalizationParams to replace parameters
  14. public delegate bool FnCustomApplyLocalizationParams(ref string translation, _GetParam getParam, bool allowLocalizedParameters);
  15. public static FnCustomApplyLocalizationParams CustomApplyLocalizationParams;
  16. #endregion
  17. #region Parameters
  18. public delegate object _GetParam(string param);
  19. public static void AutoLoadGlobalParamManagers()
  20. {
  21. foreach (var manager in Object.FindObjectsOfType<LocalizationParamsManager>())
  22. {
  23. if (manager._IsGlobalManager && !ParamManagers.Contains(manager))
  24. {
  25. Debug.Log(manager);
  26. ParamManagers.Add(manager);
  27. }
  28. }
  29. }
  30. public static void ApplyLocalizationParams(ref string translation, bool allowLocalizedParameters = true)
  31. {
  32. ApplyLocalizationParams(ref translation, p => GetLocalizationParam(p, null), allowLocalizedParameters);
  33. }
  34. public static void ApplyLocalizationParams(ref string translation, GameObject root, bool allowLocalizedParameters = true)
  35. {
  36. ApplyLocalizationParams(ref translation, p => GetLocalizationParam(p, root), allowLocalizedParameters);
  37. }
  38. public static void ApplyLocalizationParams(ref string translation, Dictionary<string, object> parameters, bool allowLocalizedParameters = true)
  39. {
  40. ApplyLocalizationParams(ref translation, p => {
  41. object o = null;
  42. if (parameters.TryGetValue(p, out o))
  43. return o;
  44. return null;
  45. }, allowLocalizedParameters);
  46. }
  47. public static void ApplyLocalizationParams(ref string translation, _GetParam getParam, bool allowLocalizedParameters=true)
  48. {
  49. if (translation == null)
  50. return;
  51. bool skip_processing = CustomApplyLocalizationParams!=null && CustomApplyLocalizationParams.Invoke(ref translation, getParam, allowLocalizedParameters);
  52. if (skip_processing) return;
  53. string pluralType=null;
  54. int idx0 = 0;
  55. int idx1 = translation.Length;
  56. int index = 0;
  57. while (index>=0 && index<translation.Length)
  58. {
  59. int iParamStart = translation.IndexOf("{[", index, StringComparison.Ordinal);
  60. if (iParamStart < 0) break;
  61. int iParamEnd = translation.IndexOf("]}", iParamStart, StringComparison.Ordinal);
  62. if (iParamEnd < 0) break;
  63. // there is a sub param, so, skip this one: "this {[helo{[hi]} end"
  64. int isubParam = translation.IndexOf("{[", iParamStart+1, StringComparison.Ordinal);
  65. if (isubParam>0 && isubParam<iParamEnd)
  66. {
  67. index = isubParam;
  68. continue;
  69. }
  70. // Check that some plural parameters can have the form: {[#name]}
  71. var offset = translation[iParamStart + 2] == '#' ? 3 : 2;
  72. var param = translation.Substring(iParamStart + offset, iParamEnd - iParamStart - offset);
  73. var result = (string)getParam(param);
  74. if (result != null)
  75. {
  76. if (allowLocalizedParameters)
  77. {
  78. // check if Param is Localized
  79. LanguageSourceData source;
  80. var termData = GetTermData(result, out source);
  81. if (termData != null)
  82. {
  83. int idx = source.GetLanguageIndex(CurrentLanguage);
  84. if (idx >= 0)
  85. {
  86. result = termData.GetTranslation(idx);
  87. }
  88. }
  89. }
  90. var paramTag = translation.Substring(iParamStart, iParamEnd - iParamStart + 2);
  91. translation = translation.Replace(paramTag, result);
  92. int amount = 0;
  93. if (int.TryParse(result, out amount))
  94. {
  95. pluralType = GoogleLanguages.GetPluralType(CurrentLanguageCode, amount).ToString();
  96. }
  97. index = iParamStart + result.Length;
  98. }
  99. else
  100. {
  101. index = iParamEnd + 2;
  102. }
  103. }
  104. if (pluralType != null)
  105. {
  106. var tag = "[i2p_" + pluralType + "]";
  107. idx0 = translation.IndexOf(tag, StringComparison.OrdinalIgnoreCase);
  108. if (idx0 < 0) idx0 = 0;
  109. else idx0 += tag.Length;
  110. idx1 = translation.IndexOf("[i2p_", idx0 + 1, StringComparison.OrdinalIgnoreCase);
  111. if (idx1 < 0) idx1 = translation.Length;
  112. translation = translation.Substring(idx0, idx1 - idx0);
  113. }
  114. }
  115. internal static string GetLocalizationParam(string ParamName, GameObject root)
  116. {
  117. string result = null;
  118. if (root)
  119. {
  120. var components = root.GetComponents<MonoBehaviour>();
  121. for (int i = 0, imax = components.Length; i < imax; ++i)
  122. {
  123. var manager = components[i] as ILocalizationParamsManager;
  124. if (manager != null && components[i].enabled)
  125. {
  126. result = manager.GetParameterValue(ParamName);
  127. if (result != null)
  128. return result;
  129. }
  130. }
  131. }
  132. for (int i = 0, imax = ParamManagers.Count; i < imax; ++i)
  133. {
  134. result = ParamManagers[i].GetParameterValue(ParamName);
  135. if (result != null)
  136. return result;
  137. }
  138. return null;
  139. }
  140. #endregion
  141. #region Plural
  142. private static string GetPluralType( MatchCollection matches, string langCode, _GetParam getParam)
  143. {
  144. for (int i = 0, nMatches = matches.Count; i < nMatches; ++i)
  145. {
  146. var match = matches[i];
  147. var param = match.Groups[match.Groups.Count - 1].Value;
  148. var result = (string)getParam(param);
  149. if (result == null)
  150. continue;
  151. int amount = 0;
  152. if (!int.TryParse (result, out amount))
  153. continue;
  154. var pluralType = GoogleLanguages.GetPluralType(langCode, amount);
  155. return pluralType.ToString ();
  156. }
  157. return null;
  158. }
  159. #endregion
  160. }
  161. }