Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numbers inside TextView getting reversed when formatted in RTL

Numbers inside TextView are getting reversed when formatted in RTL. When numbers are at the end of a text inside a TextView they getting reversed. How can I solve this programmatically?

As an example, the numbers below are reversed:

image

They should be displayed like:

image

like image 262
Ido Kahana Avatar asked May 31 '18 12:05

Ido Kahana


1 Answers

The misunderstand: Digits in RTL languages like ARABIC should be written from RTL with the arabic digits to avoid any problems i.e: "تم إرسال رسالة نصية للرقم ١٢٣٤" Note that I wrote "رسالة نصية" NOT "SMS رسالة".

The problem and it's solution: Mixing more than one direction languages required more steps, you need to tell the system "hey this is RTL word, add as it to sequence". So you may need to do this implicitly, i.e:

\u200f + تم إرسال رسالة نصية إلى + number

Consider StringBuilder: It's very painful for developer to develop something for RTL language using plus(+) notation, this much confusing and error prone.

A better way:

builder.append("\u061C").append(" تم إرسال رسالة نصية لـ").append("\u200E").append("+0123456789")

Consider BidiFormatter: Utility class for formatting text for display in a potentially opposite-directionality context without garbling

Example:

String text = "{0} تم إرسال رسالة نصية لـ ";
String phone = BidiFormatter.getInstance().unicodeWrap("+961 01 234 567");
String result = MessageFormat.format(text,phone);

Now, result will be formatted properly.

More examples on how BidiFormatter work.

like image 94
Ibrahim Ali Avatar answered Sep 29 '22 23:09

Ibrahim Ali