ONDotNet.com    
 Published on ONDotNet.com (http://www.ondotnet.com/)
 See this if you're having trouble printing code examples


Globalizing and Localizing Windows Application, Part 2

by Wei-Meng Lee
09/29/2003

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:

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

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()
		MyBase.New()

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

		'This call is required by the Windows Form Designer.
		InitializeComponent()

		'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:

Localizing the UI Using the Assembly Resource Approach

Let's now look at the second approach. To do so, let's add a new Form to our project. Populate the form with the following controls (see Figure 7):

Figure 7. Populating the second form

Next, add two resource files to your project (right-click on Project name in Solution Explorer, select Add->Add New Item->Assemble Resource File). Name the two resource files:

The naming convention of resource files deserves special mention. The first one is the resource file for the default culture. The second one is the resource file for the Chinese in China culture. All other cultures beside the default culture must have filenames that end with the specific culture codes (see Figure 8).

Figure 8. Adding two resource files to our project

Double-click on the two resource files and enter the following, as shown in Figures 9 and 10:

Figure 9. Setting the data for the resource file for the default culture

Figure 10. Setting the data for the resource file for the Chinese in China culture

To display the appropriate language for the specific culture during runtime, first you set the culture in the New() form constructor:


#Region " Windows Form Designer generated code "

	Public Sub New()
		MyBase.New()

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

		'This call is required by the Windows Form Designer.
		InitializeComponent()

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

Then you use the ResourceManager class to load the appropriate text property for the various controls:


Private Sub Form2_Load(ByVal sender As System.Object, _
                       ByVal e As System.EventArgs) _
                       Handles MyBase.Load
	Dim RM As ResourceManager = New _
		ResourceManager("WindowsApplication1.Strings", _
		GetType(Form2).Assembly)
	Label1.Text = RM.GetString("Please give us your comments")
	Label2.Text = RM.GetString("Name (optional)")
	Button1.Text = RM.GetString("OK")
End Sub

That's it! If you choose the load the Chinese in China culture during runtime, you should see the following (see Figure 11):

Figure 11. Displaying the second form in the Chinese in China culture.

To switch to another culture, simply set the CurrentUICulture property to the desired culture code (using the CultureInfo class).

WinForms Controls

Some WinForms controls are bounded to the culture set in the operating system. For example, the DateTimePicker and MonthCalendar controls display the date information in the language specified in the Regional and Language Options (in the Control Panel). Figure 12 shows the two controls displaying the date information in Spanish and Chinese, respectively.

Figure 12. Some controls are bound by the operating system's culture setting

Setting the CurrentUICulture or CurrentCulture property has no effect on the language displayed on these controls.

Summary

In this article, I have discussed the two approaches to localizing the Windows user interface -- the satellite assembly and assembly resource approaches. In my next article, I will talk more about the differences between the two approaches and when to use each of them.

Appendix A. Configuring Windows XP for Chinese Language Input

Windows XP comes with built-in support for inputting languages other than English. As a Singaporean Chinese, I am thrilled that I can input Chinese characters into my applications, such as Word, Notepad, and even my .NET applications.

Here is how you can configure Windows XP to support the Chinese language:

  1. Go to Control Panel and double-click on Regional and Language Settings.
  2. Click on the Languages tab.
  3. Check the "Install files for East Asian languages" checkbox (see Figure 13).

    Figure 13. Installing the Chinese language support
  4. Click OK. Windows XP will install the necessary files for the new languages. You will need your Windows XP installation disk.
  5. Windows will restart.
  6. After the restart, go back to the same window (as shown in Figure 13) and click on the Details... button.
  7. The Text Services and Input Languages window will be displayed, as shown in Figure 14.
    Figure 14. Configuring the input languages
  8. Click the Add... button to display the Add Input Language window (see Figure 15).
    Figure 15. Adding a new input language
  9. Select "Chinese (PRC)" as the input language and select "Chinese (Simplified) - Microsoft Pinyin IME 3.0" as the keyboard layout/IME. Click on OK.
  10. Under the Installed services group, select the "Chinese (Simplified) - Microsoft Pinyin IME 3.0"service and click on the Properties... button (see Figure 16).
    Figure 16. Configuring the Chinese language service
  11. In the Conversion mode group, select "Sentence". In the Candidate option group, check the "Prompt step by step" checkbox (see Figure 17). Click OK.
    Figure 17. Configuring Windows for "hanyupinyin" input
  12. You should now see the language bar displayed in the Taskbar (see Figure 18).
    Figure 18. Choosing the input language
  13. To switch to Chinese input, you can either use the Taskbar or press the Left Alt+Shift key combination to toggle between English and Chinese input.
  14. To test it out, use Notepad. As you type the "hanyupinyin" of the Chinese character, a list of characters that matches it will be displayed. To select the desired character, simply press its numeric equivalent (see Figure 19).
    Figure 19. Chinese input using "hanyupinyin"

Wei-Meng Lee (Microsoft MVP) http://weimenglee.blogspot.com is a technologist and founder of Developer Learning Solutions http://www.developerlearningsolutions.com, a technology company specializing in hands-on training on the latest Microsoft technologies.


Return to ONDotnet.com

Copyright © 2009 O'Reilly Media, Inc.