Tuesday, May 19, 2009

How to use Multilingual in ASP.Net

just do the following things in the page, to make the page to multilingual.

1. first add a dropdown list with the languages in item list.

then design the page fully.
finally click tools->Generate Local Resource
The resource page for that page is created.
do as follows.......................
for Example:
if u need to create multilingual for Login.aspx then you will get the resource file named Login.aspx.resx.
copy the resource page and paste each page for every language.
then change the name of the page for each language.
for English->Login.aspx.en.resx
for german->Login.aspx.de.resx
for japanese->Login.aspx.jp.resx.
---------------------------------------
click and open each resx page and give the respective name for each controls with respect to the languages.
then add the following code to the Login.aspx.cs
---------------------------------------

public const string langDropdownName = "ctl00$cphContent$ddlLanguage";
//this is the dropdownlist
public const string PostBackEventTarget = "__EVENTTARGET";

protected override void InitializeCulture()
{
// this will override the InitializeCulture() function
if (Request[PostBackEventTarget] != null)
{
string controlId = Request[PostBackEventTarget];
if (controlId.Equals(langDropdownName))
{
string selectedValue =
Request.Form[Request[PostBackEventTarget]].ToString();
switch (selectedValue)
{
case "0": SetCulture("en-US", "en-US");
break;
case "1": SetCulture("de-DE", "de-DE");
break;
default: 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)
{
System.Threading.Thread.CurrentThread.CurrentUICulture = (System.Globalization.CultureInfo)Session["MyUICulture"];
System.Threading.Thread.CurrentThread.CurrentCulture = (System.Globalization.CultureInfo)Session["MyCulture"];
}
base.InitializeCulture();
}
------------------------------------------------------------------
the setCulture is a method where we change the language which is as follows
---------------------------------------------------------------
protected void SetCulture(string name, string locale)
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(name);
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.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"] = System.Threading.Thread.CurrentThread.CurrentUICulture;
Session["MyCulture"] = System.Threading.Thread.CurrentThread.CurrentCulture;
}
----------------------------------------------------------------------
run the page. surely the language will be changed.
very easy way to use multi lingual concept in asp.net.
we can do this even in master page concept.
keep the ddl and the code in master page and try this in the pages with language resource file to see the change of language.
this is the multi lingual concept in asp.net

No comments:

Post a Comment

HTML/JAVASCRIPT BLOG

You can find the help for html and javascript codes

Any Doubts Contact my Mail-ID:
kannan.mkv@gmail.com