Skip to content

Commit

Permalink
some reverse compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
icambron committed Jul 24, 2014
1 parent 677db20 commit b09888a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
19 changes: 14 additions & 5 deletions moment.js
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@

moment[field] = function (format, index) {
var i, getter,
method = moment.fn._locale[field],
method = moment._locale[field],
results = [];

if (typeof format === 'number') {
Expand All @@ -613,7 +613,7 @@

getter = function (i) {
var m = moment().utc().set(setter, i);
return method.call(moment.fn._locale, m, format || '');
return method.call(moment._locale, m, format || '');
};

if (index != null) {
Expand Down Expand Up @@ -730,9 +730,13 @@
}

function loadLocale(name) {
var oldLocale = null;
if (!locales[name] && hasModule) {
try {
oldLocale = moment.locale();
require('./locale/' + name);
// because defineLocale currently also sets the global locale, we want to undo that for lazy loaded locales
moment.locale(oldLocale);
} catch (e) { }
}
return locales[name];
Expand Down Expand Up @@ -1913,11 +1917,11 @@
}

if (data) {
moment.duration.fn._locale = moment.fn._locale = data;
moment.duration._locale = moment._locale = data;
}
}

return moment.fn._locale._abbr;
return moment._locale._abbr;
};

moment.defineLocale = function (name, values) {
Expand All @@ -1927,8 +1931,13 @@
locales[name] = new Locale();
}
locales[name].set(values);

// backwards compat for now: also set the locale
moment.locale(name);

return locales[name];
} else {
// useful for testing
delete locales[name];
return null;
}
Expand All @@ -1950,7 +1959,7 @@
}

if (!key) {
return moment.fn._locale;
return moment._locale;
}

if (!isArray(key)) {
Expand Down
3 changes: 3 additions & 0 deletions tasks/embed_locales.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ module.exports = function (grunt) {
grunt.file.write(config.dest, modifiedContents);
});

var languageReset = 'moment.locale(\'en\');'

function determineEmbeddedContent(files) {
var embeddedContent = '';
files.forEach(function (file) {
embeddedContent += transformFile(file);
});
embeddedContent += '\n ' + languageReset + '\n';
return embeddedContent;
}

Expand Down
10 changes: 9 additions & 1 deletion test/moment/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ exports.locale = {
"defineLocale" : function (test) {
moment.locale("en");
moment.defineLocale("dude", {months: ["Movember"]});
test.equal(moment().locale(), "en", "defineLocale doesn't set it");
test.equal(moment().locale(), "dude", "defineLocale also sets it");
test.equal(moment().locale("dude").locale(), "dude", "defineLocale defines a locale");
test.done();
},
Expand Down Expand Up @@ -411,6 +411,14 @@ exports.locale = {

test.equal(registered, 'return-this', 'returns the locale configured');

test.done();
},

"changing the global locale doesn't affect existing instances" : function (test) {
moment.locale('en');
var mom = moment();
moment.locale('pr');
test.equal('en', moment.locale());
test.done();
}
};

0 comments on commit b09888a

Please sign in to comment.