Subscribe

RSS Feed (xml)

Making Multilingual Form in C#

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 Figure1) 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.

    figure1.JPG
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 Figure2.
figure2.JPG

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());
}
}

No comments:

Post a Comment

Archives

LocalsAdda.com-Variety In Web World

Fun Mail - Fun in the Mail