使用 imagebutton 更改多语言网站中的语言需要 2 次回发
我已经成功实现了一个多语言网站,用户可以通过单击其中一个图像按钮来更改语言。 我使用 Hiddenfield 来存储用户选择的语言代码(在 Imagebutton 单击事件上)。 我的 InitializeCulture 方法如下所示:
protected override void InitializeCulture()
{
string culture = "Auto";
string selectedValue = Request.Form["ctl00$HiddenFieldLang"];
switch (selectedValue)
{
case "1": culture = "Auto";
break;
case "2": culture = "zh-HK";
break;
default: break;
}
if (culture != "Auto")
{
System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo(culture);
//ci = System.Globalization.CultureInfo.CreateSpecificCulture(culture);
System.Threading.Thread.CurrentThread.CurrentCulture = ci;
System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
}
base.InitializeCulture();
}
这工作正常。但是需要2次回发才能改变页面的语言。我猜测,在一次回发(不会改变任何内容)中,隐藏字段的值已设置。在第二次回发中,进行实际翻译。
我如何实现它,以便只需单击图像按钮即可翻译页面?
谢谢!!
I have successfully implemented a multi-language website in which user can change language by clicking one of the imagebuttons.
I used a Hiddenfield to store code of language that was selected by the user (on Imagebutton click event).
My InitializeCulture method looks like this:
protected override void InitializeCulture()
{
string culture = "Auto";
string selectedValue = Request.Form["ctl00$HiddenFieldLang"];
switch (selectedValue)
{
case "1": culture = "Auto";
break;
case "2": culture = "zh-HK";
break;
default: break;
}
if (culture != "Auto")
{
System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo(culture);
//ci = System.Globalization.CultureInfo.CreateSpecificCulture(culture);
System.Threading.Thread.CurrentThread.CurrentCulture = ci;
System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
}
base.InitializeCulture();
}
This works fine. But it takes 2 postbacks to change the language of the page. I'm guessing, in one postback (which changes nothing), the hiddenfield's value is set. In second postback, actual translation takes place.
How do I implement it so that the page is translated in just one click of imagebutton?
Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以为图像按钮的单击事件添加客户端处理程序,并在其上的 HiddenFieldLang 上设置值。
或者,您可以获得一个在 InitializeCulture 方法中引起回发的控件,如果它是用于切换语言的按钮之一,则应用适当的区域性。
我认为第一个选项是首选,因为您不需要每次都检查哪个控件导致回发。
You may add client-side handler for imagebutton's click event and set value on the HiddenFieldLang on it.
Or you can get an control that caused a postback in InitializeCulture method and if it one of buttons used for switching a language then apply appropriate culture.
The first option in my opinion is preferred as you don't need check which control cause postback each time.
我已经在 ASP.NET 中实现了多语言站点,并且曾经遇到过一些问题...您可以在 问题以及我自己的答案中有关超链接的问题解决方案。在这里我认为你必须找到 Image 控件而不是 HiddenField 控件
I have implemented the Multi-Language site in ASP.NET and had some issue once... you can find the required code in the Question with an problem's solution regarding Hyperlink in the answer by me itself. And here I think you have to find the Image control and not the HiddenField control
谢谢大家的回复。
我设法在 此链接。
但我现在有另一个问题。
我有一个母版页,我将 Hiddenfield 和 Javascript 函数放在我的母版页中设置隐藏字段的值。在我所有页面的 URL 中,我有一个带有语言 ID 的查询字符串。因此,每当语言发生变化时,我也想更改查询字符串。
有什么方法可以从母版页获取当前内容页面的 URL 并从 SetHiddenValue(val) 函数本身执行 Response.Redirect 或 Server.Transfer 并确保文本已翻译?
Thank you all for the replies.
I managed to solve this problem with the help of this link.
But I have another issue now.
I have a masterpage and I put the Hiddenfield and the Javascript function to set the value of hiddenfield in my masterpage. In the URL of all my pages, I have a querystring with the language id. So, whenever the language changes, I want to change the querystring as well.
Is there any way I can fetch the URL of current content page from the masterpage and do a Response.Redirect or Server.Transfer from SetHiddenValue(val) function itself and ensure that the text is translated?