Synopsis

This post shows a simple application, CultureSwitcher.exe, which allows developers to simplify some aspects of testing localized applications.

Introduction

If your WPF application targets an international audience, ie an audience of one or more cultures (language/country) than your own, you need to spend the time to localize the application and test it.

There are two ways to legitimately test a localized application:

  1. Run it on a localized OS. 
  2. Run it on an OS with the appropriate Multilanguage User Interface (MUI) language pack.

However, the effort can be non-trivial during the development process. While you develop an application, you want to be able to quickly test that the appropriate mechanics are in place ie so that when you do test your application on an appropriately configured machine, you are reasonably confident that the application code will handle localized data and OS.

A common technique for testing an application in a different culture is to change the application's UI culture, using code like the following:

using System.Globalization; // CultureInfo

using System.Threading; // Thread

using System.Windows; // Application

 

namespace ApplicationToLocalize

{

  public partial class App : Application

  {

    public App()

    {

      // Switch current (main) thread to new UI culture eg fr-FR

      Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-FR");

    }

  }

}

As shown here, a UI culture is encapsulated by the CultureInfo class, and each thread of execution has one associated with it. To change the UI culture for a thread simply requires replacing the UI culture with another, which is what this code does.

The Problem

Unfortunately, this technique isn't great, since it requires you to change your application code to something like the above sample, rebuild your application, and run it. Furthermore, you need to repeat these steps for each culture you'd like to test against.

A Simple Solution

For this reason, I created the attached sample application, which allows you to run an application in a specific UI culture without requiring the above code changes to your application.

The sample application builds a windows application called CultureSwitcher.exe. You can launch the application from either the Windows desktop or a command prompt, in either case specifying an application to load in a specific UI culture, and the specific UI culture to load it in.

The following figure shows how the application provides a window to collect this information from you:

From the command prompt, you provide the required information as command line parameters, as shown in the following figure:

Note - the command line parameters are:

  1. /apppath:<path to application to run in specified culture>

  2. /uiculture:<short uiculture name eg fr-FR, en, en-US>

The previous figure shows the usage.

At a high level, CultureSwitcher.exe changes the UI culture for the current thread of execution to the desired UI culture before executing the specified application in an new application domain. Here are the relevant code bits:

using System; // AppDomain, Guid

using System.Globalization; // CultureInfo

using System.Threading; // Thread

 

namespace CultureSwitcher

{

  static class Launcher

  {

    public static void LaunchAppInSpecifiedCulture(

      string appPath, string uiCulture)

    {

      // Switch UI culture

      CultureInfo desiredUICulture = new CultureInfo(uiCulture);

      Thread.CurrentThread.CurrentUICulture = desiredUICulture;

 

      // Load app to test into a new AppDomain

      AppDomain childAppDomain =

        AppDomain.CreateDomain(Guid.NewGuid().ToString());

      childAppDomain.ExecuteAssembly(appPath);

      AppDomain.Unload(childAppDomain);

    }

  }

}

You can create CultureSwitcher.exe by compiling the VS project provided in the attached zip file. You can also test it on a test application, also provided with the attached zip file. The test application has been localized to two cultures, "en-US" and "fr-FR".

Important

Again, CultureSwitcher.exe is a simple developer aid; comprehensive localization testing should be performed in appropriately configured environments.

Further Information

For information on localization in WPF, you can visit the WPF SDK. You can also read a comprehensive, recommended article here.