I am working on a multilingual website with two languages to start with Arabic and English
I have set language to Arabic (ar-ae) in IE and also on FireFox but it always get me the English version i am not able to troubleshoot this issue
Site Structure
Default.aspx
en/Default.aspx
ar/Default.aspx
Problem is that when i use URL as http://localhost:49831/site/Defualt.aspx
Then it works fine and redirects me to Arabic version ar/Default.aspx
if Arabic is selected as language.
But when is use URL as http://localhost:49831/site/
then it always redirect me to English version en/Default.aspx
I am not sure what i am doing wrong.
I use Default.aspx only to detect the default browser language and then redirect accordingly
CODE for Default.aspx.cs file
// Localization and Globalization code
protected override void InitializeCulture()
{
String lang = Request["Language"];
Session["lang"] = Helper.DetectLanguage(lang);
//Set Direction of page LTR/RTL
if (Session["lang"] == "ar-AE")
{
Session["PageDIR"] = "rtl";
}
else
{
Session["PageDIR"] = "ltr";
}
base.InitializeCulture();
}
public static String DetectLanguage(String lang)
{
String LangCode = lang;
if (!string.IsNullOrEmpty(lang))
{
lang = lang.ToLower();
}
String Lang2Char;
CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentUICulture;
String LangName = ci.Name.ToString().ToLower();
//check leng & if length is less than 2 then set english as default language.
if (LangName.Length > 1)
{
Lang2Char = LangName.Substring(0, 2);
}
else
{
Lang2Char = "en";
}
// if QueryString is not null then execute foollowinf if block
//If Language is present in Querystring then excute if part else, else part
if (lang != null)
{
switch (lang)
{
case "en-us":
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
LangCode = "en-US";
break;
case "ar-ae":
Thread.CurrentThread.CurrentCulture = new CultureInfo("ar-AE");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("ar-AE");
LangCode = "ar-AE";
break;
default:
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
LangCode = "en-US";
break;
}
}
// if lang query string is null then set the language based on following logic
else
{
switch (Lang2Char)
{
case "en":
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
LangCode = "en-US";
break;
case "ar":
Thread.CurrentThread.CurrentCulture = new CultureInfo("ar-AE");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("ar-AE");
LangCode = "ar-AE";
break;
default:
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
LangCode = "en-US";
break;
}
}
return LangCode;
}
How can i best define this code so that it works. Is it better to define CurrentUICulture in Global.asx file