Introduction
Do you like to support multi-languages to your website? In this article, we discuss a way in which you could manage your site to support multiple languages. We do not say that this piece of writing is the only way of achieving the goal, but this could be a good starting point. The code for this article is written in C# Visual Studio 2010.
Using the Code
protected override void InitializeCulture()
{
string lang = string.Empty;
try
{
lang = Request.Form["DdlLanguages"].ToString();
}
catch (Exception) { }
if (lang != string.Empty)
{
SetCulture(lang, lang);
}
if (Session["MyUICulture"] != null && Session["MyCulture"] != null)
{
Thread.CurrentThread.CurrentUICulture = (CultureInfo)Session["MyUICulture"];
Thread.CurrentThread.CurrentCulture = (CultureInfo)Session["MyCulture"];
}
base.InitializeCulture();
}
...
Here we are overriding the InitializeCulture()
function. This is called on initializing of page in which we inset the current culture as well as user interface culture properties.
protected void SetCulture(string name, string locale)
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo(name);
Thread.CurrentThread.CurrentCulture = new CultureInfo(locale);
Session["MyUICulture"] = Thread.CurrentThread.CurrentUICulture;
Session["MyCulture"] = Thread.CurrentThread.CurrentCulture;
base.InitializeCulture();
}
...
By changing the value of dropdown SetCulture
function is called by the two arguments having almost the same values string name, and string locale, to set the CurrentUICulture
and CurrentCulture
properties by the newly selected language. At the end, save that culture in two different session variables Session["MyUICulture"]
and Session["MyCulture"]
. Finally, call the base function InitializeCulture()
.
Step by Step Guide
- Create an ASP.NET web site in Visual Studio 2010
- Add a page default.aspx
- Add dropdown list box, calendar and label
- Go to design view of your page, in tools menu and click on 'generate local resource' - it will create a file "Default.aspx.resx" in a system directory App_LocalResources
To write in Urdu, follow these steps.
- To write in Urdu, go to control panel, Under Clock Language and Region, click on change keyboards or other input methods
- Click keyboard and language tab
- Click change keyboards
- Click on add button and choose Urdu and click OK
- Click on the tab 'Advanced key settings' and select the language and click change key sequence
- Check the enable key sequence checkbox (remember the shortcut key) and press OK
- To work in Urdu phonetic keyboard, download it from http://urdu-phonetic-keyboard.findmysoft.com/download/
- Install it and follow step# 5-10
- Copy (Default.aspx.resx) and paste it in the same directory and rename it as (Default.aspx.ur-PK.resx)
- Open (Default.aspx.ur-PK.resx), change value of
Label1Resource1.Text
as you like to write in Urdu, press the short cut key which you set in keyboard settings i.e. (CTRL+0)
- Press CTL+F5 to run the project
History
- 29th November, 2011: Initial version