There was no need to do a delayed load of date.js, so I specified it in the header in the usual way. I did move it much further down, which may have given some of the prerequisites time to load.
var locLoaded = null;
function loadGlobalize(locale) {
var loc = util.fixNoE(locale, "en");
var codes = [
'ar', 'ca', 'cs', 'da', 'de', 'el', 'en', 'en-001', 'en-AU', 'en-CA',
'en-GB', 'en-HK', 'en-IN', 'es', 'fi', 'fr', 'he', 'hi', 'hr',
'hu', 'it', 'ja', 'ko', 'nb', 'nl', 'pl', 'pt', 'pt-PT', 'ro', 'ru',
'sk', 'sl', 'sr', 'sv', 'th', 'tr', 'uk', 'vi', 'zh', 'zh-Hant'
];
if (codes.indexOf(loc) < 0) {
var baseLoc = loc.split('-')[0];
var index = codes.indexOf(baseLoc);
loc = (index > -1 ? baseLoc : "EN");
}
if (loc != locLoaded) {
var files = [
"../globalize/supplemental/likelySubtags.json",
"../globalize/supplemental/timeData.json",
"../globalize/supplemental/weekData.json",
"../globalize/main/" + loc + "/ca-gregorian.json",
"../globalize/main/" + loc + "/numbers.json",
"../globalize/main/" + loc + "/dateFields.json"
];
var fileCount = files.length;
files.forEach(function(f) {
$.getJSON(f, function(data) {
fileCount--;
Globalize.load(data);
if (fileCount == 0) {
//wait until the last file is loaded to specify the locale
Globalize.locale(loc);
}
});
});
locLoaded = loc;
}
}
loadGlobalize(window.navigator.userLanguage || window.navigator.language);
In my app this code is in block where the DOM and jQuery are already loaded. There are two things that differ from the globalize.js documentation that caused this problem:
You have to do $.getJSON() instead of $.get() as specified in the documentation example.
If you run Globalize.locale() before the appropriate data files are loaded, you don't get a valid date formatter object.