skip to Main Content

When I type in Arabic in the print commands, for example, or any other commands in Visual Studio Code, the result appears in the terminal in reversed Arabic .for example

print ("السلام عليكم")

it’s appeared in terminal like:

م ك ي ل ع م ا ل س ل ا

2

Answers


  1. The terminal in Visual Studio Code (and many other code editors) doesn’t fully support right-to-left text rendering.

    You can try to use the following codes to make it work:

    def print_arabic(text):
        print(text[::-1])
    
    print_arabic("السلام عليكم")
    
    Login or Signup to reply.
  2. There are a few possible approaches:

    1. Do not use the vscode terminal, choose an alternative terminal/console with better bidi support. Although most terminals/consoles have poor bidi support
    2. Convert Unicode Arabic text from logical to visual ordering using the python-bidi and arabic-reshaper packages.
    3. Convert Unicode Arabic text from logical to visual ordering using the pyfribidi package.

    As indicated in the OP, the vscode terminal has serious bidirectional text rendering issues, so the simple code will not work:

    text = "السلام عليكم"
    print(text)
    

    So instead, using python-bidi and arabic-reshaper:

    import arabic_reshaper
    from bidi.algorithm import get_display
    print(get_display(arabic_reshaper.reshape(text)))
    

    python-bidi is responsible in convert from logical to visual reordering, and arabic-reshaper converts Arabic characters to appropriate presentational forms.

    Using pyfribidi:

    from pyfribidi import log2vis, RTL
    print(log2vis(text, RTL))
    

    pyfribidi is a wrapper around the fribidi library. If you have libraqm support enabled for bidi support in Pillow or in the mplcairo backend for matplotlib you probably already have fribidi available.

    Personally I prefer option 1. Options 2 and 3 are ugly hacks, but sometimes necessary.

    enter image description here

    As an example of what option 1, may look like, using the Terminal app on macOS:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search