Пример #1
0
        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;
                }

                // 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)
                {
                    // check if Param is Localized
                    string newResult;
                    if (LocalizationManager.TryGetTranslation(result, out newResult, applyParameters: false))
                    {
                        result = newResult;
                    }

                    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);
            }
        }