The command prompt in Windows has a number of limitations when it comes to dealing with Unicode – it does not support complex scripts (like Arabic, Hebrew, Indic languages), the raster font selected by default depends on the system locale and only supports glyphs for the current OEM system codepage, redirected output cannot be encoded in Unicode, TrueType fonts used in the command prompt do not do font linking etc. Therefore when you output a string with System.Console.Write[Line] it gets converted to the console codepage for the command prompt.
When creating a multilingual command line application with .NET Framework v2.0 how can you make sure to load resources that are actually displayable? Here is code that demonstrates this (it is using some new features only available in Whidbey):
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Globalization;
using System.Resources;
using System.Reflection;
[assembly: NeutralResourcesLanguageAttribute("en")]
namespace loccons
{
class Program
static void Main(string[] args)
Thread.CurrentThread.CurrentUICulture = CultureInfo.CurrentUICulture.GetConsoleFallbackUICulture();
if ( (System.Console.OutputEncoding.CodePage != 65001) &&
(System.Console.OutputEncoding.CodePage !=
Thread.CurrentThread.CurrentUICulture.TextInfo.OEMCodePage) &&
Thread.CurrentThread.CurrentUICulture.TextInfo.ANSICodePage) )
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en");
}
Console.WriteLine(loccons.strings.txtHello);
This code assumes that you have added strongly typed resources with the base name strings to your project and that the default resources are in U.S. English (this could be any other language too). The resources need to contain a string resource with the name txtHello.
The code first eliminates complex scripts from the UI culture by calling GetConsoleFallbackUI culture – this function falls back to a language most appropriate for complex script cultures, e.g. to French if Arabic (Morocco) was the original UI culture. For non-complex script UI cultures this line is a no-op.
After that the code makes sure that the selected UI culture is actually displayable in the console the code is running in – if not it is falling back to the default UI culture.
Some remarks on how you can actually tweak the console in a limited way to allow display of a language other than the one prescribed by the system locale: