Index misscalculation issue

More
6 years 2 months ago #2755 by bahattintancoskuner
Hi,

I have noticed an index misscalculation bug that prevents me to use multiple local parameters.

In LocalizationManager_Parameters.cs :
private static void ApplyLocalizationParamsInternal(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 = iParamEnd + 2; //<== HERE : when the first parameter replaced, the size of thw word changed too so it should be recalculated
            }

Change to this instead :
private static void ApplyLocalizationParamsInternal(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 = iParamEnd - (param.Length - result.Length) - 2; //TODO: This calculation is fixed, index calculation was wrong after replacing

                }
                else //TODO: wrapped with else
                {
                    index = iParamEnd + 2; 
                }

            }

Please consider this bug in your new versions.

Have a good day.
The following user(s) said Thank You: Frank

Please Log in or Create an account to join the conversation.

More
6 years 2 months ago #2758 by Frank
Replied by Frank on topic Index misscalculation issue
Hi,
Thanks for looking into that.
I verified the issue and added the fix.
I also created a Unity Test to verify all possible Parameters and tag combinations, so that this error doesn't repeat in any future update.

Again, Thanks a lot for the suggestion!
Frank

Are you :-) Give I2L 5 stars!
Are you :-( Please lets us know how to improve it!
To get the betas as soon as they are ready, check this out

Please Log in or Create an account to join the conversation.

Time to create page: 0.129 seconds
Template by JoomlaShine