From: Rocky Meza Date: Sun, 10 Jun 2012 19:34:36 +0000 (-0600) Subject: expose the getLangDefinition function X-Git-Tag: 1.7.0~22^2~3 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c44b3f021c4a3846645a076df1ae5b4c5d0c3a6b;p=thirdparty%2Fmoment.git expose the getLangDefinition function --- diff --git a/moment.js b/moment.js index 3be21d1b3..60b13d825 100644 --- a/moment.js +++ b/moment.js @@ -280,6 +280,22 @@ 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 @@ -317,13 +333,6 @@ 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); @@ -770,6 +779,9 @@ } }); + // returns language data + moment.langData = getLangDefinition; + // compare moment object moment.isMoment = function (obj) { return obj instanceof Moment; diff --git a/test/moment/lang.js b/test/moment/lang.js index 86df74f00..bba97d126 100644 --- a/test/moment/lang.js +++ b/test/moment/lang.js @@ -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);