powershellterminalarabicright-to-left

Displays Arabic Text Right-to-Left In PowerShell


The original text is $text = "مرحبا بك في PowerShell"

When I pasted it into PowerShell, the Arabic text order changed to reverse automatically:

arab

I tried to solve it with:

[Console]::OutputEncoding = [System.Text.Encoding]::UTF8

The text Still Left-to-Right.
How to set Arabic text Right-to-Left?


Finally, I found Reverse() method like this:

$Text  = "مرحبا بكم"
$charA = $Text.ToCharArray()
[array]::Reverse($charA)
-join $charA

Perhaps there is another method that is easier and simpler.


Solution

  • Your symptom implies lack of bidirectional text-rendering support in your terminal application, which, judging by the screenshot, is the Windows console host (conhost.exe, which creates regular, individual console windows)

    Because Windows Terminal shares the same technical foundation as the console host, the former unfortunately lacks that support too, as of this writing.

    Popular third-party alternative ConEmu lacks support too.

    Even embedding explicit RTL (right-to-left) characters such as the ARABIC LETTER MARK, U+061C does not help.

    Conversely, applications that do support bidirectional text automatically display embedded Arabic text correctly, i.e. RTL; Visual Studio Code is an example.


    Your workaround involving reversal of characters is not only cumbersome, but is only suitable for display output, given that it involves modifying the string data.


    There are some terminal emulators (applications) that already support bidirectional text, notably the native macOS terminal, Terminal.app, and possible some available on Linux.


    However, there seem to be still-unsolved fundamental questions on how to handle bidirectional text in terminal emulators, as the aforementioned ConEmu GitHub issue notes:

    It seems that the way Unicode describes bidirectional text printing characters in correct order requires knowing full text (chunk of text) in advance. That is not really practical for a terminal. Printing characters incrementally can change positions of previously printed characters.

    From the look of https://www.arabeyes.org/ArabeyesTodo#Terminal_Emulators, no one really knows how bidirectional text should interact with various terminal control characters like moving/reading cursor position, clearing until end of line and others.