Click here to Skip to main content
15,882,113 members
Articles / Web Development / XHTML
Article

Creating Multilingual and Localized Pages in .NET

Rate me:
Please Sign up or sign in to vote.
2.17/5 (8 votes)
14 Sep 2008CPOL2 min read 19.3K   18   3
Article describes how to create multilingual, localized pages in .NET

Introduction

Here I will discuss how to develop multilingual and localized pages in .NET web applications. The main problem with the multilingual application is reset the current culture, language each time page is postback because it may reset to the default language/culture. I will also discuss use of resource files for multilingual application and also approaches to localize images.

Description

Let us start this exercise in steps.

  1. Create your own base page class: The problem with multilingual application as I discussed earlier is that it might reset the culture back to the default during page postback. To tackle with this issue .NET provides us a method which is part of page life cycle to initialize culture. So we can write our own class inherited from System.Web.UI.Page class and override its InitializeCulture method as follows:
C#
protected override void InitializeCulture()
{
    //get the culture information from wherever you want suppose it is a property
    //with your user class

    Thread.CurrentThread.CurrentCulture = 
        CultureInfo.CreateSpecificCulture(CurrentLoggedinUser.PreferredCulture);
    Thread.CurrentThread.CurrentUICulture = new 
        CultureInfo(CurrentLoggedinUser.PreferredCulture);

    base.InitializeCulture();
}

Now override each of your aspx page with your own class instead of Page class. And it will reset the current culture as per your requirement.

  1. Using resource file: Resource files are the .resx extension file where you can keep content of your web pages to choose according to the language. You should keep 1 resource file for each language your application support. Now the question is how to pick values from these resource files, no problem, its jus matter of couple of lines of code:
C#
ResourceManager rm =new ResourceManager("Name of resource file",
    Assembly. GetExecutingAssembly());

Label1.Text=rm.GetString("key1");

To avoid few compile time error in above code add following namespace in your code

C#
Using System.Resources;

Using System.Reflection;
  1. Localizing Images: For localizing images you can choose any one of two approaches. First keep of name of all images in the resource file and access image path like this:
C#
imageButton1.ImageUrl= rm.GetString("imagepath1");

Other approach which I prefer is to keep all images of one language in one folder and keep name of that folder in your resource file but remember to keep same name for a image in all language.

C#
imageButton1.ImageUrl= rm.GetString("ImageFolder") +"LoginImage.jpg";

Summary

Here we have discussed about creating localized pages in .NET application. We discussed that how to set culture in each page trip. Then we discussed how to pick values from resource file and the we discusssed approaches for localizing images of your application. Hope it helps. Thanks.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer Nagarro Software Pvt Ltd Gurgaon India.
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralNeed more detail Pin
Donsw20-Jan-09 17:01
Donsw20-Jan-09 17:01 
GeneralMy vote of 2 Pin
Donsw20-Jan-09 17:00
Donsw20-Jan-09 17:00 
GeneralMy vote of 2 Pin
Member 196961510-Dec-08 1:39
Member 196961510-Dec-08 1:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.