Mar 24 2011

Localization / Multilingual Support Using Resource Files In .net

Category: .net,ASP.netJebarson Jebamony @ 7:52 pm

Download Demo Localization.zip (258.06 kb)

Localization for a multilingual application is every developer’s nightmare. With .net it is not a tough programming as most of the algorithm is already available. Well in this article we will discuss on how we can achieve localization using resource files.

In our sample attached, I have added a resource file “DemoResource.resx” which has the keys and the corresponding values. Remember this resource file is my default language of the application which in my case considered to be en-US. Now to add localization support of other languages all I have to do is to add resource files with the same name but suffixed with the culture i.e., for french, I have added a resource file “DemoResource.fr-FR.resx”; well this actually picks this resource file for french speaking locales from France, however if I am adding a file named “DemoResource.fr.resx”, then this file will be used for all french speaking locales apart from the ones who are not from France.

Localization will be always picked from the thread UI culture which can be set both programmatically and from the user’s computer. As a thumb rule, you should be always adhering to the culture set on the user’s machine and you should also provide a mean for the user to change the culture through the application. The culture on the user machine is normally set while OS installation and can be changed any time. Refer to the “Region And Language” of your machine for the same. Our demo application uses the default culture set on the machine while start up and also demonstrates how to change the culture for the application programmatically.

Since the localization is picked from the UI culture, you have to do nothing other than writing your resource files and assigning the apt resource values to apt controls. We will see how we can do it for a Windows Application and a Web Application.

For Windows Application

this.WelcomeText.Text = Resources.DemoResource.Welcome;

The above statement will assign the resource value specified for “Welcome” based on the culture set.

System.Threading.Thread.CurrentThread.CurrentUICulture

To programmatically change the culture, setting the culture to the above property will do.

For Web Application

For any web application the resource files has to be put into “App_GlobalResources” or “App_LocalResources” depending on the scope. There are two ways by which you can set the resource value to a asp.net control.

this.WelcomeText.Text = Resources.DemoResource.Welcome;

The above is same as you do with any windows application but it is done on a runtime.

The above allows you to bind the resource value on a design time. But remember that you will need to set the culture (if you want to change the culture on runtime) by overriding “InitializeCulture” method which occurs even before the “Page Init” event.

        protected override void InitializeCulture()
        {
            if (this.CustomCulture != null)
            {
                this.UICulture = this.CustomCulture;
            }

            base.InitializeCulture();
        }

The above depicts how this has to be done. The same is demonstrated in the demo attached.

Do remember that you need not write even a singleline of code for the sake of localization other than setting the culture at the runtime; if you want to support that feature. Localization can be done by different ways but resource files has its own advantages like being faster and the ability to keep it out of assembly so that it can be modified without recompiling / deploying the application again. Definitely it has its own disadvantages compared to having localization in a database with respect to the flexibility and more. But with resource files, a small to mid size application can happily run without the need to have a localization framework.

Lastly, if you are developing a multilingual application don’t forget to read the article from MSDN which talks about the best practices to have an application world ready: Best Practices for Developing World-Ready Applications

I would encourage you to download the sample and have a look to understand better before implementing the concepts in your application.

Download Demo Localization.zip (258.06 kb)

One Response to “Localization / Multilingual Support Using Resource Files In .net”

  1. Microsoft Programmers » Localization / Multilingual Support Using … « GILTCareers.com Blog says:

    [...] post: Microsoft Programmers » Localization / Multilingual Support Using … Share [...]

Leave a Reply