Friday, March 24, 2006

ASP.NET 2.0 - Explicit Localization in different Languages

In my previous article on ASP.NET 2.0 localization, I had mentioned how easily you can localize your applications using Automatic Localization feature present in Visual Studio 2005.

In this article let us see how we can explicitly set the localization of a page in different languages.

Let us assume we have a page in four different languages. Instead of getting the localization information from the browser settings, we will now see how we can explicitly get the same page in different languages on clicking four links (French, Spanish, German, English) on the page.

On clicking on each of these links, the page has to get loaded in the respective languages. Let me describe how this can be achieved.

The first step is to override the InitializeCulture method to load set the culture settings. To get the culture that is set by each of the Link buttons, we store the culture information in a Session object (Profile object may also be used). The following is the InitializeCulture method:



protected override void InitializeCulture()
{
// override virtual method InitializeCulture() to check if session contains a language setting

if (Session["PreferredCulture"] != null)
{
string UserCulture = Session["PreferredCulture"].ToString();
if (UserCulture != "")
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(UserCulture);
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(UserCulture);
}
}
else
{
string UserCulture = ConfigurationSettings.AppSettings["ApplicationCulture"].ToString();
System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(UserCulture);
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(UserCulture);
}
}


As you can see, for the first time page loads, the culture of the page is set from the config file settings. Subsequent loads of the page after the click of each link button, we set the culture from the Session object.

Now let us see what code has to come in for each Link button click event. This event must set the selected language culture in the Session object, and then must force re-load of the page. The code is as follows for French Language:

protected void frenchLinkButton_Click(object sender, EventArgs e)
{
string SelectedLanguage = "fr";
Session["PreferredCulture"] = SelectedLanguage;
//Force re-initialization of the page to fire InitializeCulture()
Response.Redirect(Request.Url.LocalPath);
}


The same code has to be re-written for other languages:

For Spanish Language:
protected void spanishLinkButton1_Click(object sender, EventArgs e)
{
string SelectedLanguage = "es";
Session["PreferredCulture"] = SelectedLanguage;
//Force re-initialization of the page to fire InitializeCulture()
Response.Redirect(Request.Url.LocalPath);
}


For German Language:
protected void germanLinkButton_Click(object sender, EventArgs e)
{
string SelectedLanguage = "de";
Session["PreferredCulture"] = SelectedLanguage;
//Force re-initialization of the page to fire InitializeCulture()
Response.Redirect(Request.Url.LocalPath);
}


For English language:

protected void englishLinkButton_Click(object sender, EventArgs e)
{
string SelectedLanguage = "en-US";
Session["PreferredCulture"] = SelectedLanguage;
//Force re-initialization of the page to fire InitializeCulture()
Response.Redirect(Request.Url.LocalPath);
}


Of course, to get the respective language page loaded, you must have the following resx files in containg text in respective languages, in your App_LocalResources folder, assuming your page is Default.aspx:
Default.aspx.es.resx
Default.aspx.fr.resx
Default.aspx.resx
Default.aspx.de.resx

If you have a large application that has many pages that require explicit localization, you can have a Base class that inherits System.Web.UI.Page and put the InitializeCulture method in that Base class. All pages in your application can be made to inherit this Base class.


6 comments:

Sendilkumar said...

Nice article.

NicheProvider said...

This is a very nice article. I have two questions:

1. what if I want to use the same page to dynamically display in different UICulture based on user selection from a dropdown box in the same page? InitializeCulture() is always called before any user event handler gets called. Therefore, it will not work within the same page posting.

2. how about the localization of the dynamic data that is retrieved from the database? Do you have some example on that?

Thanks,

NP
www.nicheprovider.com

NicheProvider said...

Whoops!! Found the answer from MS site.

You can get the posted query parameter value from form collection instead of within event handler code.

That solves my problem.

Thanks,

NicheProvider

Anand said...

Small but really informative.

Great and thanks.

Looking for more on Localization concepts

Swapnil said...

HI,
Do you know how when My Applcaition starts it will start in the culture defined that is when anyone starts my application
in Taiwan it will automatically initialized. the way google works
it would be much helpful
Thanks in advance swapnil

Jems Nichole said...

I admire the valuable information you offer in your articles. I will bookmark your site!!
TechWhirl