]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
expose the getLangDefinition function
authorRocky Meza <rocky@fusionbox.com>
Sun, 10 Jun 2012 19:34:36 +0000 (13:34 -0600)
committerRocky Meza <rocky@fusionbox.com>
Sun, 10 Jun 2012 19:34:36 +0000 (13:34 -0600)
moment.js
test/moment/lang.js

index 3be21d1b3221ba21752220c365bd8e4ffef9769d..60b13d8255f7637e0ec256568cc57c264e63a46f 100644 (file)
--- a/moment.js
+++ b/moment.js
         return date;
     }
 
+    // Determines which language definition to use and returns it.
+    //
+    // With no parameters, it will return the global language.  If you
+    // pass in a language key, such as 'en', it will return the
+    // definition for 'en', so long as 'en' has already been loaded using
+    // moment.lang.  If you pass in a moment or duration instance, it
+    // will decide the language based on that, or default to the global
+    // language.
+    function getLangDefinition(m) {
+        var langKey = (typeof m === 'object') && m._lang ||
+                      (typeof m === 'string') && m ||
+                      currentLanguage;
+
+        return languages[langKey];
+    }
+
 
     /************************************
         Formatting
         return formatFunctions[format];
     }
 
-    // Determines which language definition to use based on a moment instance.
-    // If a moment has the _lang property set on it, it will return that
-    // language; otherwise, it will return the global currentLanguage.
-    function getLangDefinition(m) {
-        return languages[(m && m._lang) || currentLanguage];
-    }
-
     // format date using native date object
     function formatMoment(m, format) {
         var lang = getLangDefinition(m);
         }
     });
 
+    // returns language data
+    moment.langData = getLangDefinition;
+
     // compare moment object
     moment.isMoment = function (obj) {
         return obj instanceof Moment;
index 86df74f00cf53984212669f3817c2ea7e735841e..bba97d126ec47a6998b6686374ffd732a3cc957b 100644 (file)
@@ -34,6 +34,23 @@ exports.lang = {
         test.done();
     },
 
+    "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');
+        test.equal(moment.langData('zh-cn').months[0], '一月', 'a string returns the language based on key');
+        test.equal(moment.langData(moment().lang('es')).months[0], 'Enero', "if you pass in a moment it uses the moment's language");
+
+        test.done();
+    },
+
     "instance lang method" : function(test) {
         test.expect(3);