Tuesday, December 13, 2005

.NET Localization

[ C#, .NET 1.1, 2.0 ]

We can do this by using resource files. A default resource file is generated for every form and text, images etc will be stored in this file and they are complied in to a satellite assembly. Custom text which you need to display as messages etc can be embedded in to custom resource file. You have to create this file manually.

To create multi language form first you have to set the Localize property to true;

Then you can add controls to the form. These controls will be added in the default language. Then you can change the language property to the language that you want and enter text in the form. This will result creating two resource file for the Form by the designer with there culture-name. At the compile time multiple assemblies are created for each culture. When you change the current culture form the Regional Settings the form will load with the specific language text that you added to the form.

Custom resource files will act as the same way, but you have to manually name them accordingly. Ex. Messages.resx , Meesages.fr-FR.resx

You have to load them in the following way

private void button1_Click(object sender, EventArgs e)

{

ResourceManager localResMgr = new ResourceManager("GraphicsTest.messages", typeof(TestWinControls).Assembly);

string welcomeMsg = localResMgr.GetString("strWelcomeMessage");

MessageBox.Show(welcomeMsg);

}

To check this you have to manually change the UI Culture of the form. You have to change the current threads’ UI culture in the Constructor of the Form before the Initializecomponent method is called.


public TestWinControls()

{

System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fr-FR");

InitializeComponent();

}

Note: When you deploy the application the OS should be Win2k , Win3K or WinXP Multi-Language version and you should be able to change the UI Culture from the control panel.

No comments: