Welcome, Guest
Username: Password: Remember me

TOPIC: Index misscalculation issue

Index misscalculation issue 1 week 6 days ago #2755

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 administrator has disabled public write access.
The following user(s) said Thank You: Frank

Index misscalculation issue 1 week 5 days ago #2758

  • Frank
  • Frank's Avatar
  • Offline
  • Administrator
  • Posts: 962
  • Thank you received: 208
  • Karma: 63
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
The administrator has disabled public write access.
Time to create page: 0.144 seconds
Colors