Combine Two String in Different Language Rtl & Ltr

Combine Two String in different Language RTL & LTR

I'm pretty sure that the problem here is that the hebrew date in strDate is carrying unicode characters that make it display right-to-left. That's causing chaos when combined with the 'ordinary' left-to-right string in timeForResponse. The date formatter is picking that up from the hebrew locale.

Try this:

  1. Change your date format string to

[dateFormatter setDateFormat:@"dd.MM.yyyy,EEEE"];


  1. Change your string with format to

NSString *result = [NSString stringWithFormat:@"\u200E%@ | %@", timeForRequest, strDate];

The 0x200E unicode character is invisible but puts the rendering back into left-to-right mode.

After the above, this is the output that I'm getting:

07: 00-16: 00 | 17.08.2016,יום רביעי

concatenation of strings in languages read LTR and RTL

but without the unicode appears as -

مركز صيانة الإحساء (مركز صيانة ، قطع غيار) مركز الصيانة

which is incorrect

(As an Arabic and Hebrew speaking person): The string above is the correct output for your concatenation. Because Arabic is a RTL language, concatenating the string:

مركز صيانة الإحساء (مركز صيانة ، قطع غيار)

with

مركز الصيانة

Should return:

مركز صيانة الإحساء (مركز صيانة ، قطع غيار) مركز الصيانة

Don't let the brackets throw you off. Any IDE will show the last bracket on the right side of the string, but it's actually on the left side (at the end) of the string. The best way to examine a combination of RTL strings with special characters like brackets, periods, commas ect. is to paste the string to a Notepad, and click the right side ctrl + shift to change the text direction to right to left:

enter image description here

How mixing LTR and RTL languages is managed in unicode?

The process is described by the Unicode bidirectional algorithm described here: http://www.unicode.org/reports/tr9/.

By default, text is left to right (level 0). Unicode has special character codes to delimit RTL text within level 0 (level 1). You use special characters to delimit LTR within RTL and so on. I think you can have up to 61 levels of embedding.

HTML tags have the "dir" attribute to specify the default direction.

The process is platform neutral, but you will be relying on the unicode algorithm to get it right.

Java String wrong order concatenation of different languages

This is because the first character is R2L (right to left orientation as in asian languages), so next character becames at the begining (correct orientation):

First char:

الف 
// actual orientation ←

Second char added at L

// add ←
B : الف
// actual orientation →

After this, B is L2R as usual in Europe, so next char (1/2) is added in the right orientation AFTER B:

// → add in this direction
B : 1/2 : الف
// actual orientation → (still)

You can easily test it by copy paste char and writting manually another, you will see how orientation changes depending of the char you inserted.


UPDATE:

what is my solution for this issue, because i made this example only to show what issue i was facing in making some big reports, where data is mix sometimes, it is L2R String and sometimes R2L. And i want to make a string in strictly this format.(

From this answer:

  • Left-to-right embedding (U+202A)
  • Right-to-left embedding (U+202B)
  • Pop directional formatting (U+202C)

So in java, to embed a RTL language like Arabic in an LTR language like English, you would do

myEnglishString + "\u202B" + myArabicString + "\u202C" + moreEnglish

and to do the reverse

myArabicString + "\u202A" + myEnglishString + "\u202C" + moreArabic

See (for the source material)

  • Bidirectional General Formatting for more details,
  • the Unicode specification chapter on "Directional Formatting Codes"

ADD ON 2:

char l2R = '\u202A';
System.out.println(l2R + a + " : " + e +" : "+b);

OUTPUT:

‪الف : B : 1/2


Related Topics



Leave a reply



Submit