Skip to content

Commit

Permalink
Backwards compatibility with changing moment.months and other lang pr…
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Wood committed Jul 5, 2012
1 parent 73c3363 commit c018776
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
5 changes: 3 additions & 2 deletions moment.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,9 @@
function getLangDefinition(m) {
var langKey = (typeof m === 'string') && m ||
m && m._lang ||
currentLanguage;
null;

return languages[langKey] || loadLang(langKey);
return langKey ? (languages[langKey] || loadLang(langKey)) : moment;
}


Expand Down Expand Up @@ -729,6 +729,7 @@
for (i = 0; i < langConfigProperties.length; i++) {
moment[langConfigProperties[i]] = languages[key][langConfigProperties[i]];
}
moment.monthsParse = languages[key].monthsParse;
currentLanguage = key;
}
};
Expand Down
51 changes: 51 additions & 0 deletions test/moment/lang.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,57 @@ exports.lang = {
test.equal(moment([2011, 0, 1]).format('dd ddd dddd MMM MMMM'), 'date date date date date', 'format month name function should be able to access the moment object');
test.equal(moment([2011, 0, 2]).format('dd ddd dddd MMM MMMM'), 'default default default default default', 'format month name function should be able to access the moment object');

test.done();
},

// the following tests should be removed after the 2.0.0 release as they will be deprecated
"lang accessors on the global object should exist < 2.0.0" : function(test) {
moment.lang('en');

var a = 'months|monthsShort|monthsParse|weekdays|weekdaysShort|weekdaysMin|longDateFormat|calendar|relativeTime|ordinal|meridiem'.split('|');
var i;

test.expect(a.length);

for (i = 0; i < a.length; i++) {
test.ok(moment[a[i]], "moment." + a[i] + " should exist");
}

test.done();
},

// the following tests should be removed after the 2.0.0 release as they will be deprecated
"lang accessors on the global object should change < 2.0.0" : function(test) {
moment.lang('en');

var a = 'months|monthsShort|weekdays|weekdaysShort|weekdaysMin|longDateFormat|calendar|relativeTime|ordinal'.split('|');
var i;
var en = {};

test.expect(a.length);

for (i = 0; i < a.length; i++) {
en[a[i]] = moment[a[i]];
}

moment.lang('fr');

for (i = 0; i < a.length; i++) {
test.notDeepEqual(en[a[i]], moment[a[i]], "the " + a[i] + " lang data should change on the global object");
}

test.done();
},

"manip lang accessors on the global object < 2.0.0" : function(test) {
test.expect(1);
moment.lang('en');

moment.months = ["test"];
test.equal(moment([2011, 0]).format('MMMM'), "test", "Should be able to manipulate the objects on the global object");

moment.lang('en');

test.done();
}
};

0 comments on commit c018776

Please sign in to comment.