RTL issue in string containing English, Hebrew and digits in Android (Java)

杀马特。学长 韩版系。学妹 提交于 2020-06-01 06:44:25

问题


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


回答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.




回答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



来源:https://stackoverflow.com/questions/53684306/rtl-issue-in-string-containing-english-hebrew-and-digits-in-android-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!