6.13 Make a Multilingual Form


Problem

You need to create a localizable form that can be deployed in more than one language.

Solution

Store all locale-specific information in resource files, which are compiled into satellite assemblies.

Discussion

The .NET Framework includes built-in support for localization through its use of resource files. The basic idea is to store information that's locale-specific (for example, button text) in a resource file. You can then create multiple resource files for multiple different cultures and compile them into satellite assemblies. When you run the application, .NET will automatically use the correct satellite assembly based on the locale settings of the current computer.

You can read to and write from resource files manually. However, Visual Studio .NET also includes extensive design-time support for localized forms. It works like this:

  1. First set the Localizable property of the form to true using the Properties window.

  2. Set the Language property of the form to the locale for which you would like to enter information. (See Figure 6.6.) Then configure the localizable properties of all the controls on the form. Instead of storing your changes in the designer-generated code for the form, Visual Studio .NET will actually create a new resource file to hold your data.

  3. Repeat step 2 for each language that you want to support. Each time, a new resource file will be generated. If you change the Language property to a locale you have already configured, your previous settings will reappear, and you'll be able to modify them.

    click to expand
    Figure 6.6: Selecting a language for localizing a form.

You can now compile and test your application on differently localized systems. Visual Studio .NET will create a separate directory and satellite assembly for each resource file in the project. You can select Project Show All Files from the Visual Studio .NET menu to see how these files are arranged, as shown in Figure 6.7.

click to expand
Figure 6.7: A French-locale satellite assembly.

As a testing shortcut, you can also force your application to adopt a specific culture by modifying the Thread.CurrentUICulture property of the application thread. However, you must modify this property before the form has loaded.

 using System; using System.Windows.Forms; using System.Threading; using System.Globalization; public class MultiLingualForm : System.Windows.Forms.Form {     private System.Windows.Forms.Label label1;     // (Designer code omitted.)     static void Main() {         Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr");         Application.Run(new MultiLingualForm());     } } 
Note  

You can also use a utility called Winres.exe (included with Visual Studio .NET) to edit resource information. It provides a scaled- down form editor that doesn't include the capability to modify code, which is ideal for translators and other nonprogramming professionals who might need to enter locale-specific information.




C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net