【发布时间】:2010-12-10 16:15:06
【问题描述】:
我刚刚开始本地化 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 帖子显示这可以在没有代码的情况下使用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"/>
【问题讨论】:
标签: asp.net-mvc localization globalization