0

I have an issue when mixing in one string English, Hebrew and digits. The order of digits next to Hebrew is getting reversed, no matter what order I make - fist digit and then text, of first text and then Hebrew - it's getting reversed to: on the left digit, on the right text. My text example is:

String leftPart = "10 gr";
int numder = 8;
String hebrewText = "כפות";
String rightPart = hebrewText + " " + number;
String finalString = leftPart + " · " + rightPart; //10 gr · כפות 8

I want to display the digit 8 in the end of this string, after the Hebrew word, not before it, but I'm unable to do it even here...it's getting reversed because of the English text in the begging.

Even if I change the order to:

String rightPart = number + " " + hebrewText ;

the result is the same...

Any ideas? It's looks like something simple that I'm missing

enter image description here

Paebbels
  • 15,573
  • 13
  • 70
  • 139
Natalie
  • 163
  • 1
  • 3
  • 15
  • *I want to display the digit 8 in the end of this string, after the Hebrew word, not before it* -- this is exactly what you get in your screenshot. '8' is after the Hebrew word, because for Hebrew ***after*** means *to the left from*. – Alex Cohn Dec 10 '18 at 08:06

2 Answers2

2

A tip for forcing English to be shown nicely when mixed with Hebrew:

Wrap the English (or numbers) words with LRI and PDI (check here: https://unicode.org/reports/tr9/ ) .

For example, instead of these (first word is in English) :

    <string name="test">ABC היא האפליקציה הכי טובה</string>
    <string name="test2">%1$s היא האפליקציה הכי טובה</string>

Use these:

    <string name="test">\u2066ABC\u2069 היא האפליקציה הכי טובה</string>
    <string name="test2">\u2066%1$s\u2069 היא האפליקציה הכי טובה</string>

Other useful ones can be found here:

https://stackoverflow.com/a/10989502/878126

android developer
  • 114,585
  • 152
  • 739
  • 1,270
1

Nothing is screwing up here, this is actually correct behavior. The number is coming after the end of the hebrew word- the end of the hebrew word is on the left. What you seem to want is for the number to come before the hebrew word. But when you combine it with english like that it doesn't know tht the number is supposed to be bound to the hebrew part and not the english part, so putting it before the hebrew doesn't work either.

I'd suggest putting the number before the hebrew part and wrapping the number and hebrew text in unicode right to left mark characters, to tell it explicitly the 8 is part of the right to left text.

Alternatively you could put the number after the hebrew text but use an rtl mark before the hebrew and a ltr mark after. Which is probably a slightly better way of doing things overall if you want more complex embedding elsewhere.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • I've added a screenshot. The behavior is not as expected - the number appears to the left of the Hebrew word instead of to the right, after the word. – Natalie Dec 08 '18 at 16:26
  • 1
    The behavior is as expected, you just don't understand how it works. Numbers are displayed in either an LTR or RTL context, based on what comes before them. If you put the number after the hebrew, its in an RTL context so it appears to the left of the hebrew word. If you put it before, then add english before it, it appears in an LTR context and is laid out to the left of the hebrew word. The answer is like I said above- explicit LTR and RTL marks – Gabe Sechan Dec 08 '18 at 16:30
  • As I mentioned in my question, even if I change the order to String rightPart = number + " " + hebrewText; the number is still not in the end of the string, I need to have the number in the end of the whole string. How can I do it? – Natalie Dec 08 '18 at 16:39
  • 1
    @Natalie I've told you why that happens, and told you the answer twice. Explicit LTR and RTL marks. – Gabe Sechan Dec 08 '18 at 17:29