Thread.CurrentThread.CurrentUICulture VS接受语言中的ViewModels [英] Thread.CurrentThread.CurrentUICulture Vs Accept-Language in ViewModels

查看:162
本文介绍了Thread.CurrentThread.CurrentUICulture VS接受语言中的ViewModels的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全球化:之所以问这个问题是有一个更深入的了解 Thread.CurrentThread.CurrentUICulture 有关工作的属性和在的ViewModels和控制器的错误消息

Globalization: The reason for asking this question is to have a better understanding of how Thread.CurrentThread.CurrentUICulture works in relation to properties and error messages in ViewModels and Controllers

我已经安装我的项目,覆盖 OnActionExecuting 我控制器的方法我已经设置基于用户选择存储在cookie中的语言。

I have setup my project and overrides OnActionExecuting method of my controllers I have set the language based on user selection stored in cookie.

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
    base.OnActionExecuting(filterContext);

    HttpCookie cookie = Request.Cookies["lang"];

    string lang = cookie != null ? cookie.Value : "en-US";
    System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);
}

现在,在此之后,检查接受语言在HTTP请求头由浏览器发送的,用户的变化并没有反映 - 价值仍然是EN-US 。这让我觉得这两个是不依赖。

Now, after this, checking the Accept-Language header in http request being sent by the browser, the user changes was not reflected - value is still "en-US". It makes me feel that these two are not dependent.

我也搬到了code到Global.asax中,但它的效果相同。从昨天起,一直在

I have also moved the code to Global.asax but it's the same effect. Been on since yesterday.

Thread.CurrentThread.CurrentUICulture 设置会影响页面上的文本而不是从视图模型注释错误信息或属性。但是,当接受语言从浏览器改变了,那么错误信息从右侧国土资源文件中选取。

The Thread.CurrentThread.CurrentUICulture setting affects texts on page but not error messages or properties from ViewModel annotations. But when the Accept-Language is changed from the browser, then the error messages are picked from the right resourse files.


  • 是否有可能操纵接受语言头? - 这似乎是只读

  • 主要问题的:如何在 Thread.CurrentThread.CurrentUICulture 作品?这似乎是大多数例子/用品/在互联网上使用博客等。

  • Is it possible to manipulate the Accept-Language header? - this seems to be read-only
  • Main Question: how does the Thread.CurrentThread.CurrentUICulture works? It seems to be what most examples/articles/blogs etc. on the internet use.

推荐答案

为了得到正确的全球化上的公开网站ASP.NET MVC中,我提出以下建议:

In order to get globalization right on a public site with ASP.NET MVC, I recommend the following:


  • 使用(片段)的URL来确定文化,不是一个cookie。否则,将无法在pre-定义的语言链接到一个页面,搜索引擎将有麻烦索引您的内容。

  • 浏览器preferences可用于的重定向的对第一次访问适当的语言(例如,调用网页的没有的任何URL片段时:www.mysite .COM重定向到www.mysite.com/en-US/与在浏览器中的en-US preferences)
  • 用户
  • OnActionExecuting 太晚的文化初始化。我建议你​​阅读约本地化亚历克斯Adamyan的文章 ASP.NET MVC,并实施自定义路由处理程序以初始化的UICulture /文化。这也将正确地与Modelbinding工作(例如,使用资源,并且当视图模型类属性)。

  • Use (a fragment of) the URL to determine the culture, not a cookie. Otherwise it will not be possible to link to a page in a pre-defined language, and search engines will have trouble indexing your content.
  • Browser preferences can be used to redirect to the appropriate language on first visit (for example, when calling the homepage without any URL fragment: www.mysite.com redirects to www.mysite.com/en-US/ for users with en-US preferences in the browser)
  • OnActionExecuting is too late for culture initialization. I recommend reading Alex Adamyan's article about localization in ASP.NET MVC, and implement a custom route handler to initialize the UiCulture/Culture. This will also work correctly with Modelbinding (for example, when using resources and attributes on view model classes).

至于你的其他问题:它并没有真正意义,因为客户端发送它们的服务器修改接受语言头。即使你可以修改它们,客户端将永远不会知道。

As to your other questions: It doesn't really make sense to modify the Accept-Language headers since the client sends them to the server. Even if you could modify them, the client would never know about it.

这篇关于Thread.CurrentThread.CurrentUICulture VS接受语言中的ViewModels的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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