ASP.NET MVC:当设置Thread.CurrentThread.CurrentUICulture? [英] ASP.NET MVC: When to set Thread.CurrentThread.CurrentUICulture?

查看:609
本文介绍了ASP.NET MVC:当设置Thread.CurrentThread.CurrentUICulture?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始本地化ASP.Net MVC应用程序。大部分的字符串将在资源文件中被定义,并通过马特的本地化助手。其他字符串必须存储在数据库中。

我的问题:
应如何设置的CurrentUICulture 早在请求管道并使用,在整个申请,或直接使用 Request.UserLanguages​​ [0] 每当需要的?

现在的我在想,我应该设置的CurrentUICulture 中的Application_BeginRequest 。实施将是这个样子:

 保护无效的Application_BeginRequest(对象发件人,EventArgs的发送)
{
    变种cultureName = HttpContext.Current.Request.UserLanguages​​ [0];
    Thread.CurrentThread.CurrentUICulture =新的CultureInfo(cultureName);
}

这是设置的最佳场所的CurrentUICulture Request.UserLanguages​​ [0] 最好的地方获得这些信息?


更新:

Ariel的后让我意识到,这可能没有code来定义,使用的web.config

 <&的System.Web GT;
  <! - 如果enableClientBasedCulture是真实的,ASP.NET可以自动设置为网页的UI文化和文化的基础上,由浏览器发送的值。 - >
  <全球化enableClientBasedCulture =真正的文化=自动:EN-US的UICulture =自动:EN/>


解决方案

下面是使用一个HttpModule的例子:

<一个href=\"http://weblogs.manas.com.ar/smedina/2008/12/17/internationalization-in-aspnet-mvc/\">http://weblogs.manas.com.ar/smedina/2008/12/17/internationalization-in-aspnet-mvc/

其他选项,创建一个基础类,并实施本土化的逻辑在那里。
或者使用动作过滤属性,但你必须记住要添加的每个控制器或合并与基础类这种方法。

I am just beginning to localize an ASP.Net MVC application. Most of the strings will be defined in resource files and retrieved via Matt's Localization Helpers. Other strings must be stored in a database.

My Question: Should I set CurrentUICulture early in the request pipeline and use that throughout the application, or directly use Request.UserLanguages[0] whenever needed?

Right now I'm thinking that I should set CurrentUICulture in Application_BeginRequest. The implementation would look something like this:

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

Is this the best place to set CurrentUICulture and is Request.UserLanguages[0] the best place to get that info?


Update:

Ariel's post made me realize that this can be defined without code, using 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"/>

解决方案

Here is a sample using an HttpModule:

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

Other options, create a base Controller class and implement the localization logic there. Or use an action filter attribute, but you'll have to remember to add it on every controller or combine this approach with the base Controller class.

这篇关于ASP.NET MVC:当设置Thread.CurrentThread.CurrentUICulture?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆