我有多语言的asp.net 网站。有一个母版页和default.aspx。
我在母版页中放了两个按钮 - 当我想将语言更改为英语时单击一个,第二个用于波兰语。当然,我想在点击这些按钮后更改语言(所有更改都应该自动出现在页面上)
这是两者的代码:
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);
}
在 default.aspx.cs 我有 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();
}
我添加了资源文件,并在一个标签中显示了实际文化:
Welcome.Text = "文化:" + System.Globalization.CultureInfo.CurrentCulture.ToString();
问题是当我运行这个应用程序并单击例如英语按钮(默认语言是波兰语)时,没有效果。如果我第二次单击它或按 F5,则会应用更改,并且在标签中文化显示为 en-US。如果我想将语言改回波兰语(它在第二次单击(或单击并刷新)后起作用),也会发生相同的情况。
我究竟做错了什么?