Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Right To Left Language Bracket Reversed

I am using a StringBuilder in C# to append some text, which can be English (left to right) or Arabic (right to left)

stringBuilder.Append("(");
stringBuilder.Append(text);
stringBuilder.Append(") ");
stringBuilder.Append(text);

If text = "A", then output is "(A) A"

But if text = "بتث", then output is "(بتث) بتث"

Any ideas?

like image 641
daniely Avatar asked Jan 24 '13 14:01

daniely


2 Answers

This is a well-known flaw in the Windows text rendering engine when asked to render Right-To-Left text, Arabic or Hebrew. It has a difficult problem to solve, people often fall back to Western words and punctuation when there is no good alternative word available in the language. Brand and company names for example. The renderer tries to guess at the proper render order by looking at the code points, with characters in the Latin character set clearly having to be rendered left-to-right.

But it fumbles at punctuation, with brackets being the most visible. You have to be explicit about it so it knows what to do, you must use the Unicode Right-to-left mark, U+200F or \u200f in C# code. Conversely, use the Left-to-right mark if you know you need LTR rendering, U+200E.

like image 107
Hans Passant Avatar answered Nov 09 '22 07:11

Hans Passant


Use AppendFormat instead of just Append:

stringBuilder.AppendFormat("({0}) {0}", text)

This may fix the issue, but it may - you need to look at the text value - it probably has LTR/RTL markers characters embedded. These need to either be removed or corrected in the value.

like image 39
Oded Avatar answered Nov 09 '22 06:11

Oded