From: Iskren Chernev Date: Fri, 29 Aug 2014 16:11:45 +0000 (+0300) Subject: Fix moment().locale('ivalid-id') X-Git-Tag: 2.8.3~13^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F1896%2Fhead;p=thirdparty%2Fmoment.git Fix moment().locale('ivalid-id') --- diff --git a/moment.js b/moment.js index 868206aad..3b5d65adc 100644 --- a/moment.js +++ b/moment.js @@ -2448,10 +2448,15 @@ // instance. Otherwise, it will return the locale configuration // variables for this instance. locale : function (key) { + var newLocaleData; + if (key === undefined) { return this._locale._abbr; } else { - this._locale = moment.localeData(key); + newLocaleData = moment.localeData(key); + if (newLocaleData != null) { + this._locale = newLocaleData; + } return this; } }, @@ -2462,8 +2467,7 @@ if (key === undefined) { return this.localeData(); } else { - this._locale = moment.localeData(key); - return this; + return this.locale(key); } } ), diff --git a/test/moment/locale.js b/test/moment/locale.js index bf0bcdefb..57ecb4ae9 100644 --- a/test/moment/locale.js +++ b/test/moment/locale.js @@ -458,5 +458,17 @@ exports.locale = { test.equal(m.locale(), 'fr', 'instance locale reset to global locale'); test.done(); + }, + + 'moment().locale with missing key doesn\'t change locale' : function (test) { + test.equal(moment().locale('boo').localeData(), moment.localeData(), + 'preserve global locale in case of bad locale id'); + test.done(); + }, + + 'moment().lang with missing key doesn\'t change locale' : function (test) { + test.equal(moment().lang('boo').localeData(), moment.localeData(), + 'preserve global locale in case of bad locale id'); + test.done(); } };