LocalizationManager_Parameters.cs 6.4 KB

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