AddThis Social Bookmark Button

Print

Globalizing and Localizing Windows Application, Part 2
Pages: 1, 2, 3

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

  • Label
  • TextBox
  • Button
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:

  • Strings.resx
  • Strings.zh-CN.resx

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.

Pages: 1, 2, 3

Next Pagearrow