Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArabicFixer messes with angle brackets and quotes around {0} format placeholders #36

Open
idbrii opened this issue Oct 2, 2019 · 5 comments

Comments

@idbrii
Copy link
Contributor

idbrii commented Oct 2, 2019

I have text that contains {0} to allow translations to position words within the translated phrase.

ArabicFixer messes with angle brackets and quotes around my {0} which breaks the formatting. (Causing a FormatException.)

  • It converts <color=#{0}>{1} to {#>color=0}<{1}>/color<
  • It converts <color=#0033ffee>ARABIC to >/colorcolor=#0033ffee<
  • It converts '{0}' to '}{'0
  • It converts "{0}" to " {"0}
  • It converts /{0}/ to }{/0/
  • It converts /"{0}"/ to /}{0 /

It looks like the IsPunctuation checks should prevent this, but maybe those are only to prevent character replacement of punctuation and not to prevent right-to-left conversion?

My workaround is to strip out some of these brackets and quotes. Here's my tests and my FixArabic function.


public static class Localization
{
#if UNITY_EDITOR
    [UnityEditor.MenuItem("Localization/Test FixupRightToLeft")]
#endif
    static void Menu_Test_FixupRightToLeft()
    {
        var tests = new string[] {
            // "Replace\n<color=#{0}>{1}</color>"
            "استبدال \n<color=#{0}>{1}</color>", // fails because it strips the color tag, but I'll accept it

                // "\"{0}\" - Found!"
                "/\"{0}/\" - تم العثور عليه!",

                // "Complete world \"{0}\""
                "أتمم العالم /{0}/",

                // "Found Star {0} of {1}"
                "تم العثور على نجمة {0} من {1}",

                // "Failed to load world: \"{0}\"\n\nVerify the integrity of your game files before trying again."
                "فشل تحميل العالم: \"{0}\"\n\nتأكد من سلامة ملفات لعبتك قبل تكرار المحاولة.",

                // "Please restart the game to apply settings:\n\n{0}"
                "يُرجى إعادة بدء اللعبة  لتطبيق الإعدادات:\n{0}",

                // "Press '{0}' to Clear"
                "اضغط '{0}' للمسح",

                // "Unlock {0}"
                "فتح {0}",
        };
        string marker = "{0}";
        foreach (var msgstr in tests)
        {
            var rtl = FixupRightToLeft(msgstr);
            Debug.Log(msgstr);
            Debug.Log(rtl);
            Debug.Assert(rtl.Contains(marker) == msgstr.Contains(marker));
        }
    }

    public static string FixArabic(string text)
    {
        // ArabicFixer messes with angle brackets and quotes:
        // * It converts <color=#{0}>{1}</color> to {#>color=0}<{1}>/color<
        // * It converts <color=#0033ffee>ARABIC</color> to >/color<ARABIC>color=#0033ffee<
        // * It converts '{0}' to '}{'0
        // * It converts \"{0}\" to " {"0}
        // * It converts /{0}/ to }{/0/
        // * It converts /"{0}"/ to /}{0 /
        text = Regex.Replace(text, @"<[^>]+>", "", RegexOptions.None); // remove color. I don't know how else to fix.
        text = Regex.Replace(text, @"/({\d})/", "$1", RegexOptions.None);
        text = Regex.Replace(text, "/\"({\\d})/\"", "$1", RegexOptions.None);
        text = Regex.Replace(text, "['\"]", " ", RegexOptions.None);
        // Since we're passing {0} for formatting, we can't use hindu numbers.
        return ArabicSupport.ArabicFixer.Fix(text, showTashkeel: false, useHinduNumbers: false);
    }
}
@mostafayahia2020
Copy link

Hello , do you know any solution to Brackets ?
when i try to write some equations in Arabic Text it becomes a mess
x*(x+y)
result = )*xx+Y(

@idbrii
Copy link
Contributor Author

idbrii commented Apr 14, 2020

@mostafayahia2020 I'd try keeping your Arabic text separate from your math:

var math = "x*(x+y)";
var result = string.Format(FixArabic("فتح {0}"), math); // my function

If you're not putting quotes or anything funny around your format characters, then you may not need my function. Just make sure you don't use hindu numbers.

@mostafayahia2020
Copy link

I am sorry but i don't know how to use your class or your function inside the scripts
if you can tell me where to put them

@mostafayahia2020
Copy link

image

@idbrii
Copy link
Contributor Author

idbrii commented Apr 18, 2020

Sorry, I used Format wrong. It's supposed to be string.Format:

var math = "x*(x+y)";
var fmt = FixArabic("فتح {0}"); // my function
var result = string.Format(fmt, math);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants