From: Rocky Meza Date: Mon, 11 Jun 2012 17:23:53 +0000 (-0600) Subject: getLangDefinition will now automatically load lang X-Git-Tag: 1.7.0~22^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F332%2Fhead;p=thirdparty%2Fmoment.git getLangDefinition will now automatically load lang I extracted the loadLang function out of moment.lang so that it could be used internally in moment. When developers use moment.fn.lang or moment.langData now, if the language requested has not yet been loaded, it will be loaded. Unlike moment.lang, these functions do not change the global language config. --- diff --git a/moment.js b/moment.js index fd04df9fd..99b943c84 100644 --- a/moment.js +++ b/moment.js @@ -280,6 +280,35 @@ 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 @@ -293,7 +322,7 @@ m && m._lang || currentLanguage; - return languages[langKey]; + return languages[langKey] || loadLang(langKey); } @@ -689,27 +718,17 @@ // 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 @@ -718,11 +737,6 @@ moment[langConfigProperties[i]] = languages[key][langConfigProperties[i]]; } currentLanguage = key; - } else { - if (hasModule) { - req = require('./lang/' + key); - moment.lang(key, req); - } } }; diff --git a/test/moment/lang.js b/test/moment/lang.js index bba97d126..364edc583 100644 --- a/test/moment/lang.js +++ b/test/moment/lang.js @@ -36,12 +36,6 @@ exports.lang = { "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'); @@ -53,11 +47,8 @@ exports.lang = { "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'); @@ -67,11 +58,8 @@ exports.lang = { "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'); @@ -81,11 +69,8 @@ exports.lang = { "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); @@ -98,11 +83,8 @@ exports.lang = { "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'); @@ -112,11 +94,8 @@ exports.lang = { "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); @@ -126,10 +105,6 @@ exports.lang = { "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'),