]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
getLangDefinition will now automatically load lang 332/head
authorRocky Meza <rocky@fusionbox.com>
Mon, 11 Jun 2012 17:23:53 +0000 (11:23 -0600)
committerRocky Meza <rocky@fusionbox.com>
Mon, 11 Jun 2012 17:23:53 +0000 (11:23 -0600)
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.

moment.js
test/moment/lang.js

index fd04df9fda35fd70ad13d9860d0f697686aa4c00..99b943c84ad3b7e0fad0a00df85eaec10aaf86a5 100644 (file)
--- a/moment.js
+++ b/moment.js
         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);
-            }
         }
     };
 
index bba97d126ec47a6998b6686374ffd732a3cc957b..364edc583f8f5e09fef975f6a5dd06533b02731c 100644 (file)
@@ -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'),