1

I am trying to print Urdu sentence in C#. Let's say I have to print

10 apples, 20 oranges.

My expectation is, it should print (well, I can't even get it typed here)Urdu Translation:

Got this translation from Google Translate just to elaborate my query.

In C#, I've the Urdu strings for apples and oranges. Console.WriteLine() shows the format correctly, albeit with ????

So I see,

???? 20 ???? 10

However when I see the print preview, I see this instead: 10 سیب، 20 سنتری

I'm constructing the string by adding strings at the start of the string variable. However, it still displays incorrectly when printed.

Here is the sample code:

static void Main(string[] args)
        {
            string apples = "سیب،";
            string oranges = "سنتری";

            // Creating Urdu string for 10 apples, 20 oranges. Urdu is read from right to left. So created the string accordingly  
            string _10_apples_20_oranges = oranges + " 20 " + apples + " 10";
            Console.WriteLine("output: " + _10_apples_20_oranges);
    
            string[] lines = { _10_apples_20_oranges };
            System.IO.File.WriteAllLines(@"test.doc", lines);

        }

test.doc file contains ‫سنتری 20 سیب، 10

This does not match the expected output given in the image above.

Any pointers would be really useful.

Thank you.

Community
  • 1
  • 1
user2039804
  • 119
  • 1
  • 7
  • 1
    Questions seeking debugging help ("why isn't this code working?") must include the desired behaviour, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Albireo Nov 30 '15 at 13:59

1 Answers1

0

In your code you are concatenating string pieces in visual order: you know that you want to see the word 'oranges' at the left, so you put it at the start of your string. Then you add the other string pieces in their visual order from right to left. Most text editors that are aware of bidirectional text display issues expect input in logical order (in your example 10, apples, 20, oranges), and once they discover characters that belong to a right-to-left script they will attempt to determine the correct visual order, which is what happens in your print preview. The console output is not bidi-aware: it displays those characters that are supported by your font in logical order (this answer offers a workaround).

So, the problem is not your code, but your expectation. To find out a bit more about visual vs logical order you could read this W3 article, but if you prefer to dive in at the deep end you can study the Unicode Bidirectional Algorithm.

Community
  • 1
  • 1
Jenszcz
  • 547
  • 3
  • 9
  • Thank you. What I'm seeing is, when the number is added to this string, the order changes. If only Urdu is used, it works fine. The moment I use 10/20, the order changes. I need to get it working with English characters. – user2039804 Dec 04 '15 at 09:42
  • As long as you have only RTL string pieces there is no difference between logical and visual order. Only if your string contains both LTR and RTL string pieces this difference will come into play for any edit controls that support the Unicode bidirectional algorithm. I am not sure what exactly you mean by 'get it working'. If you want to create strings with bidirectional content that look the same in the console and in a browser or a rich edit control that's not possible, as far as I know. – Jenszcz Dec 04 '15 at 16:11
  • When I print the string containing Urdu and English characters, the file in which I write the string contains English RTL ordering and not the Urdu LTR ordering. However, when it is just Urdu, it contains the LTR ordering. By 'get it working', I meant, I want the mixed character string to be LTR instead of current RTL order. – user2039804 Dec 07 '15 at 10:37