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.
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:
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):
Localizable property to TrueLanguage 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()
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:
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.
|
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):
LabelTextBoxButton
|
| 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).
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.
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.
|
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:
![]() |
| Figure 13. Installing the Chinese language support |
|
| Figure 14. Configuring the input languages |
|
| Figure 15. Adding a new input language |
|
| Figure 16. Configuring the Chinese language service |
|
| Figure 17. Configuring Windows for "hanyupinyin" input |
|
| Figure 18. Choosing the input language |
|
| 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.