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.