oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Globalizing and Localizing Windows Application, Part 2

by Wei-Meng Lee

In my last article, I talked about how to localize your Windows application using the CurrentCulture property. In this article, we will continue to work on localizing our Windows application so that it can now display different languages, according to the culture selected.

Localizing the User Interface (UI)

Another important aspect of localization is UI localization. You may need to display your text in your Label controls using the local language preferred by the user. I will discuss two approaches to accomplish this:

  • Satellite assembly approach
  • Assembly resource approach

Localizing the UI Using the Satellite Assembly Approach

Let's start by illustrating the first approach. Using the Windows application developed in the last article, let's modify the property of the default Form1 (see Figure 1):

  • Set the Localizable property to True
  • Set the Language to "Chinese (People's Republic of China)"
Figure 1. Changing the Language and Localizable properties in the form for localization

Now, change the text of the labels to the text as shown in Figure 2. Refer to the end of this article to see how to configure your Windows computer to accept Chinese language input.

Figure 2. Modifying the text of the user interface controls

Your modified Windows Form should look like this (see Figure 3):

Figure 3. The modified form and MainMenu control

Look under Solution Explorer and you will notice that underneath Form1.vb (click on the Show All Files button in Solution Explorer) are three resource files with the extension .resx (see Figure 4).

Figure 4. The resources files created for the different languages

These resource files each hold the text used for your various controls (such as the Label and TextBox controls) in the different cultures. You can double-click on any one of them to see its content, such as the one shown in Figure 5:

Figure 5. Examining the resource file for the Chinese in China culture

Now, how do you select the culture to display when your form is loaded? You do so within the New() form constructor, located within the "Windows Form Designer generated code" region.

Imports System.Threading
Imports System.Globalization
Imports System.Resources
#Region " Windows Form Designer generated code "

	Public Sub New()

		'===Set the culture===
		Thread.CurrentThread.CurrentUICulture = New CultureInfo("zh-CN")

		'This call is required by the Windows Form Designer.

		'Add any initialization after the InitializeComponent() call
	End Sub

Note that you add in the following line immediately after the MyBase.New() statement:

  Thread.CurrentThread.CurrentUICulture = New CultureInfo("zh-CN")

Recall that earlier in the last article we used the Thread.CurrentThread.CurrentCulture property for localizing date and number format. For user interface localization, you use the Thread.CurrentThread.CurrentUICulture property.

When the application is executed and the form loaded, you should see something like the following (see Figure 6):

Figure 6. Displaying the form in the Chinese in China (UI) and the English in US (date and numbering format) cultures.

A couple of issues to note about this approach:

  • You can specify any number of languages you want to set for this form. Once a language is selected, you can simply change the text in the user interface. Windows will maintain separate resource files for each selected culture.
  • If no culture information is set during runtime, the user interface will display the default culture (that is, the English text that we typed in originally).
  • If a culture is specified that does not exist in the project, the default settings will apply. For example, in our project I have specified the Chinese in China and default cultures. But if I specify the French culture ("fr-FR") during runtime, the default culture will apply, since the French culture has not been configured.
  • If you do not set the culture in the New() form constructor, you can set it elsewhere in your code. However, the new culture will not take effect until you reload the form again.
  • You can set the user interface to one culture and the display format to another culture. As evident in Figure 6, the user interface is set to the Chinese in China culture while the display format is set to the English in the US format.

Pages: 1, 2, 3

Next Pagearrow