单击按钮后更改文化信息
我有多语言 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 = "Culture: " + System.Globalization.CultureInfo.CurrentCulture.ToString();
问题是,当我运行这个应用程序并单击例如英语按钮(默认语言是波兰语)时,没有任何效果。如果我第二次单击它或按 F5,则会应用更改,并且在标签中文化将显示为 en-US。 如果我想将语言更改回波兰语,则会发生同样的情况(第二次单击(或单击并刷新)后即可工作)。
我做错了什么?
i have multilingual asp.net site.
there is a masterpage and default.aspx.
I put two buttons in masterpage - one to click when i want to change the language to english, second for polish. Of course, I want to change the language after click on these buttons (and all changes should appear automatically on the page)
here is a code for both:
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);
}
in default.aspx.cs i have 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 added resource files and in one label i show actual culture:
Welcome.Text = "Culture: " + System.Globalization.CultureInfo.CurrentCulture.ToString();
the problem is that when i run this app and click e.g. english button (default language is polish), there is no effect. if i click it second time or press F5, the changes are applied and in the label the Culture is displayed as en-US.
Tsame happens if i want to change language back to polish (it works after second click (or one click and refresh)).
What am i doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我怀疑问题可能出在您在按钮单击事件中使用 Server.Transfer 。您是否尝试过使用 Response.Redirect 来代替?
I suspect the problem might be in your use of Server.Transfer in your button click event. Have you tried using Response.Redirect instead?
当您使用 Server.Transfer 时,所有事情都发生在同一个 Web 请求中 - 它只是一个内部服务器处理重定向。
尝试使用 Response.Redirect 而不是 Server.Transfer。这将迫使客户端浏览器调用另一个请求,然后 cookie 将被发送到 Web 服务器。
When you use Server.Transfer there everything happens within the same web request - it is just an internal server processing redirect.
Try to use Response.Redirect instead of Server.Transfer. This will force the client browser to invoke another request and then the cookie will be sent to the web server.
您为什么使用 Cookie?为什么不使用会话?我认为 Session 更方便,你不会有这个问题。当InitializeCulture()在Server.Transfer之后启动时,它仍然是相同的请求,但没有英文Cookie。你把它放在 Response 中,但 Response 仍然在这里,在服务器端。
关于您的国际化解决方案。通常建议将区域设置嵌入到 URL 中。假设一位英语用户点击了英语按钮,然后将链接发送给朋友。他的朋友打开链接,却一个字也听不懂。他没有英语 Cookie,因此他正在查看波兰语页面。
希望这有帮助
Why are you using Cookies? Why not Session? I think Session is more convenient and you wouldn't have that problem. When InitializeCulture() starts after Server.Transfer it's still the same request still without English Cookie. You put it in Response but Response is still here, on the server side.
Regarding your internationalization solution. It is usually advised to have locale embedded in URL. Consider an English user who clicked on the English button and then sends a link to a friend. His friend opens the link and can't understand a word. He doesn't have the English Cookie, so he is viewing Polish page.
Hope this helps
使用
代替
Use
in place of