以前,我认为这段代码没有问题,而且它总是正确地反映了用户在 IE9 中设置的语言偏好:
@using (Html.BeginForm("Edit", "Revision", FormMethod.Post, new { id = "RevisionMain" }))
{
@Html.Hidden("UICultureLanguage", CultureInfo.CurrentUICulture.Name)
...
}
我最近添加了这段代码,以前也为我工作。然而,在上一次这似乎工作和它崩溃之间,我的公司唯一做的就是升级到 Telerik Kendo;我看不出这会如何影响这一点,但问题是上面的代码和下面的代码不再正确反映用户在 IE9 中选择的语言设置......
我的视图中有一个代码块使用 CultureInfo.CurrentUICulture.Name,但无论我将 IE9 语言首选项设置为什么,它似乎总是设置为 en-US。注意:此代码嵌入在@section 中:
**@section JQueryScripts** {
<script src="@Url.Content("~/Scripts/jquery-1.7.2.min.js")" type="text/javascript"> </script>
<script src="@Url.Content("~/Scripts/jquery-ui-1.8.11.min.js")" type="text/javascript"> </script>
<script src="@Url.Content("~/Scripts/jquery.validate.1.9.js")" type="text/javascript"> </script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"> </script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"> </script>
@* Language support for numerics that use ',' instead of '.' for decimal separator *@
<script src="@Url.Content("~/Scripts/jquery.validate.globalized.number-mvc4.js")" type="text/javascript"> </script>
@* Language support for jQuery datepicker *@
@{
var currentUiCultureName = CultureInfo.CurrentUICulture.Name;
var scriptFile = @Url.Content("~/Scripts/jquery/ui/1.8.1/i18n/jquery.ui.datepicker-" + currentUiCultureName + ".js");
// var scriptFile = @Url.Content("~/Scripts/jquery/ui/1.8.1/i18n-minified/jquery.ui.datepicker-" + currentUiCultureName + ".min.js");
// Note: If we have a language other than english (en/en-US), include the appropriate .js file to
// support the jquery datepicker. Nothing needed to include for english because A) english is
// the default language supported by the jquery datepicker and B) there is no physical .js file for
// english because it is not needed (see A).
//
// Attention: If a language is not supported, let the javascript error come up, so we know that the
// language needs to be supported.
//
if (!currentUiCultureName.Equals("en", StringComparison.OrdinalIgnoreCase) &&
!currentUiCultureName.Equals("en-US", StringComparison.OrdinalIgnoreCase))
{
<script type="text/javascript" src="@scriptFile"> </script>
}
}
}
我的 web.config 目前在全球化中有以下设置:
<globalization enableClientBasedCulture="true" uiCulture="auto" culture="auto" requestEncoding="utf-8" responseEncoding="utf-8" fileEncoding="utf-8" />
我还在控制器中将 CultureInfo.CurrentCulture 设置为 CultureInfo.CurrentUICulture,以便我的模型正确绑定日期和数值:
protected override void OnAuthorization(AuthorizationContext filterContext)
{
base.OnAuthorization(filterContext);
// For model binding.
Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture;
}
非常需要注意:由于某种原因,如果我在 OnAuthorization 和 jQueryScripts @section 中的代码块中设置断点,OnAuthorization 会被命中 3 次(我确实有一些可能导致此问题的 ajax 调用($(document) .ready(...),但必须验证,以前从未注意到这一点) - 但 Thread.CurrentThread.CurrentUICulture 似乎只在最后一次调用中正确反映了用户的语言偏好,以及我在 jQueryScripts 中放置的断点@查询 CultureInfo.CurrentUICulture 的部分在第一次调用 OnAuthorize 后被命中。我知道 OnAuthorize 没有设置 CurrentUICulture,但只是指出似乎用户的语言设置在控制器中被识别,但在观点。非常令人困惑……有什么想法吗?