我正在开发一个有两种语言的多语言网站,从阿拉伯语和英语开始
我已在 IE 和 FireFox 中将语言设置为阿拉伯语(ar-ae),但它总是给我英文版本,我无法解决此问题
网站结构
Default.aspx
en/Default.aspx
ar/Default.aspx
问题是,当我使用 URL 时,如果选择阿拉伯语作为语言http://localhost:49831/site/Defualt.aspx
,它可以正常工作并将我重定向到阿拉伯语版本。ar/Default.aspx
但是当使用 URL 时,http://localhost:49831/site/
它总是将我重定向到英文版本 en/Default.aspx
我不确定我做错了什么。
我只使用 Default.aspx 来检测默认浏览器语言,然后相应地重定向
Default.aspx.cs 文件的代码
// 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;
}
我怎样才能最好地定义此代码以使其正常工作。在 Global.asx 文件中定义 CurrentUICulture 是否更好