我有多語言的asp.net網站。 有一個masterpage和default.aspx。點擊按鈕後更改CulturalInfo
我在主頁上放了兩個按鈕 - 一個點擊當我想把語言改成英文,第二個打磨。當然,我想點擊這些按鈕後更改語言(和所有的變化應該在頁面上自動顯示)
這裏是兩個代碼:在default.aspx.cs
protected void EnglishButton_Click(object sender, ImageClickEventArgs e)
{
string selectedLanguage = "en-US";
//Sets the cookie that is to be used by InitializeCulture() in content page
HttpCookie cookie = new HttpCookie("CultureInfo");
cookie.Value = selectedLanguage;
Response.Cookies.Add(cookie);
Server.Transfer(Request.Path);
}
protected void PolishButton_Click(object sender, ImageClickEventArgs e)
{
string selectedLanguage = "pl-PL";
//Sets the cookie that is to be used by InitializeCulture() in content page
HttpCookie cookie = new HttpCookie("CultureInfo");
cookie.Value = selectedLanguage;
Response.Cookies.Add(cookie);
Server.Transfer(Request.Path);
}
我有InitializeCulture():
protected override void InitializeCulture()
{
HttpCookie cookie = Request.Cookies["CultureInfo"];
// if there is some value in cookie
if (cookie != null && cookie.Value != null)
{
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture(cookie.Value);
Thread.CurrentThread.CurrentUICulture = new
CultureInfo(cookie.Value);
}
else // if none value has been sent by cookie, set default language
{
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture("pl-PL");
Thread.CurrentThread.CurrentUICulture = new
CultureInfo("pl-PL");
}
base.InitializeCulture();
}
我添加的資源文件,並在一個標籤i顯示了實際的文化:
Welcome.Text = 「文化:」 + System.Globalization.CultureInfo.Curre ntCulture.ToString();
問題是,當我運行這個應用程序,並點擊例如英語按鈕(默認語言是波蘭語),不起作用。如果我第二次單擊它或按F5,則會應用更改,並在文化標籤中顯示爲en-US。 如果我想將語言改回波蘭語(它在第二次點擊(或單擊並刷新)之後起作用),Tsame會發生。
我到底做錯了什麼?
感謝的,它幫助! – Bart 2010-04-04 07:05:54