Recipe 14.27. Localizing the Controls on a Form


You want to make your application available to speakers of other languages.


Sample code folder: Chapter 14\MultiLanguage

Use the features built right into Visual Studio to assist you with the localization process. Windows applications have long supported multiple languages through inter-changeable language-specific resource files. When managing the display language for the fields on your application forms, you can have Visual Studio generate the resource files for you automatically.


Create a new Windows Forms application, and add two Label controls to Form1, named Label1 and Label2. Set Label1's Text property to The message is:, and set Label2's Text property to Good day!. Arrange the controls as shown in Figure 14-23.

Figure 14-23. The English-language interface

The English-language version of the application is ready to compile and use. (Actually, the default-language version is ready to use, and the default language happens to be English.) To enable support for multiple languages on this form, set its Localizable property to TRue.

To enable French-language support, change the form's Language property to French. You will see the form blink briefly. Select Label2, and change its Text property to Bon jour!, as shown in Figure 14-24.

Figure 14-24. The French-language interface

To test both language versions, change the language either to the default language or to French when the program first starts. On the Application tab of the Project Properties window, click the View Application Events button to access the ApplicationEvents.vb file. Add the following code to the MyApplication class in this file:

 Private Sub MyApplication_Startup(ByVal sender As Object, _       ByVal e As Microsoft.VisualBasic.ApplicationServices. _       StartupEventArgs) Handles Me.Startup    ' ----- Prompt to change the culture.    Dim newCulture As String    newCulture = InputBox("Enter new culture string.")    If (newCulture <> "") Then       Threading.Thread.CurrentThread.CurrentUICulture = _          New Globalization.CultureInfo(newCulture)    End If End Sub 

Run the program. When prompted for a culture, leave the prompt empty to default to English, or enter fr to use French. Then, enjoy the results.

To see what's really going on, build the program through the Build BuildWindowsApplication1 menu command. Then locate the folder with the generated application (the fr, which contains a "satellite assembly" containing the language-specific resources.

In addition to building language-specific resources when you design your program, you can add them after release by using the winres.exe application included with Visual Studio. On our system, the link to this program is found in Start [All] Programs Microsoft .NET Framework SDK v2.0 Tools Windows Resource Localization Editor (see Figure 14-25). You must have set the form's Localizable property to TRue to use this tool.

Figure 14-25. The winres.exe localization tool

To use the tool, open the Form1.resx resource file associated with the localized form, select each element whose Text property needs to be localized in turn, and enter in the new language-specific settings. When saving the file, you are prompted for an output language. The tool generates a separate language-specific resource file. We chose to create a Japanese-specific resource file; the tool generated Form1.ja.resx.

To generate the new resource's satellite assembly, recompile the application. If this is not an option, you can generate the file manually. This is a two-step process, and it must be done on the command line. Open the Visual Studiospecific command line using the Start [All] Programs Microsoft Visual Studio 2005 Visual Studio Tools Visual Studio 2005 Command Prompt menu command. Change to the source-code directory that contains the new Compile the .resx file into a .resources file, using the resgen.exe application included with Visual Studio:

 resgen.exe Form1.ja.resx 

The directory now contains a Form1.ja.resources file. Compile it to a satellite assembly using the al.exe (Assembly Linker) program. Enter the command on a single line, not on four lines as shown here:

 al /t:lib /embed:Form1.ja.resources,    MultiLanguage.Form1.ja.resources /culture:ja    /out:MultiLanguage.resources.dll    /template:bin\Release\MultiLanguage.exe 

Now move the new MultiLanguage.resources.dll file to a culture-specific folder within the release directory. You may wish to move the file into a bin\Release\ja folder you create within the project directory. On deployment, the file should be installed in a ja folder within the release directory.

When you run the program again and enter ja for the culture, you'll see the form in Figure 14-26.

Figure 14-26. The Japanese-language interface

Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: