Achim Ruopp

Tales from the Crossroads of Internationalization and .NET

Localization of command line applications

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) &&

                           (System.Console.OutputEncoding.CodePage !=

                           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:

  • In the properties of the command prompt and you can choose the TrueType font (more info on this can be found in Windows Help). In addition you have to set the appropriate OEM codepage by using the chcp or the mod con cp= command. Like this you can for example display Russian text on an English machine, but because the TrueType fonts are not font-linked you can only display languages that are supported by the selected font. A good tip is that you can actually create this customization for a shortcut to a command prompt by setting the property on this shortcut and calling chcp in a startup batch file.
  • In Windows 2000 or higher you can use chcp or mode con cp= to set the codepage to 65001 (UTF-8) – this is an unsupported feature. Like this no characters will be replaced with the standard replacement character ‘?’, but the font display limitations are of course still there.
Published Wednesday, August 11, 2004 12:52 PM by AchimR

Comments

 

Echo said:

It's informative!
August 29, 2004 8:10 AM
 

Localization of command line applications said:

December 5, 2007 3:57 PM
 

Achim Ruopp Localization of command line applications | Cellulite Creams said:

June 13, 2009 6:02 AM
Anonymous comments are disabled

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker