使用母版页在 ASP.NET 应用程序上切换语言
我正在使用 Visual Studio 2008 和 ASP.NET 构建 Web 应用程序。我的大多数网页都基于单个母版页。该母版页包含三个充当语言开关的按钮链接; Click
事件处理程序如下所示:
protected void lbuLangEN_Click(object sender, EventArgs e)
{
this.SwitchLanguage(string.Empty);
}
protected void lbuLangES_Click(object sender, EventArgs e)
{
this.SwitchLanguage("es");
}
然后我有我的私有方法 SwitchLanguage
:
private void SwitchLanguage(string culture)
{
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(culture);
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture);
}
据我了解,这应该足以使基于我的母版页的页面显示本地化行为,即根据文化从适当的本地 resx
文件获取资源。但是,它不起作用。它们始终以西班牙语显示,这是我的浏览器的默认语言。我在 SwitchLanguage 的入口点和出口点设置了一些跟踪消息,显然当前线程没有更改其文化信息:每次调用 SwitchLanguage 时,当前线程的文化是“es-ES”,无论我的代码刚刚设置了什么。
我的代码或我采用的方法有问题吗?谢谢。
I am using Visual Studio 2008 and ASP.NET to build a web app. Most of my web pages are based on a single master page. This master page contains three button links that act as language switches; the Click
event handlers look like this:
protected void lbuLangEN_Click(object sender, EventArgs e)
{
this.SwitchLanguage(string.Empty);
}
protected void lbuLangES_Click(object sender, EventArgs e)
{
this.SwitchLanguage("es");
}
Then I have my private method SwitchLanguage
:
private void SwitchLanguage(string culture)
{
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(culture);
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture);
}
As far as I understand, this should be enough to make the pages based on my master page exhibit a localised behaviour, i.e. get their resources from the appropriate local resx
file according to culture. However, it is not working. They always appear in Spanish, which is my browser's default language. I have set some trace messages at the entry and exit points of SwitchLanguage
, and apparently the current thread is not changing its culture info: every time SwitchLanguage
is called, the current thread's culture is "es-ES", regardless of what my code has just set.
Is there any problem with my code or with the approach I am taking? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在 Global.asax 中使用 OnAcquireRequestState 来更改区域性。它在会话加载后、任何母版页事件之前立即调用。
因此,在按钮事件处理程序中,您将会话变量设置为所需的当前区域性。然后您重定向到当前页面。然后,
Global.asax
事件可以在母版页加载之前获取新语言:You could use
OnAcquireRequestState
inGlobal.asax
to change the culture. It is called right after the session is loaded, but before any master page event.So in the button event handler, you set a session variable to the desired current culture. Then you redirect to the current page. The
Global.asax
event can then pick up the new language, right before the masterpage loads:对于使用@Andomar 解决方案的任何主体,该方法应命名为Application_AcquireRequestState,位于Global.ascx 代码中(例如在Application_Start 下方),并且不需要附加任何事件处理程序。 (如果您执行除此之外的任何操作,即使方法主体中没有代码,您也可能会遇到 NullReferenceException!或者您的方法根本不会运行)
所以代码将是这样的:
For any body using @Andomar solution, the method should be named Application_AcquireRequestState positioned in Global.ascx code (e.g. below the Application_Start), and no event handlers are required to be attached. (If you do anything other than this you may encounter NullReferenceException even with no code in the method body! or your method will not run at all)
So the code will be something like this:
重写页面级别的 InitializeCulture 方法来设置 UICulture
override the InitializeCulture method on page level to set the UICulture