Question

I believe it was possible to show Arabic characters on a console application 13+ years ago, since the days of Windows ME.

Now i am using Visual Studio 2013, On a Windows 8, and the following code shows:

????? ??

   Console.OutputEncoding = System.Text.Encoding.Unicode;
   Console.WriteLine("مرحبا بك");

Is there anyway to show Arabic characters in the console output?

Was it helpful?

Solution

There are several issues to resolve to get this to work.

  • You need a font that supports both Arabic AND the windows console.

See KB : Necessary criteria for fonts to be available in a command window

The font must be a fixed-pitch font.
The font cannot be an italic font.
The font cannot have a negative A or C space.
If it is a TrueType font, it must be FF_MODERN.
If it is not a TrueType font, it must be OEM_CHARSET.
  • You must install the font.

For testing, I used DejaVu Mono, which is one of the few that supports Arabic. Arabic is a tough language to make a monotype font with since the aesthetics of the language do not work well with a fixed width for each character. Nevertheless, this font makes an honest effort. For other possible alternatives, see :

complete, monospaced Unicode font?

The font must be installed in the normal way for your version of Windows (in Vista/7/8 this is right-click, Install on the .ttf file). Once this is done, you have to follow the directions in the KB.

  1. Registry Editor --> HKLM\Software\Microsoft\WindowsNT\CurrentVersion\Console\TrueTypeFont
  2. Add a new string value named "000" with the value DejaVu Sans Mono
  3. Reboot

enter image description here

Once you've rebooted, you can change the font in the console by selecting "Properties" from the console menu and changing the font in the "Font" tab.

enter image description hereenter image description here

Result.

enter image description here

... so after all that, we discover that the console does not support Right-To-Left languages. I guess you could use a function like :

static string Reverse(string text)
{
   if (text == null) return null; 
   char[] array = text.ToCharArray();
   Array.Reverse(array);
   return new String(array);
}

and then do

Console.OutputEncoding = System.Text.Encoding.Unicode;
Console.WriteLine(Reverse("مرحبا بك"));

enter image description here

OTHER TIPS

Since the answer here doesn't solve your problem. I'm posting an alternate walkaround that may help for testing stuff.

If you can use a WPF project instead of a console application you'll be able to:

  • View arabic text on the screen.
  • Scroll over the entire output of your execution
  • Easily copy the output
  • Keep using C# as a coding language

Create a WPF project and add a multiligne textBox to your WPF design that has the following properties:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <TextBox HorizontalAlignment="Stretch" AcceptsReturn="True"
             TextAlignment="Right"
             VerticalScrollBarVisibility="Auto"
             Name="textBox1" VerticalAlignment="Stretch"/>
</Grid>

TextAlignment to right as in arabic, VerticalScrollBarVisibility to view all the output and AcceptsReturn to have a multiline textBox. The HorizontalAlignment and VerticalAlignment set to stretch to fill all the displayed window.

You could add a method in the code section to ease the adding of String in this textBox, the method could be like this:

    private void writeToTextBox(string textToWrite)
    {
        textBox1.Text += textToWrite + "\n";
    }

The global code behing would be:

namespace WpfApplication1
{
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        createSourateFromXML();

    }
    private void createSourateFromXML()
    {
        string xmlquranfile = @"C:\Users\hp\Downloads\quran-simple.xml";

        XmlDocument xml_quran = new XmlDocument();
        xml_quran.Load(xmlquranfile);
        foreach (XmlNode soura in xml_quran.DocumentElement.ChildNodes)
        {
            writeToTextBox(soura.Attributes["name"].Value);
        }
    }
    private void writeToTextBox(string textToWrite)
    {
        textBox1.Text += textToWrite + "\n";
    }
}

The foreach loops over names in my xml file and adds them to the WPF textBox. This is a screenshot of the execution result http://i.imgur.com/d0jql3z.png

You can tune the display by changing the textBox properties, things like font, style, size are all customizable.

You can use simple asp .net core and preset your data in the browser

  context.Response.ContentType = "text/plain; charset=utf-8";
  await context.Response.WriteAsync("بسم الله الرحمن");

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top