This is a mirror of official site: http://jasper-net.blogspot.com/

Routing a localized ASP.NET MVC application

| Wednesday, April 14, 2010
Localizing a general ASP.NET application is easy to implement. All we have to do is assigning a specified culture info to Thread.CurrentThread.CurrentUICulture and use resource to display the localized text. There are two common ways to do this task:

Manually change CurrentUICulture in Application.BeginRequest event

The implementation will look somthing like this:

   protected void Application_BeginRequest(object sender, EventArgs e)
   {
       var cultureName = HttpContext.Current.Request.UserLanguages[0];
       Thread.CurrentThread.CurrentUICulture = new CultureInfo(cultureName);
   }
       

Let ASP.NET set the UI culture automatically based on the values that are sent by a browser

We get it by changing web.config. The configuration will look like this:

   <system.web>
     <globalization enableClientBasedCulture="true" uiCulture="auto:en"/>
   </system.web>
       

The problem is that culture info is stored in UserLanguages which is defined by browser. If user want to change to another language, he must go to "Internet Options" (IE for example) to change the Language Preference. This technique also has problem for search engines because a search engine can not classify two different language version. It cause our page rank won't be promoted. Generally, we solve this problem by using URL to define culture (we will call it as localized url). So, an url will be change from: http://domain/action/ to http://domain/[culture-code]/action (for example: http://domain/vi-VN/action) Then, in Application.BeginRequest event, we can change UICulture like this:


   protected void Application_BeginRequest(object sender, EventArgs e)
   {
       var cultureName = HttpContext.Current.Request.RawUrl.SubString(1, 5); //RawUrl is "/domain/vi-VN/action"
       Thread.CurrentThread.CurrentUICulture = new CultureInfo(cultureName);
   }
   

Everthing seems to be OK until the born of ASP.NET MVC. Routing technique which is used by ASP.NET MVC framework cause problem for this localization technique because routing engine need to parse an url to create the right controller. Appending a prefix before real URL will make a wrong result for routing engine. So, we need a way to combine both of ASP.NET MVC and localized url.

Read more: Codeproject

Posted via email from jasper22's posterous

0 comments: