Thursday, April 3, 2008

Multi Lingual Web sites using ASP.Net

In one of our projects, we were given a task to create a website that supports multi languages. Majority of the samples were difficult to test and implement. The best one I found so far is in the video found in the ASP.Net website.

This is what I have derived from the video.
  1. This only applies for Framework 2.0 and above (not applicable for Framework 1.1).
  2. We need to override the InitializeCulture in the page class (or codebehind class).
I am focusing on changing the words. So I need to use labels. For each of these labels, I will need to add expression linked to Resources. The ClassKey is used to identify the resource filename and the ResourceKey are the entries in the resource file. The following is the example of the interface in Visual Studio.

The resulting html tag will be as follows:

<asp:Label ID="lblMessage" runat="server"
Text="<%$ Resources:LocalizedText, Msg1 %>"></asp:Label>

For example if the ClassKey is "LocalizedText", then the resx files will be: for French for Malay
LocalizedText.resx for the default.

In the above example the files should all be placed in App_GlobalResources under the application folder (just like there is a bin under the application folder).

As for the resource entry, this is a sample of the resource file entry interface in Visual Studio:

This is a sample of the overridden method:

Protected Overrides Sub InitializeCulture()
Dim lang As String = Request("Language1")

If lang IsNot Nothing Or lang <> "" Then
Thread.CurrentThread.CurrentUICulture = New CultureInfo(lang)
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang)
End If

End Sub

In the above example, I used the dropdownlist similar to the video:

<asp:DropDownList ID="Language1" runat="server" AutoPostBack="True">
<asp:ListItem Value="en-US">English</asp:ListItem>
<asp:ListItem Value="ms-MY">Malay</asp:ListItem>
<asp:ListItem Value="fr-FR">French</asp:ListItem>

However, given the logic, we can also use a url parameter, radio button etc to identify the language.

To deployment, it will be like normal with the additional App_GlobalResources folder to copy.

This was the key thing I was looking for. In majority of the examples I saw, the language is automatically detected based on the browser setting. This will not work if the users are sharing a public computer to access the site.


Samet Kaya said...

Your example is working, but it doesn't work with a masterpage, because the a masterpage doesn't have a method called initializeculture, how can i solve this. I prefer not working with querystrings

Strovek said...

I have not tried master pages. Based on your comment of preferring not to work use querystrings, how will your users select the culture?

If it is based on the IE settings then majority of the articles already do that. The querystrings is just a way to check. The setting could come from a database or even a cookie setting.

All said, let me check on the masterpage and get back to you.

Strovek said...

I have verified that it is not possible to perform this setting on the master page.

However, you can create a class and overwrite the initilizeculture in the class and have your aspx codebehind inherit from that class instead:

Imports Microsoft.VisualBasic
Imports System.Threading
Imports System.Globalization
Imports System.Web

Public Class clsPage
Inherits System.Web.UI.Page

Protected Overrides Sub InitializeCulture()
Dim lang As String = Request("lang")

If lang IsNot Nothing Or lang <> "" Then
Thread.CurrentThread.CurrentUICulture = New CultureInfo(lang)
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang)
End If

End Sub

End Class

Then inherit as follows:

Partial Class Sample2
Inherits clsPage

End Class

This way, you to override the sub in every page in your application. I hope this helps. Let me know.

Ève said...

Hello Strovek,
Nice presentation. However I need to go further. Maybe you have an answer : I'm trying to change the culture of the sitemap's menu control in the master page. but i can't find a way to pass the new value of the currentCulture to the sitemap.
The sitemap is localize following these instruction :
. It works fine. When I change the language of IE, The localization of the sitemap changes and the menu in the master page switches languages. But when the user selects another culture using a click event handlers on the masterpage, the pages are translated but not the menu.
Any ideas ?

Thank you for your attention,


Strovek said...

Off hand I do not have a solution since I have not used sitemap before but I will try it out.

I am curious, how is the sitemap rendered, is it via the master page. This article uses explicit changing of the culture but what you have described is implicit changing of the culture (i.e. depending on the browser culture setting).

You can try not setting enableLocalization="true" on the sitemap and let the page do the culture instead as my previous reply to samet kaya.

If it works, please let me know.

Ève said...

The sitemap is rendered into the masterpage via the asp menu control. The Datasource of the menu control is the default sitmapdatasource which is web.sitemap.
Websitemap is localize with a resource file SiteMapLocazations(.en.resx,.fr.resx,.resx)

Strovek said...

Could you post the code? Then I can modify from them and help you fix the problem. I am not that familiar with Sitemap so don't have an answer off hand.

Anonymous said...

I'd a problem similiar to Eve's one.
When I choosed new language - every element on page became translated after post back, except menu control. I'd solve this problem by setting property "EnableViewState" of menu control to "False".

Strovek said...

Thank you for the feedback. I am sure it will help others facing the same situation :)

Anonymous said...

Hi Strovek,

I did the class with the override and then made the code behind inherit from it, like you said (post on April 15, 2008 1:21 PM), but it didn't work :(

Do you have other solution for this?

Thank you!

Strovek said...

One thing I found is the file naming convention and the location of the resource file is very important.

That might be the source of the problem. I can't be certain without seeing the code.

Zam Boyle said...

Hello !

Thank you for this nice article :)

I've a problem with the App_LocalResources folder. Imagine that you have two different Default.aspx pages in two different folders:

* Default.aspx (in the root folder)
* /Orders/Default.aspx (in the /Orders folder).

How can you localize thoses pages ?

I tried to create two localized Default.aspx.resx files in two different folders in the App_LocalResources folder:

* App_LocalResources/Default.aspx.resx
* App_LocalResources/Orders/Default.aspx.resx

And I've the following error message at the runtime:
"Parser Error

Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

Parser Error Message: The resource file '/App_LocalResources/Default.aspx.resx' cannot be used, as it conflicts with another file with the same name."

How can you localize all the different Default.aspx pages ?

Thanks in advance for your support :)



Strovek said...


Are the /Default.aspx.resx and /Orders/Default.aspx.resx different, if so then convert Orders into an application folder so it will /Orders will be the root for the pages under /Orders folder.

Let me know if it works.