Saturday, September 1, 2007

CultureInfo meets Windows MUI

Culture information is tightly integrated in the .NET framework but often overlooked by developers. Every time you format a string, parse a number or load an embedded resource, the .NET framework takes the current culture into consideration. Culture information is encapsulated  by System.Globalization.CultureInfo and is active on every thread.

Consider the following simple console program and you will see that the current thread has two properties of type CultureInfo: CurrentCulture and CurrentUICulture. CurrentCulture supplies the culture for the thread and is referred to by default when formatting numbers, dates and currency. CurrentUICulture is used by the resource manager to look up culture-specific resources at run time. These properties are easy to confuse and can refer to two different cultures at runtime.

class Program
{
static void Main(System.String[] args)
{
System.Console.WriteLine(
System.Threading.Thread.CurrentThread.CurrentCulture.Name
);
System.Console.WriteLine(
System.Threading.Thread.CurrentThread.CurrentUICulture.Name
);
}
}

Thread culture information is inherited from the operating system. Paste the above code into Notepad and compile it on the command line [csc.exe program.cs]. Run the program and provided you have an English version of Windows you will see "en-US" printed twice. Now open the regional settings control panel, change the format culture to French (France) and run the program. This time you should see "fr-FR" followed by "en-US".


The CurrentCulture is easily changed by setting the format culture in the regional settings control panel. CurrentUICulture is a different story and cannot be changed using regional settings on most versions of Windows. What would you expect to see if you could change the UI Culture for the operating system?


Most versions of Windows are localized by Microsoft for a single neutral culture so you can't change the language for strings displayed on operating system menus and dialogs. Microsoft ships localized versions of Windows for English, Japanese, Spanish, Russian and many other cultures. However, you need a specific build of Windows if you want to change the UI Culture for the operating system.


Windows Multilanguage User Interface (MUI) Version is available for Windows Vista Ultimate and allows you to conveniently switch your UI Culture. Language packs are also available for Windows XP Professional.


I highly suggest a copy of Windows Vista Ultimate if you're building globalized .NET software and want to test the display of localized UI strings. However, if this isn't possible, you can always override the CurrentUICulture in code, shown below. Many applications provide a languages menu to allow the user to set the UI Culture at runtime.

class Program
{
static void Main(string[] args)
{
System.Console.WriteLine(
System.Threading.Thread.CurrentThread.CurrentUICulture.Name
);
System.Threading.Thread.CurrentThread.CurrentUICulture =
new System.Globalization.CultureInfo("fr-FR");
System.Console.WriteLine(
System.Threading.Thread.CurrentThread.CurrentUICulture.Name
);
}
}


No comments:

Post a Comment