asp.net-mvc ASP.NET MVC:何时设置 Thread.CurrentThread.CurrentUICulture?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1633980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC: When to set Thread.CurrentThread.CurrentUICulture?
提问by Robert Claypool
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.
我刚刚开始本地化 ASP.NET MVC 应用程序。大多数字符串将在资源文件中定义并通过Matt 的 Localization Helpers检索。其他字符串必须存储在数据库中。
My Question:Should I set CurrentUICultureearly in the request pipeline and use that throughout the application, or directly use Request.UserLanguages[0]whenever needed?
我的问题:我应该CurrentUICulture在请求管道的早期设置并在整个应用程序中使用它,还是Request.UserLanguages[0]在需要时直接使用?
Right now I'm thinking that I should set CurrentUICulturein Application_BeginRequest. The implementation would look something like this:
现在我想我应该CurrentUICulture在 Application_BeginRequest 中设置。实现看起来像这样:
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 CurrentUICultureand is Request.UserLanguages[0]the best place to get that info?
这是设置的最佳场所CurrentUICulture,是Request.UserLanguages[0]获得这些信息的最佳地点?
Update:
更新:
Ariel'spost shows this can be defined without code, using web.config
Ariel 的帖子显示这可以在没有代码的情况下定义,使用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"/>
采纳答案by Ariel Popovsky
Here is a sample using an HttpModule:
这是使用 HttpModule 的示例:
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/
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.
其他选项,创建一个基本的 Controller 类并在那里实现本地化逻辑。或者使用动作过滤器属性,但您必须记住将其添加到每个控制器上,或者将此方法与基本 Controller 类结合使用。
回答by Mathias F
Request.UserLanguages[0] can only be a hint what language the users wishes to see. Most users dont know where to change the browser language.
Request.UserLanguages[0] 只能提示用户希望看到的语言。大多数用户不知道在哪里更改浏览器语言。
Another point: Dont be sure that Request.UserLanguages[0] is a valid language. It can even be null. (Not sure what bots have there)
另一点:不要确定 Request.UserLanguages[0] 是一种有效的语言。它甚至可以为空。(不确定那里有什么机器人)
You usually have a Language chooser on the page. Once a user has selected a language there, it is stored in a cookie, session or url. I like to use url because I think it looks pretty.
您通常在页面上有一个语言选择器。一旦用户在那里选择了一种语言,它就会存储在 cookie、会话或 url 中。我喜欢使用 url,因为我认为它看起来很漂亮。
If a user sees your page without having set a language on your page, you should check if Request.UserLanguages[0] is a language you support and set Thread.CurrentThread.CurrentUICulture.
如果用户在没有在页面上设置语言的情况下看到您的页面,您应该检查 Request.UserLanguages[0] 是否是您支持的语言并设置 Thread.CurrentThread.CurrentUICulture。
I use a filter to set Thread.CurrentThread.CurrentUICulture. Thats ok as long as no other filter is using Thread.CurrentThread.CurrentUICulture. Otherwise you would need to set the right execution order for filters.
我使用过滤器来设置 Thread.CurrentThread.CurrentUICulture。只要没有其他过滤器使用 Thread.CurrentThread.CurrentUICulture,就可以。否则,您需要为过滤器设置正确的执行顺序。
I also use Matts helper and it worked very well so far.
我也使用 Matts 助手,到目前为止效果很好。

