Test localized programs in C#

[Test localized programs][Test localized programs]

The example Localize a program in C# explains how you can localize a program so it displays different user interfaces depending on the system’s locale. After you localize it, the program automatically selects the correct locale at runtime. Because it selects the locale automatically, it’s hard to test the program for different locales. (It would be a serious hassle to have to reset your operating system to use a locale.)

Fortunately you can make a program select a particular locale so you can test the user interface. To make a program select a specific locale instead of picking one automatically, add the blue code in the following to the form’s constructor.

public Form1()
{
    // English.
    //Thread.CurrentThread.CurrentCulture =
    //    new CultureInfo("en-US", false);
    //Thread.CurrentThread.CurrentUICulture =
    //    new CultureInfo("en-US", false);

    Thread.CurrentThread.CurrentCulture =
        new CultureInfo("de-DE", false);
    Thread.CurrentThread.CurrentUICulture =
        new CultureInfo("de-DE", false);

    InitializeComponent();
}

Note that this code must come before the call to InitializeComponent because that’s where the locale-specific user interface is loaded.

The culture code “en-US” means English in the United States, and the code “de-DE” means German in Germany. For a list of more than 100 culture values that you can use in code, such as en-US and de-DE, see Microsoft’s Table of Language Culture Names, Codes, and ISO Values Method.

After testing, don’t forget to comment out the code that selects the locale so the program can select the locale automatically on the end user’s system.


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in globalization, internationalization, localization and tagged , , , , , , , , , , , , , . Bookmark the permalink.

One Response to Test localized programs in C#

  1. Pingback: Change a program's locale at runtime in C# - C# HelperC# Helper

Leave a Reply

Your email address will not be published. Required fields are marked *