Android Hebrew (RTL) Integration

2019-03-14 06:20发布

问题:

I'm working on a relatively simple Android app. I want it to have an English version as well as a Hebrew version. (RTL Right to Left Alignment)

I have manually change the alignment to right in layout xml file. When a sentence contains digits (in the middle of it), the digits appear in a mirror view: 29 appears as 92, 21:45 appears as 54:12 and 2,000 appears as 000,2.

Also, when a sentence starts with digits or English characters, they get thrown to the end of the sentence messing it all up.

I think for android version 4.0.3 it supports Hebrew. I have check that in emulator.

So for older versions is there correct way to implement Hebrew? Please help.

回答1:

I think that Android's bidi analysis algorithm has some flaws. Unicode has two invisible, strongly directional characters that might help with these problems:

  • U+200E - left-to-right mark
  • U+200F - right-to-left mark

For the digit order problem, try putting left-to-right marks (U+200E) on both sides of the digit sequence.

Unicode also has the following bidi formatting codes:

  • U+202A - left-to-right embedding
  • U+202B - right-to-left embedding
  • U+202C - pop directional formatting (cancels the previous embedding or override)
  • U+202D - left-to-right override
  • U+202E - right-to-left override

For the problem with English fragments in Hebrew text, it might be as simple as putting a right-to-left mark before the English. (Android's algorithm may be under the impression that the paragraph is left-to-right since the first characters are English.) If that doesn't work, perhaps try surrounding selected text with some combination of formatting codes. (I'd try left-to-right embedding followed by pop directional formatting. I'd also try right-to-left embedding around everything combined with selective explicit right-to-left embeddings.)

The way these are supposed to affect text layout are defined by the Unicode Bidirectional Algorithm Unicode Standard Annex #9. However, if Android's implementation is broken (and I suspect it is), the best you can do is trial-and-error until you get things looking right. Good luck.

EDIT

As far as code is concerned, here's an example of how it might be done in Java:

String text = "גרסה \u200e2.100\u200e זמינה";

In XML, it might be:

<string name="update_available">גרסה &#x200e;2.100&#x200e; זמינה</string>


回答2:

here is an example from my hebrew string xml, Thanks to Ted Hopp's answer:

you need to add '\u200e' before the char that causes you the problem:

<string name="basic_text1">המר על תוצאת המשחק\u200e:</string>

and the result will be:

 :המר על תוצאת המשחק