(Print this page)

Localization
Published date: Thursday, July 1, 2004
On: Moer and Éric Moreau's web site

People often get Globalization and Localization mixed up probably because these two concepts are often used together. To differentiate them, here is a short definition of each of them. Globalization is the process of formatting data based on a culture. For example, in the United States, a period is used as a decimal place while the coma is used in other countries. Localization is the process of displaying languages and graphics based on a culture. This article will introduce you to Localization only.

Many methods were used through the years of computing to offer multilingual applications. I have seen text files, databases, binary files and even … hard coding (ouch!). You probably have seen the same techniques and maybe some more. Each of these methods has good and bad marks.

Visual Studio .Net strongly recommend the use of resources files to implement localization. At least 2 methods are offered to create resources files.

The first method is a manual process that assumes the creation of text files (XML) and the use of the Resource File Generator (resgen.exe). The XML file can be edited with any text editor, the VS.Net IDE, or the Windows Forms Resource Editor (winres.exe).

The second method is much more integrated. This is the method I will demonstrate here. The result will still be a resource file. Instead of building the resources files into a separate process, the VS.Net IDE now let you simply use the properties of the controls of your forms to create resources files automatically.

Creating the base application

To test the creation of a localized application, we need a sample application. Nothing sophisticated. A simple application having a couple of controls will be more then sufficient.

Create a simple Windows application form like the following one as you always do.

Up to here, nothing special has been done but the application is not yet localizable. This is where your current applications are standing.

Figure 1: The sample application

Implement localization

The first step (after creating the interface) is to make the form localizable. To accomplish this, you need to set a form’s property. This property is named Localizable. Its default value is False. You need to set it to true.

There is already a resource file!

If you ever looked at the content of your source code folder, you may have seen that each form you created (ie: Form1.vb), you also find a resource file (ie: Form1.resx). This resource file is automatically used as the default resource file when you create your assembly (.exe).

Figure 2: The solution Explorer

You can see its content directly from the VS.Net IDE. From the “Solution Explorer” window, click the “Show all files” icon (the fourth icon from the left).

Showing all the files will let you expand your forms (Form1.vb in my example) to discover that a resource file is really present (Form1.resx). Double-click the resource file. An editor will open your file. You will be able to recognize the properties and their values displayed into a grid.

Figure 3: The resource editor

Adding a new supported language

Now go back to the form and be sure the form itself is selected. Find the Language property into the form’s property. Set this property to French. If you look back at the solution explorer, you will find that a new resource file has been added (Form1.fr.resx). This file contains resources for the French localization.

Modify the Text property of each control to the value you see in the image here and also change the Save button location according to figure 4.

Figure 4: The French version

Double-click the French resource file (Form1.fr.resx). You should find a lot less properties then into the Form1.resx). The important lines to notice are the Text properties that are displaying the French caption and also the Location property of the Save button.

Now run your application. In which language is your application running? When we have started the application we were looking at the French interface and the UI now displays in English! This is simply because the default locale is the English one. We need to tell the application that we want to switch to another locale (more on this later). For now stop the application to go back to the VS.Net IDE.

Look back into the Solution Explorer, under the bin folder (be sure that Show All files option is still selected), you will discover an “fr” folder containing a resource DLL. This DLL contains your French localization. For each new language you are adding to your project, a folder like this one will be created. This means that the application .EXE itself does not contains all the resources files. It contains the default one (in English in our case). All others are in separate DLLs. This means that you can install only desired resources to users.

Displaying a localized form

To display our form in French, we need to change the current thread culture. A single line of code is required to do this but … it is required that this line is executed before the call to the InitializeComponent method (otherwise, it would have no effect). This means that a form cannot be translated once it is loaded into memory. Open the code editor for Form1.vb and find the Sub New method. Add the call to change the current culture like shown in the figure 5.

Figure 5: The form's constructor

Re-run the application. If you did everything correctly, your interface should appear in French. Notice the save button location. This means that each resource can have different size and location in addition to the caption. I suggest that you customize size and location to a minimum. Imagine the testing of these forms for complex forms!

The parameter to the CultureInfo class constructor should be the same as the name of your folder containing the resource file under the bin folder.

If you pass an invalid parameter (replace “fr” with “zz”), an exception will be raised telling you that the culture is not supported.

If you pass a valid culture code (replace “fr” with “it”) but you did not supply a resource file for it, the default culture will be displayed.

Not only labels

Localization is not only a mean of translating the caption of labels. The technique shown here can also handle images. In the downloadable demo, I have changed the content of a PictureBox control not at runtime but at design time. Just like we did with caption, I have set the Image property in the (default) language to an image and I have set the same control to a different image for the French language. The image is saved into the resource file and the correct image is displayed at runtime just like labels.

Changing the CultureInfo at runtime

One problem you may have seen going through all this is that the CultureInfo class needs to have its value changed before the form is instantiated. In the downloadable demo, I have added a menu form that shows two buttons: one to display the form in English and the other to display the form in French. It is important to set the desired culture before creating the instance of your form (before calling the New).

More on the subject

If you want to learn more on the topic, there is a complete section on Globalization and Localization on MSDN titled “Planning World-Ready Applications”.

Conclusion

This is only the first step to localize an application. You also need to think about your message boxes, your help file, your reports, your documentation, … but at least this part can be done easily and even after your application is completed!

I hope you appreciated the topic and see you next month.


(Print this page)