我正在开发一个多语种网站,有两种语言,首先是阿拉伯语和英语
我已经在IE中以及在FireFox上将语言设置为阿拉伯语(ar-ae)但它总是让我获得英语版本我无法解决此问题
网站结构
Default.aspx
en/Default.aspx
ar/Default.aspx
问题是,当我将网址用作http://localhost:49831/site/Defualt.aspx
时,如果选择阿拉伯语作为语言,则可以正常工作并将我重定向到阿拉伯语版本ar/Default.aspx
。
但是,当使用网址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是否更好
答案 0 :(得分:7)
我会建议你在处理多语言网站时采取一些不同的方法。首先,您不必使用多个默认页面,只需要一个。
让我解释一下如何做到这一点。
首先,可以在web.config中设置默认站点全球化:
<system.web>
<globalization uiCulture="sl-SI" culture="sl-SI" enableClientBasedCulture="false"/>
</system.web>
如果可能,您可以设置enableClientBasedCulture="true"
以强制客户端站点全球化。如果没有,将进行默认文化。
接下来,如何在asp.net页面中设置全球化(强制,当用户需要时):
internal static void ChangeLanguage(string languageCode, System.Web.UI.Page page)
{
CultureInfo ci = new CultureInfo(languageCode);
page.Session[cLanguageSessionKey] = ci;
//save to user profile is required (just a sample)...
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
Entities.MembershipUserFull user = Services.MembershipUserManager.GetMembershipUserFull(HttpContext.Current.User.Identity.Name);
user.LanguageId = Services.LanguageManager.GetLanguageIdFromLanguageCode(languageCode);
//save profile...
user.SaveProfile();
}
//reload page. New culture will take efect...
page.Response.Redirect(page.Request.Url.AbsoluteUri);
}
这是Page OnLoad:
//set language if required...
if (User.Identity.IsAuthenticated)
{
Entities.MembershipUserFull user = Services.MembershipUserManager.GetMembershipUserFull(HttpContext.Current.User.Identity.Name);
if (user != null && user.LanguageId != null)
{
GP.Solutions.Entities.Language language = Services.LanguageManager.GetLanguage((int)user.LanguageId);
if (Thread.CurrentThread.CurrentUICulture.Name != language.Code)
{
ChangeLanguage(language.Code, this);
}
}
}
最后,如何在Web控件上处理多语言支持:
注意我有两个全局资源(每个支持的语言一个。没有语言代码的那个是默认的)
假设我需要在Label上显示一些文字:
<asp:Label ID="Label1" runat="server" CssClass="AdvertPreTitle" Text='<% $resources:AppResource,MostPopularService %>'></asp:Label>
请注意,根据当前的UI Culture,从特定语言资源中读取MostPopularService。这是来自资源文件的snipp:
所以最后我只有一个处理所有语言的默认页面。
答案 1 :(得分:1)
根据此MSDN链接,enableClientBasedCulture="true"
不再有效。相反,请执行以下操作:
<system.web>
<globalization culture="auto" uiCulture="auto"/>
</system.web>