Build a Localized Form that Speaks the User's Language, Page 2
Form Sample Output
Figure 1 shows the output of the sample form. When you press either of the buttons, it displays some simple message boxes.
Figure 1: Default Locale (English)
Editing Resource Files
Resource files are simple text-based files that you can edit with a simple editor like "Visual Notepad" (as I often affectionately refer to good old Notepad). However, Visual Studio .NET makes creating resource files through the IDE easy. It also has a utility called winres.exe that allows resources to be edited in a lightweight editor that cannot change code. Because I'm all about taking the path of least resistance, I'll create the resource files through the Visual Studio .NET IDE. Here is the basic recipe for creating a localized form using the form from above:
- Set the Localizable property of the form to true using the properties window.
- The form has a Language property as well. To enter the settings for a specific locale, you set the Language property to the desired locale. (For this example, I set the Language property to German (Germany).) Now, Visual Studio is ready for you to enter your locale-specific settings. Go through each control and change the properties as desired. Here are the changes that I made for the German (Germany) locale:
- Form1.Text = Deutsche Anwendung
- NameLabel.Text = Ich heisse:
- LangQuesLabel.Text = Sprechen sie Deutsche?
- GoBtn.Text = Gehen
- HelpBtn.Text = Helfen
- Rinse and repeat, err, I mean repeat Step 2 for every other locale you wish to set up. As you switch between the locales you have set up, you will see the form automatically change to match your configured settings, like when I changed the locale back to English (United States).
- Now, it's time to compile the form. Once you have compiled, you should see an additional set of folders underneath the bin\Debug directory that contain locale-specific information. The form itself also will have several additional .resx files generated for it.
- Now, test out your application. You could go into the Control Panel and change the locale information on your machine. If you're anything like me, however, you may not be smart enough to get it all put back. A safer way to test is setting the CurrentUICulture property of your application's thread to specify the desired culture at the start of the Main method. For example, I added System.Threading.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("de-DE");.