Tuesday, July 17, 2007

.NET Localization

Here is code to offer a dropdownlist so visitors can change the language.

– Language –
English
中文版本 - Chinese
Ceština - Czech
Francois - French
Deutsch - German
Italiono - Italian
Español - Spanish
Português - Portuguese

protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
{
//store the selected language in a cookie
}
protected override void InitializeCulture()
{
string lang = string.Empty;
HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies[“MyAPP”];

if (cookie != null)
{
try
{
lang = cookie[“Language”];
}
catch
{ }
}

if ((lang != null) && (lang != string.Empty))
{
String selectedLanguage = lang;

if ((selectedLanguage != null) && (selectedLanguage != “”))
{
UICulture = selectedLanguage;
Culture = selectedLanguage;

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