using System; using System.Collections.Generic; using System.Text.RegularExpressions; using UnityEngine; using Object = UnityEngine.Object; namespace I2.Loc { public static partial class LocalizationManager { #region Variables: Misc public static List ParamManagers = new List(); #endregion #region Parameters public delegate object _GetParam(string param); public static void AutoLoadGlobalParamManagers() { foreach (var manager in Object.FindObjectsOfType()) { if (manager._IsGlobalManager && !ParamManagers.Contains(manager)) { Debug.Log(manager); ParamManagers.Add(manager); } } } public static void ApplyLocalizationParams(ref string translation, bool allowLocalizedParameters = true) { ApplyLocalizationParams(ref translation, p => GetLocalizationParam(p, null), allowLocalizedParameters); } public static void ApplyLocalizationParams(ref string translation, GameObject root, bool allowLocalizedParameters = true) { ApplyLocalizationParams(ref translation, p => GetLocalizationParam(p, root), allowLocalizedParameters); } public static void ApplyLocalizationParams(ref string translation, Dictionary parameters, bool allowLocalizedParameters = true) { ApplyLocalizationParams(ref translation, p => { object o = null; if (parameters.TryGetValue(p, out o)) return o; return null; }, allowLocalizedParameters); } public static void ApplyLocalizationParams(ref string translation, _GetParam getParam, bool allowLocalizedParameters=true) { if (translation == null) return; string pluralType=null; int idx0 = 0; int idx1 = translation.Length; int index = 0; while (index>=0 && index0 && isubParam= 0) { result = termData.GetTranslation(idx); } } } var paramTag = translation.Substring(iParamStart, iParamEnd - iParamStart + 2); translation = translation.Replace(paramTag, result); int amount = 0; if (int.TryParse(result, out amount)) { pluralType = GoogleLanguages.GetPluralType(CurrentLanguageCode, amount).ToString(); } index = iParamStart + result.Length; } else { index = iParamEnd + 2; } } if (pluralType != null) { var tag = "[i2p_" + pluralType + "]"; idx0 = translation.IndexOf(tag, StringComparison.OrdinalIgnoreCase); if (idx0 < 0) idx0 = 0; else idx0 += tag.Length; idx1 = translation.IndexOf("[i2p_", idx0 + 1, StringComparison.OrdinalIgnoreCase); if (idx1 < 0) idx1 = translation.Length; translation = translation.Substring(idx0, idx1 - idx0); } } internal static string GetLocalizationParam(string ParamName, GameObject root) { string result = null; if (root) { var components = root.GetComponents(); for (int i = 0, imax = components.Length; i < imax; ++i) { var manager = components[i] as ILocalizationParamsManager; if (manager != null && components[i].enabled) { result = manager.GetParameterValue(ParamName); if (result != null) return result; } } } for (int i = 0, imax = ParamManagers.Count; i < imax; ++i) { result = ParamManagers[i].GetParameterValue(ParamName); if (result != null) return result; } return null; } #endregion #region Plural private static string GetPluralType( MatchCollection matches, string langCode, _GetParam getParam) { for (int i = 0, nMatches = matches.Count; i < nMatches; ++i) { var match = matches[i]; var param = match.Groups[match.Groups.Count - 1].Value; var result = (string)getParam(param); if (result == null) continue; int amount = 0; if (!int.TryParse (result, out amount)) continue; var pluralType = GoogleLanguages.GetPluralType(langCode, amount); return pluralType.ToString (); } return null; } #endregion } }