Monday, August 29, 2011

How to localize a website - ASP.NET

There are many different ways of doing localization for the website. This post explains one simple way of localization for the ASP.NET website. By and large, there are 3 steps of doing it...

Step 1. Create ASP.NET resource file

The first step is to create ASP.NET resource file (.resx). There are two types of ASP.NET resource file - Global Resource and Local Resource. Global resource file can be shared by all ASP.NET pages and local resource file only applies to one ASP.NET page. In this post, let's use Global resource file for simplicity. To add resource file, create a special ASP.NET folder called App_GlobalResources and add resource files under the folder.
  1.  (VS 2010) In ASP.NET web project, rightclick and select Add -> Add ASP.NET Folder -> App_GlobalResources. 
  2.  Under App_GlobalResources, add New Item (ex: WebResource1.resx file). This is English (or neutral) resource file.
  3. Under App_GlobalResources, add another New Item whose filename has locale characters such as ko,jp,es, etc. (ex: WebResource1.ko.resx file).
  4. Add the same resource [Name]s to the resource files and just localize resource [Value].

Step 2. Use Resource String in ASPX
Once resource strings are stored in .resx file, simply build the project and then VS will automatically generate strongly type resource class, just like WinForm .resx file. Now, you can use resource string in your .aspx file or .aspx.cs code behnid file.

<asp:label runat="server" text="<%$ Resources:WebResource1,Title %>">

The example above shows that resource class is WebResource1 and resource name is Title. One thing to note is that you have to put the resource expression in ASP.NET server control such as asp:Label or asp:Literal, not in HTML tag. For example, you will get an error if you put resource expression in SPAN html tag.

In code behind, you can set the same way of .NET resource.

   using Resources;
   labelTitle.Text = WebResource1.Title;

Step 3. Set Page to use resource string based on Culture

Now, the last step is to set locale to the web page, so that the page reads the resource string from the specific language. You can set the entire web site to use specific culture by using web.config or set each page by using @page directive or programmatically. Here is an example of doing it in the code - overrides InitializeCulture() method and set 4 Culture properties as follows.

protected override void InitializeCulture()
  if (Session["lang"] != null)
    lang = Session["lang"].ToString();
    Page.Culture = lang;
    Page.UICulture = lang;
    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);
    Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(lang);

If you use Session to store language information, each user can use different language for the same web page. Even if this is not required to implment localization, I found it useful sometimes.

1 comment:

  1. You have a lot's of way to localize your website. Above give a good process to know for a localize website. Localization website isn’t the same thing as translating your entire website.

    Website Localization