Home > Back-end >  How to type placeholder string in RTL with non-Arabic characters
How to type placeholder string in RTL with non-Arabic characters

Time:11-16

I have a simple string with 2 placeholders:

<string name="substring_two_substrings_dash">%1$s - %2$s</string>

I don't know how to type this into Android studio for RTL languages like Arabic, where "1" should appear on the right and "2" on the left. Usually the placeholders are automatically reversed as you type them among other Arabic characters:

<string name="snackbar_lock_out_period_scheduled">تم جدولة الحبس %1$s لمدة %2$s!</string> [Actually this changed when I pasted it here, it looks like "s$2%" in Android studio]

However, for the simple string with just "-" in between the 2 placeholders, it doesn't automatically reverse, because it doesn't detect any Arabic characters. If I do also add an Arabic character in there, it works.

How can I type it in and force it to be RTL?

It crashes at runtime if I manually type it in as:

<string name="substring_two_substrings_dash">s$2% - s$1%</string>

with the error "java.util.DuplicateFormatFlagsException: Flags = ' '

CodePudding user response:

This is the example provided by the documentation for multiple arguments/placeholders in a String resource:

<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>

So, the template of arguments is %o$x where:

  • o: order of the argument within the String
  • x: the format of the argument (number, string,...)

This format ( %o$x ) need to be maintained in English regardless the language you use

And to achieve that, you need to switch back and forth between the desired language and English to write the entire String

For instance to write الرقم الأول 21 – الرقم الثاني 14: you need to follow:

  • Turn on Arabic keyboard: write down -> الرقم الأول
  • Turn on English keyboard: write down -> %1$s
  • Turn on Arabic keyboard: write down -> - الرقم الثاني
  • Turn on English keyboard: write down -> %2$s

To test this:

<string name="test">الرقم الأول %1$s - الرقم الثاني %2$s</string>

getString(R.string.test, 21.toString(), 52.toString())

The number in the <string> doesn't seem readable like it's formatted due to changing between LTR/RTL

UPDATE:

This doesn't answer how to do it for my simple example with no Arabic characters, just a dash in between the placeholders.

The same way applies except that you don't have to switch the keyboard; so you just need to use only the English keyboard and the pattern %o$x when you want to add a placeholder/argument in your String.

In your example <string name="substring_two_substrings_dash">s$2% - s$1%</string>

should be: <string name="substring_two_substrings_dash">%2$s - %1$s</string>

Or if you want to follow the order sequentially: <string name="substring_two_substrings_dash">%1$s - %2$s</string>

  • Related