看到网上全是vb.net写的实现多语言界面,于是用C#写个 ^_^
我只写详细代码,如果想了解详细解释,请参照他人相关文章.
1、Global.asax
//默认语言
private string[] SupportedLanguages = new string[]{\”en\”,\”zh\”};
private const string LANGUAGE_COOKIE_NAME = \”UserLanguage\”;
void Application_AcquireRequestState(object sender, EventArgs e)
{
HttpCookie languageCookie = this.Request.Cookies[LANGUAGE_COOKIE_NAME];
string language = string.Empty;
if(languageCookie==null)
{
string[] userLanguages = this.Request.UserLanguages;
int index = 0;
for (int i = 0; i++; i < userLanguages.Length – 1)
{
index = Array.IndexOf(this.SupportedLanguages,userLanguages[i]);
if (index >= 0)
{
language = this.SupportedLanguages[index];
break;
}
}
if (language == string.Empty)
{
language = this.SupportedLanguages[0];
}
Response.Cookies.Add(new HttpCookie(LANGUAGE_COOKIE_NAME,language));
}
else
{
language = languageCookie.Value;
}
System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo(language);
System.Threading.Thread.CurrentThread.CurrentUICulture = culture;
}
2、.aspx.cs(请在前台创建控件)
protected void Button1_Click(object sender, EventArgs e)
{
this.SetLanguage(\”zh\”);
}
protected void Button2_Click(object sender, EventArgs e)
{
this.SetLanguage(\”en\”);
}
/// <summary>
/// 手动设置语言
/// </summary>
/// <param name=\”language\”></param>
protected void SetLanguage(string language)
{
HttpCookie myCook = new HttpCookie(\”UserLanguage\”);
myCook.Value = language;
this.Response.Cookies.Add(myCook);
System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo(language);
System.Threading.Thread.CurrentThread.CurrentUICulture = culture;
Server.Transfer(Request.Path);
}
好了,写完了,有什么不当之处,请指正.^_^
- 点赞
- 收藏
- 分享
- 文章举报
fantasy21s发布了1 篇原创文章 · 获赞 0 · 访问量 857私信关注