private static void ApplyLocalizationParamsInternal(ref string translation, _GetParam getParam) { if (translation == null) { return; } var regex = new Regex(@"{\[(.*?)\]}"); var regexMatches = regex.Matches(translation); var pluralType = GetPluralType(regexMatches, CurrentLanguageCode, getParam); int idx0 = 0; int idx1 = translation.Length; if (pluralType != null) { var tag = "[i2p_" + pluralType + "]"; idx0 = translation.IndexOf(tag, System.StringComparison.OrdinalIgnoreCase); if (idx0 < 0) { idx0 = 0; } else { idx0 += tag.Length; } idx1 = translation.IndexOf("[i2p_", idx0 + 1, System.StringComparison.OrdinalIgnoreCase); if (idx1 < 0) { idx1 = translation.Length; } translation = translation.Substring(idx0, idx1 - idx0); } for (int i = 0, nMatches = regexMatches.Count; i < nMatches; ++i) { var match = regexMatches[i]; var param = match.Groups[match.Groups.Count - 1].Value; var result = (string)getParam(param); if (result != null) { translation = translation.Replace(match.Value, result); } } }
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); }
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 && index < translation.Length) { int iParamStart = translation.IndexOf("{[", index); if (iParamStart < 0) { break; } int iParamEnd = translation.IndexOf("]}", iParamStart); if (iParamEnd < 0) { break; } // there is a sub param, so, skip this one: "this {[helo{[hi]} end" int isubParam = translation.IndexOf("{[", iParamStart + 1); if (isubParam > 0 && isubParam < iParamEnd) { index = isubParam; continue; } // Check that some plural parameters can have the form: {[#name]} var offset = translation[iParamStart + 2] == '#' ? 3 : 2; var param = translation.Substring(iParamStart + offset, iParamEnd - iParamStart - offset); var result = (string)getParam(param); if (result != null && allowLocalizedParameters) { // check if Param is Localized LanguageSourceData source; var termData = LocalizationManager.GetTermData(result, out source); if (termData != null) { int idx = source.GetLanguageIndex(LocalizationManager.CurrentLanguage); if (idx >= 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, System.StringComparison.OrdinalIgnoreCase); if (idx0 < 0) { idx0 = 0; } else { idx0 += tag.Length; } idx1 = translation.IndexOf("[i2p_", idx0 + 1, System.StringComparison.OrdinalIgnoreCase); if (idx1 < 0) { idx1 = translation.Length; } translation = translation.Substring(idx0, idx1 - idx0); } }
public static void ApplyLocalizationParams(ref string translation, _GetParam getParam) { if (translation == null) { return; } string pluralType = null; int idx0 = 0; int idx1 = translation.Length; int index = 0; while (index >= 0 && index < translation.Length) { int iParamStart = translation.IndexOf("{[", index); if (iParamStart < 0) { break; } int iParamEnd = translation.IndexOf("]}", iParamStart); if (iParamEnd < 0) { break; } // there is a sub param, so, skip this one: "this {[helo{[hi]} end" int isubParam = translation.IndexOf("{[", iParamStart + 1); if (isubParam > 0 && isubParam < iParamEnd) { index = isubParam; continue; } var param = translation.Substring(iParamStart + 2, iParamEnd - iParamStart - 2); var result = (string)getParam(param); if (result != null) { 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, System.StringComparison.OrdinalIgnoreCase); if (idx0 < 0) { idx0 = 0; } else { idx0 += tag.Length; } idx1 = translation.IndexOf("[i2p_", idx0 + 1, System.StringComparison.OrdinalIgnoreCase); if (idx1 < 0) { idx1 = translation.Length; } translation = translation.Substring(idx0, idx1 - idx0); } }