ASP.NET MVC:什么时候设置Thread.CurrentThread.CurrentUICulture?

我刚刚开始本地化一个ASP.Net MVC应用程序。大多数字符串将在资源文件中定义并通过 Matt’s Localization Helpers检索。其他字符串必须存储在数据库中。

我的问题:
我应该在请求管道中早期设置CurrentUICulture,并在整个应用程序中使用它,或者直接使用Request.UserLanguages [0]?

现在我想我应该在Application_BeginRequest中设置CurrentUICulture。实现将如下所示:

protected void Application_BeginRequest(object sender,EventArgs e)
{
    var cultureName = HttpContext.Current.Request.UserLanguages[0];
    Thread.CurrentThread.CurrentUICulture = new CultureInfo(cultureName);
}

这是设置CurrentUICulture的最好的地方,是Request.UserLanguages [0]最好的地方得到这个信息吗?

更新:

Ariel’s post让我意识到,这可以定义没有代码,使用web.config

<system.web>
  <!--If enableClientBasedCulture is true,ASP.NET can set the UI culture and culture for a Web page automatically,based on the values that are sent by a browser.-->
  <globalization enableClientBasedCulture="true" culture="auto:en-US" uiCulture="auto:en"/>

解决方法

这里是使用HttpModule的示例:

http://weblogs.manas.com.ar/smedina/2008/12/17/internationalization-in-aspnet-mvc/

其他选项,创建一个基本的Controller类并在那里实现本地化逻辑。或者使用动作过滤器属性,但是你必须记住将它添加到每个控制器或者结合这个方法与基本的Controller类。

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...