[英]Globalize.js E_DEFAULT_LOCALE_NOT_DEFINED error
我正在開發一個使用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:尚未定義默認語言環境。
似乎全球化尚未加載。
當您嘗試使用Globalize靜態函數(例如, Globalize.formatMessage
)而未事先設置區域設置默認值(例如, Globalize.locale("fr")
)時,會發生E_DEFAULT_LOCALE_NOT_DEFINED
錯誤。
您能否提供有關觸發上述錯誤的代碼的更多詳細信息?
我知道這是一個非常古老的故事,但我偶然發現它並且答案非常簡單 - 而不是在$(document).ready
事件上做你想做的事情,你必須等待globalize完成加載資源,然后做你的東西。
這樣做的簡單方法是在加載全局化之后觸發自己的事件,如下所示:
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");
});
}
您感興趣的行是$(document).trigger("globalizeHasBeenLoadedEvent");
因為這會觸發自定義事件。
然后你可以等待那個事件發生,然后做你的事情:
$(document).on("globalizeHasBeenLoadedEvent",
function () {
alert("I'm done loading globalize...");
});
希望它能幫助將來的某個人......(不是我曾經遇到過問題而且我已經搜索了SO並找到了我自己的答案):-))
Globalize JavaScript文件可能不是您嘗試使用Globalize函數的其他頁面的一部分。
嘗試將<script type="text/javascript" src="YourPath/Globalize.js">
在它無法處理的任何頁面之上。
如果存在所有其他頁面都繼承的基本_Layout頁面,那么請查看是否可以在該頁面上包含該腳本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.