return date;
}
+ // Loads a language definition into the `languages` cache. The function
+ // takes a key and optionally values. If not in the browser and no values
+ // are provided, it will load the language file module. As a convenience,
+ // this function also returns the language values.
+ function loadLang(key, values) {
+ var i,
+ parse = [];
+
+ if (!values && hasModule) {
+ values = require('./lang/' + key);
+ }
+
+ for (i = 0; i < langConfigProperties.length; i++) {
+ // If a language definition does not provide a value, inherit
+ // from English
+ values[langConfigProperties[i]] = values[langConfigProperties[i]] ||
+ languages.en[langConfigProperties[i]];
+ }
+
+ for (i = 0; i < 12; i++) {
+ parse[i] = new RegExp('^' + values.months[i] + '|^' + values.monthsShort[i].replace('.', ''), 'i');
+ }
+ values.monthsParse = values.monthsParse || parse;
+
+ languages[key] = values;
+
+ return values;
+ }
+
// Determines which language definition to use and returns it.
//
// With no parameters, it will return the global language. If you
m && m._lang ||
currentLanguage;
- return languages[langKey];
+ return languages[langKey] || loadLang(langKey);
}
// default format
moment.defaultFormat = isoFormat;
- // language switching and caching
+ // This function will load languages and then set the global language. If
+ // no arguments are passed in, it will simply return the current global
+ // language key.
moment.lang = function (key, values) {
- var i, req,
- parse = [];
+ var i;
+
if (!key) {
return currentLanguage;
}
- if (values) {
- for (i = 0; i < langConfigProperties.length; i++) {
- // If a language definition does not provide a value, inherit
- // from English
- values[langConfigProperties[i]] = values[langConfigProperties[i]] ||
- languages.en[langConfigProperties[i]];
- }
-
- for (i = 0; i < 12; i++) {
- parse[i] = new RegExp('^' + values.months[i] + '|^' + values.monthsShort[i].replace('.', ''), 'i');
- }
- values.monthsParse = values.monthsParse || parse;
-
- languages[key] = values;
+ if (values || !languages[key]) {
+ loadLang(key, values);
}
if (languages[key]) {
// deprecated, to get the language definition variables, use the
moment[langConfigProperties[i]] = languages[key][langConfigProperties[i]];
}
currentLanguage = key;
- } else {
- if (hasModule) {
- req = require('./lang/' + key);
- moment.lang(key, req);
- }
}
};
"library langData" : function(test) {
test.expect(3);
-
- // load Spanish
- moment.lang('es');
- // load Chinese
- moment.lang('zh-cn');
- // set global language
moment.lang('en');
test.equal(moment.langData().months[0], 'January', 'no arguments returns global');
"instance lang method" : function(test) {
test.expect(3);
-
- // load Spanish
- moment.lang('es');
- // set global language
moment.lang('en');
+
test.equal(moment([2012, 5, 6]).format('MMMM'), 'June', 'Normally default to global');
test.equal(moment([2012, 5, 6]).lang('es').format('MMMM'), 'Junio', 'Use the instance specific language');
test.equal(moment([2012, 5, 6]).format('MMMM'), 'June', 'Using an instance specific language does not affect other moments');
"instance lang persists with manipulation" : function(test) {
test.expect(3);
-
- // load Spanish
- moment.lang('es');
- // set global language
moment.lang('en');
+
test.equal(moment([2012, 5, 6]).lang('es').add({days: 1}).format('MMMM'), 'Junio', 'With addition');
test.equal(moment([2012, 5, 6]).lang('es').day(0).format('MMMM'), 'Junio', 'With day getter');
test.equal(moment([2012, 5, 6]).lang('es').eod().format('MMMM'), 'Junio', 'With eod');
"instance lang persists with cloning" : function(test) {
test.expect(2);
-
- // load Spanish
- moment.lang('es');
- // set global language
moment.lang('en');
+
var a = moment([2012, 5, 6]).lang('es'),
b = a.clone(),
c = moment(a);
"duration lang method" : function(test) {
test.expect(3);
-
- // load Spanish
- moment.lang('es');
- // set global language
moment.lang('en');
+
test.equal(moment.duration({seconds: 44}).humanize(), 'a few seconds', 'Normally default to global');
test.equal(moment.duration({seconds: 44}).lang('es').humanize(), 'unos segundos', 'Use the instance specific language');
test.equal(moment.duration({seconds: 44}).humanize(), 'a few seconds', 'Using an instance specific language does not affect other durations');
"duration lang persists with cloning" : function(test) {
test.expect(1);
-
- // load Spanish
- moment.lang('es');
- // set global language
moment.lang('en');
+
var a = moment.duration({seconds: 44}).lang('es'),
b = moment.duration(a);
"instance lang used with from" : function(test) {
test.expect(2);
-
- // load Spanish
- moment.lang('es');
- // set global language
moment.lang('en');
var a = moment([2012, 5, 6]).lang('es'),