There's no sense postponing the introduction to the localization features of Visual Studio, because they are so easy to use. You already know about the application-wide project properties resource editor. Instead, let's look at the amazing part: localizing forms and controls right in the Visual Studio form editor. You might as well start up Visual Studio and try it out with me, because it's just so fun.
Here's a cute but relatively harmless Windows Forms application that writes your name upside down. I've added some Label controls, a TextBox control, and a PictureBox control to a form, as shown in Figure 18-3.
Figure 18-3. A typical Windows Forms application
Then I added the following source code to the form.
Private Sub TextBox1_TextChanged( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles TextBox1.TextChanged ' ----- Force a redraw. PictureBox1.Invalidate() End Sub Private Sub PictureBox1_Paint(ByVal sender As Object, _ ByVal e As System.Windows.Forms.PaintEventArgs) _ Handles PictureBox1.Paint ' ----- Draw the blank background. e.Graphics.Clear(SystemColors.Window) e.Graphics.DrawRectangle(SystemPens.InactiveCaption, _ 0, 0, PictureBox1.Width - 1, PictureBox1.Height - 1) ' ----- Change the orientation of the display. Dim saveState As Drawing2D.GraphicsState = _ e.Graphics.Save() Dim mirrorMatrix As New Drawing2D.Matrix( _ 1, 0, 0, -1, 0, PictureBox1.Height) e.Graphics.Transform = mirrorMatrix ' ----- Draw the text. e.Graphics.DrawString(TextBox1.Text, TextBox1.Font, _ SystemBrushes.WindowText, 1, 4) ' ----- Put everything back. e.Graphics.Restore(saveState) End Sub
When you run the program, it creates a mirror image of whatever you type in the TextBox control. Figure 18-4 shows me playing with the program instead of meeting this chapter's submission deadline.
Figure 18-4. Look Ma, I'm upside down
As interesting as this program may be, it is neither fully globalized nor localized. It's almost globalized. All we need to fully globalize it is to "throw the switch" on the form that enables later localization. This is done through the form's Localizable property. Change this property from False to True. Ta da! Your form is globalized!
Now for part 2: localization. Here are the steps to localize the form.
That's it. Whenever the form's Language property is changed to something other than (Default), Visual Studio starts recording all form and control changes into a separate form-specific and language- or language-culture-specific resource file.
You can localize the form with multiple languages. Each time you change the Language property to another language or language-culture selection, the changes to the form or controls apply only to that selection. Whatever you change gets saved in a separate resource file.
Let's try it with the sample mirror program. I'm going to choose Japanese for the localization language. First, I set the form's Language property to "Japanese." The form momentarily blinks, but there is no other noticeable change. It looks just as it did in Figure 18-3.
Next, I change the Text properties of the form and of each of the label controls to their Japanese language equivalents (see Figure 18-5).
Figure 18-5. The name-mirror program in Japanese
Do you notice how the shorter Japanese language labels are farther away from the text and mirror display fields? Does it bother you as much as it bothers me? To get it out of my mind, I will resize the two fields a little larger by stretching them to the left, as I've done in Figure 18-6.
Figure 18-6. The Japanese version with adjusted fields
The amazing part is that if you set the form's Language property back to "(Default)," not only will the labels return to English, but the resized text and mirror fields will return to their "natural" sizes. Although I haven't checked out every property, the localization feature seems to impact all display elements of each control.
The program is now fully localized for English (the default language) and Japanese. Normally, the Japanese resource would be used only on a system running the Japanese version of Microsoft Windows. But we can force the program to use Japanese by changing its "user interface culture." In the application's startup code (the MyApplication_Startup routine in the ApplicationEvents.vb file), I add the following code.
Private Sub MyApplication_Startup(ByVal sender As Object, _ ByVal e As Microsoft.VisualBasic.ApplicationServices. _ StartupEventArgs) Handles Me.Startup If (MsgBox("Switch from English to Japanese?", _ MsgBoxStyle.Question Or MsgBoxStyle.YesNo) = _ MsgBoxResult.Yes) Then My.Application.ChangeUICulture("ja-JP") End If End Sub
And sure enough, running the program and saying "Yes" to the "Switch to Japanese" prompt presents a form in Japanese, as shown in Figure 18-7. (If you answer "No" to the question, the default language, English, appears.)
Figure 18-7. Look Ma, I'm a foreigner
Let's look at the files created in this project. (Look in the installation directory of this book's code for the Foreign Names subdirectory. I've placed a copy of this mirror-text project there for you.) The source code directory includes a Form1.resx file, added by default to all new Windows Forms applications. But there is also a Form1.ja.resx file, the Form1 resource file for the Japanese language. Visual Studio will compile this file into a language-specific resource when it builds the project. At that time, the code's bin\Release subdirectory will contain a further ja subdirectory with a file named ForeignNames.resources.dll. This is the satellite assembly that contains all of the Japanese language resources. If the application had included multiple forms, all of the Japanese resources for all forms would appear in that single DLL file.