MVC 5-无法运行全球化 [英] MVC 5 - can not get globalisation running
问题描述
我想添加全球化信息,因为该网站要求用户提供日期.而我的德语用户想输入"31.12.1966"而不是"1966-12-31".
I want to add globalization because the site asks the user for a date. And my german user want to type "31.12.1966" and not "1966-12-31".
所以我添加了nuget-Packages "jQuery.Validation.Globalize"和 "jquery-globalize" 进入项目.
So I add the nuget-Packages "jQuery.Validation.Globalize" and "jquery-globalize" to the project.
现在我无法配置BundleConfig!从我的研究中,我知道我需要globalize.js和其他一些文件.所以我试着做个bündle:
Now I am not able to configure my BundleConfig! From my research I know, that I Need globalize.js and some other files. So I try to make a bündle:
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.validate*"));
bundles.Add(new ScriptBundle("~/bundles/global").Include(
"~/Scripts/globalize.js",
"~/Scripts/cldr.js").IncludeDirectory("~/Scripts/cldr/",
"~/Scripts/globalize/")
);
视图中的用法:
...
@section Scripts {
@Scripts.Render("~/bundles/global")
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/unobtrusiveajax")
<script type="text/javascript">
$(function () {
$.validator.methods.date = function (value, element) {
Globalize.culture("de-DE");
// you can alternatively pass the culture to parseDate instead of
// setting the culture above, like so:
// parseDate(value, null, "en-AU")
return this.optional(element) || Globalize.parseDate(value) !== null;
}
});
</script>
}
但是我得到一个错误:
Error at line 9, column 5 in http://localhost:58289/Scripts/jquery.validate.globalize.js
0x800a138f - runtimeerror in JavaScript:
The property "methods" of a undefindes or null-pointer can not bei called
我翻译了这张德语原著中的信息:
I translated the message from this german original:
Ausnahmefehler in Zeile 9, Spalte 5 in http://localhost:58289/Scripts/jquery.validate.globalize.js
0x800a138f - Laufzeitfehler in JavaScript:
Die Eigenschaft "methods" eines undefinierten oder Nullverweises kann nicht abgerufen werden.
捆绑中需要更多/其他文件吗?
Need I more/other files in the bundle?
我该怎么办?有帮助吗?
What can I do? Any help?
真诚地 彼得
推荐答案
我是这样解决的:
在我看来是以下脚本块:
In my view is the following script block:
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
<script src="~/Scripts/cldr.js"></script>
<script src="~/Scripts/cldr/event.js"></script>
<script src="~/Scripts/cldr/supplemental.js"></script>
<script src="~/Scripts/cldr/unresolved.js"></script>
<script src="~/Scripts/globalize.js"></script>
<script src="~/Scripts/globalize/currency.js"></script>
<script src="~/Scripts/globalize/number.js"></script>
<script src="~/Scripts/globalize/date.js"></script>
<script src="~/Scripts/globalize/plural.js"></script>
<script src="~/Scripts/globalize/relative-time.js"></script>
<script src="~/Scripts/globalize/unit.js"></script>
<script src="~/Scripts/jquery.validate.globalize.js"></script>
<script>
$(document).ready(function () {
// Use $.getJSON instead of $.get if your server is not configured to return the
// right MIME type for .json files.
$.when(
$.get("/Scripts/cldr/main/de/ca-gregorian.json"),
$.get("/Scripts/cldr/main/de/numbers.json"),
$.get("/Scripts/cldr/supplemental/likelySubtags.json"),
$.get("/Scripts/cldr/supplemental/timeData.json"),
$.get("/Scripts/cldr/supplemental/weekData.json")
).then(function () {
// Normalize $.get results, we only need the JSON, not the request statuses.
return [].slice.apply(arguments, [0]).map(function (result) {
return result[0];
});
}).then(Globalize.load)
.then(function () {
Globalize.locale("de-DE");
});
});
我的数据类具有这样的注释:
My data-class has an annotiation like this:
...
[Required]
[DataType(DataType.DateTime)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd.MM.yyyy}")]
public DateTime Geburtsdatum { get; set; }
...
而且-非常重要!! -您必须获取数据文件! 这是必要的,因为clrd数据不是NuGet-Packages"jQuery.Validation.Globalize"/"jquery-globalize"的一部分. (是的,它在项目"页面上提到-但我没有看到... :-()
And - very important!! - you must get the data-files! This is nessecary because the clrd-data is not part of the NuGet-Packages "jQuery.Validation.Globalize" / "jquery-globalize". (Yes, it is mentioned on the Project-page - but I didn't see it... :-( )
我安装了Bower(每个NuGet),然后通过Bower安装了Cldr数据.示例:
I installed Bower (per NuGet) and installed then via Bower the Cldr-data. Example:
bower install cldr-dates-full
(请参阅软件包概述和安装说明 1 )
(see overview over packages and install instruction 1)
然后我移动了所需的json文件(在这里您可以找到用于选择文件的在线工具
Then I moved the needed json-files (here you find an online-tool for file-selection 2) from Directory "bower_components" to "scripts\cldr\main\de" respectively "scripts\cldr\supplemental".
我将它们添加到项目中并将其标记为内容",无副本".
I add them to the project and mark them as "Content", "no copy".
所以终于可以了!!! :-)
So finally it works!!! :-)
如果我将其捆绑成js文件和json文件,我将更新答案.
If I manage it to bundle the js- and json-files, I will update the answer.
这篇关于MVC 5-无法运行全球化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!