簡體   English   中英

MVC 5-無法運行全球化

[英]MVC 5 - can not get globalisation running

我想添加全球化,因為該網站要求用戶提供日期。 我的德語用戶想輸入“ 31.12.1966”而不是“ 1966-12-31”。

因此,我將nuget-Packages“ jQuery.Validation.Globalize”和“ jquery-globalize”添加到項目中。

現在,我無法配置BundleConfig! 從我的研究中,我知道我需要globalize.js和其他文件。 所以我試着做一個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

我翻譯了這張德語原著的信息:

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.

捆綁包中是否需要更多/其他文件?

我能做什么? 有什么幫助嗎?

真誠的彼得

我是這樣解決的:

在我看來是以下腳本塊:

<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");
    });
    });

我的數據類具有這樣的注釋:

...
[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”的一部分。 (是的,它在“項目”頁面上提到-但我沒有看到它... :-()

我安裝了Bower(每個NuGet),然后通過Bower安裝了Cldr-data。 例:

 bower install cldr-dates-full

(請參閱軟件包概述和安裝說明1

然后,我將所需的json文件(在此處找到用於文件選擇2的在線工具)從目錄“ bower_components”移至“ scripts \\ cldr \\ main \\ de”或“ scripts \\ cldr \\ supplemental”。

我將它們添加到項目中並將其標記為“內容”,“無副本”。

所以終於可以了!!! :-)

如果我將其捆綁到js和json文件中,則將更新答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM