Monday, October 3, 2011

Multilingual Website tutorial

Code in your aspx page
English
runat="server">
العربية

Base class
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.Threading;
using System.Globalization;

///
/// BasePage for the common funtionality in all
/// the web pages of the site.
///
public class BasePage : Page
{
///
/// Default constructor
///
public BasePage()
{
//
// TODO: Add constructor logic here
//
}

///
/// The name of the culture selection dropdown list in the common header.
///
// public const string LanguageDropDownName = "ctl00$cphHeader$Header1$ddlLanguage";

///
/// The name of the PostBack event target field in a posted form. You can use this to see which
/// control triggered a PostBack: Request.Form[PostBackEventTarget] .
///
public const string PostBackEventTarget = "__EVENTTARGET";

///
/// Overriding the InitializeCulture method to set the user selected
/// option in the current thread. Note that this method is called much
/// earlier in the Page lifecycle and we don't have access to any controls
/// in this stage, so have to use Form collection.
///
protected override void InitializeCulture()
{
///
///Check if PostBack occured. Cannot use IsPostBack in this method
///as this property is not set yet.
///
///

if (Request[PostBackEventTarget] != null)
{
string controlID = Request[PostBackEventTarget];
//LinkButton cc= Fn_GetPostBackControl(Page) as LinkButton;

if (controlID != null)
{

//string selectedValue = cc.CommandName;
switch (controlID)
{
case "ctl00$LbArb": SetCulture("ar-SA", "ar-SA");
Session["Language"] = "Arabic";
break;
case "ctl00$LbEng": SetCulture("en-US", "en-US");
Session["Language"] = "English";
break;
default:
Session["Language"] = "English";
break;
}
}
}


///
///Get the culture from the session if the control is tranferred to a
///new page in the same application.
///
if (Session["MyUICulture"] != null && Session["MyCulture"] != null)
{
Thread.CurrentThread.CurrentUICulture = (CultureInfo)Session["MyUICulture"];
Thread.CurrentThread.CurrentCulture = (CultureInfo)Session["MyCulture"];
}
base.InitializeCulture();
}


public Control Fn_GetPostBackControl(Page thePage)
{
Control _ctrl_myControl = null;
string ctrlName = Request.Params.Get("__EVENTTARGET");
if (((ctrlName != null) & (ctrlName != string.Empty)))
{
_ctrl_myControl = thePage.FindControl(ctrlName.Replace("ctl00$", ""));
}
else
{
foreach (string Item in thePage.Request.Form)
{
Control c = thePage.FindControl(Item);
if (((c) is System.Web.UI.WebControls.Button))
{
_ctrl_myControl = c;
}
}

}
return _ctrl_myControl;
}


///
/// Sets the current UICulture and CurrentCulture based on
/// the arguments
///
///
///
protected void SetCulture(string name, string locale)
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo(name);
Thread.CurrentThread.CurrentCulture = new CultureInfo(locale);
///
///Saving the current thread's culture set by the User in the Session
///so that it can be used across the pages in the current application.
///
Session["MyUICulture"] = Thread.CurrentThread.CurrentUICulture;
Session["MyCulture"] = Thread.CurrentThread.CurrentCulture;
}


}


Inherit base class on your aspx.cs like below image


Add global resource file and give proper name is you provide base.cs

2 comments:

Rahul Pal said...

asdas

Multilingual websites said...

One of the most effective ways of reaching out to a culturally diverse target audience is by developing multilingual websites.

Multilingual websites