Saturday, January 16, 2010

Multi Language Support


Use Global Resources for your page text and controls based on the user’s language. 


Basics
  1. A Resource file(.resx) stores values.
  2. Manually indicate that controls should use resources for their property value
  3. At run-time, the controls property values are derived from the resource file.

Steps

  1. In Solution Explorer, right click the root of your web site , Click Add ASP.NET Folder and then click App_GlobalResources..
             




  1. Right-Click the App_GlobalResources folder, and then click Add New Item
  2. Choose Resource templates , Name box , type MultiLanguage.resx and click Add.
  3. Open the MultiLanguage.resx file
  4. First row under the Name column type Login  and Value Column type Login
       6 . Create for resource file for Chinese and Malay language in the name of  MultiLanguage.zh-SG.resx  and MultiLanguage.ms-MY.resx
       7. Enter the all the corresponding values.







Now you have created three values for the resource. At runtime, ASP.Net will read the value based on what language user selected.

     8. Add Label Control to the page


1.    Default.aspx and switch to design mode, drag label control into the page
2.    Right-Click the Label control , click properties and then click the ellipsis(..)button in the expression box.The Expressions dialog box appears
3. In the Bindable Properties list, click Text.
4. In the Expression Type Lost, Select Resources.
5. Expression Properties, Set ClassKey to MultiLanguage and ResourceKey to Username , click Ok.
6. Assign for all Control.




Chinese Link Button Event



Session["Language"] = "zh-SG";
Response.Redirect("Default.aspx");



Malay Link Button Event



  Session["Language"] = "ms-MY";
  Response.Redirect("Default.aspx");



Add the below function in every page



protected override void InitializeCulture()
    {
String str = (String)Session["Language"];
        if (str != null)
        {
            String selectedLanguage = str;
            UICulture = selectedLanguage;
            Culture = selectedLanguage;

            Thread.CurrentThread.CurrentCulture =
                CultureInfo.CreateSpecificCulture(selectedLanguage);
            Thread.CurrentThread.CurrentUICulture = new
                CultureInfo(selectedLanguage);
        }
        else
        {
            base.InitializeCulture();
        }

}




Write the InitializeCulture function in separate file and call that file.

MultiLanguage.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Globalization;
using System.Threading;

///
/// Summary description for MultiLanguage
///
public class MultiLanguage : System.Web.UI.Page
{
      public MultiLanguage()
      {
            //
            // TODO: Add constructor logic here
            //
      }
  
    
    public void fnSupportMultiLanguage()
    {
        String str = (String)Session["Language"];
        if (str != null)
        {
            String selectedLanguage = str;
            UICulture = selectedLanguage;
            Culture = selectedLanguage;

            Thread.CurrentThread.CurrentCulture =
                CultureInfo.CreateSpecificCulture(selectedLanguage);
            Thread.CurrentThread.CurrentUICulture = new
                CultureInfo(selectedLanguage);
        }
        else
        {
            base.InitializeCulture();
        }
     
    }
   
}

In Aspx Page

protected override void InitializeCulture()
    {

        MultiLanguage ml = new MultiLanguage();
        ml.fnSupportMultiLanguage();
    }