Search code examples
c#winformslocalearabicright-to-left

RTL Arabic text, full stop's at the end not beginning


See below the Testo textbox has the Full Stop correctly at the beginning: enter image description here

In the Watch Window and in memory string variables the FullStop is incorrectly at the End: enter image description here

The result of this is when I graphically process the string the Full Stop (aka dot) is in the wrong position, how should I move it at start?


Solution

  • You can use Visual Studio to create Windows-based applications that support bi-directional (right-to-left) languages such as Arabic and Hebrew, you will need to set the Control.RightToLeft Property

    Gets or sets a value indicating whether control's elements are aligned to support locales using right-to-left fonts.

    Both of these answers failed but helped identify the solution.

    If you want to get the RTL value in code with variables you have to rely on the unicode control characters to mark them as RTL.

    var badWhenSetRTLtrue = textBox1.Text;
    var goodWhenSetRTLtrue = ((Char)0x202B).ToString() + textBox1.Text;
    

    It would make sense to have these as Extension methods:

    partial class StringExtensions {
    
    private const char LTR_EMBED = '\u202A';
    private const char RTL_EMBED = '\u0x202B';
    private const char POP_DIRECTIONAL = '\u202C';
    private string ForceLTR(this string inputStr)
    {
        return LTR_EMBED + inputStr + POP_DIRECTIONAL;
    }
    
    private string ForceRTL(this string inputStr)
    {
        return RTL_EMBED  + inputStr;
    }
    
    }
    

    eg:

    textBox1.Text.ForceRTL();
    

    For anyone wanting to do this with a web browser and HTML (instead of WinForms) you can set the direction: rtl;, eg:

    <p style="direction: rtl;">טקסט</p>
    

    Or either of these answers: https://stackoverflow.com/a/42551367/495455