4

我正在开发一个使用 Globalize.js 的 ASP MVC 应用程序。在 _Layout.cshtml 我添加了这段代码

<script>
(function () {
$(function () {
    $.when(
              $.getJSON("@Url.Content("~/Scripts/cldr/supplemental/likelySubtags.json")"),
              $.getJSON("@Url.Content("~/Scripts/cldr/main/fr/numbers.json")"),
              $.getJSON("@Url.Content("~/Scripts/cldr/supplemental/numberingSystems.json")"),
              $.getJSON("@Url.Content("~/Scripts/cldr/main/fr/ca-gregorian.json")"),
              $.getJSON("@Url.Content("~/Scripts/cldr/main/fr/timeZoneNames.json")"),
              $.getJSON("@Url.Content("~/Scripts/cldr/supplemental/timeData.json")"),
              $.getJSON("@Url.Content("~/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("fr");
            });
});
})();
</script>

它正在工作。但是当我尝试在 $(document).ready 或 $(window).load 的其他页面中使用它时,我有错误 JavaScript: E_DEFAULT_LOCALE_NOT_DEFINED: Default locale has not been defined。

似乎 The Globalize 尚未加载。

4

2 回答 2

3

I know that this is a very old story, but I stumbled upon it and the answer is pretty simple - instead of doing what you want to do on the $(document).ready event, you have to wait for the globalize to finish loading the resources and then do your stuff.

The simple way of doing this is to trigger your own event after you loaded globalize like this:

function loadcldr() {
    var currentCultureCode = $("#hfTwoCharsCultureCode").val();
    var publicCdnGlobalizeCompleteUrl = "/Resources/cldr/";

    $.when(
        $.get(publicCdnGlobalizeCompleteUrl + "main/" + currentCultureCode + "/ca-gregorian.json"),
        $.get(publicCdnGlobalizeCompleteUrl + "main/" + currentCultureCode + "/numbers.json"),
        $.get(publicCdnGlobalizeCompleteUrl + "main/" + currentCultureCode + "/currencies.json"),
        $.get(publicCdnGlobalizeCompleteUrl + "supplemental/likelySubtags.json"),
        $.get(publicCdnGlobalizeCompleteUrl + "supplemental/timeData.json"),
        $.get(publicCdnGlobalizeCompleteUrl + "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(currentCultureCode);
        customNumberParser = Globalize.numberParser();

        $(document).trigger("globalizeHasBeenLoadedEvent");
    });
}

The line that is of interest for you is $(document).trigger("globalizeHasBeenLoadedEvent"); because this triggers the custom event.

And then you can wait for that event to happen and then do your stuff:

$(document).on("globalizeHasBeenLoadedEvent",
    function () {
        alert("I'm done loading globalize...");
    });

Hope it helps someone in the future... (not once I had an issue and I've searched on SO and found my own answers) :-))

于 2018-06-11T15:51:51.933 回答
2

E_DEFAULT_LOCALE_NOT_DEFINED当您尝试使用 Globalize 静态函数(例如,Globalize.formatMessage)而没有事先设置语言环境默认值(例如,Globalize.locale("fr"))时会发生错误。

您能否提供有关触发上述错误的代码的更多详细信息?

于 2016-06-23T15:30:06.573 回答