r111646 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r111645‎ | r111646 | r111647 >
Date:15:57, 16 February 2012
Author:santhosh
Status:resolved (Comments)
Tags:i18nreview 
Comment:
Add grammar parsing support for mw.jqueryMsg
Define the magic SITENAME in mw.jqueryMsg itself
Add convertGrammar to mediawiki.language.js
The grammar data need to be pushed by a resource loader module. That and tests coming.
Modified paths:
  • /branches/jsgrammar/resources/mediawiki.language/mediawiki.language.js (modified) (history)
  • /branches/jsgrammar/resources/mediawiki/mediawiki.jqueryMsg.js (modified) (history)

Diff [purge]

Index: branches/jsgrammar/resources/mediawiki/mediawiki.jqueryMsg.js
@@ -103,7 +103,9 @@
104104 };
105105
106106 var parserDefaults = {
107 - 'magic' : {},
 107+ 'magic' : {
 108+ 'SITENAME' : mw.config.get( 'wgSiteName' )
 109+ },
108110 'messages' : mw.messages,
109111 'language' : mw.language
110112 };
@@ -660,8 +662,18 @@
661663 }
662664 var forms = nodes.slice(1);
663665 return this.language.gender( gender, forms );
 666+ },
 667+ /**
 668+ * Transform parsed structure into grammar conversion
 669+ * Invoked by putting {{grammar:form|word}} in a message
 670+ * @param {Array} of noders [{Grammar case eg: genitive}, {String word}]
 671+ * @return {String} selected gender form according to current language
 672+ */
 673+ grammar: function( nodes ) {
 674+ var form = nodes[0];
 675+ var word = nodes[1];
 676+ return this.language.convertGrammar( word , form );
664677 }
665 -
666678 };
667679
668680 // TODO figure out a way to make magic work with common globals like wgSiteName, without requiring init from library users...
Index: branches/jsgrammar/resources/mediawiki.language/mediawiki.language.js
@@ -9,6 +9,24 @@
1010
1111 mw.language = {
1212 /**
 13+ * @var data {Object} Langauge related data
 14+ * Keyed by language, contains instances of mw.Map
 15+ * @example Set data
 16+ * <code>
 17+ * var langCode = 'nl';
 18+ * var langData = mw.language.data;
 19+ * if ( langData[langCode] === undefined ) {
 20+ * langData[langCode] = new mw.Map();
 21+ * }
 22+ * langData[langCode].set( .. ); // Will override, extend or create the data
 23+ * </code>
 24+ * @example Get data
 25+ * <code>
 26+ * var grammerForms = mw.language.data[langCode].get( 'grammerForms' );
 27+ * </code>
 28+ */
 29+ data: {},
 30+ /**
1331 * Process the PLURAL template substitution
1432 *
1533 * @param {object} template Template object
@@ -121,7 +139,23 @@
122140 }
123141 return ( forms.length === 3 ) ? forms[2] : forms[0];
124142 },
125 -
 143+ /**
 144+ * Grammatical transformations, needed for inflected languages
 145+ * Invoked by putting {{grammar:form|word}} in a message
 146+ * The rules can be defined in wgGrammarForms global or grammar
 147+ * forms can be dynamically calculated by overriding this per language
 148+ *
 149+ * @param word string
 150+ * @param form string
 151+ * @return string
 152+ */
 153+ 'convertGrammar': function( word, form ) {
 154+ var grammarForms = mw.language.data[ mw.config.get( 'wgContentLanguage' )].get( 'grammarForms' );
 155+ if ( grammarForms && grammarForms[form] ) {
 156+ return grammarForms[form][word] || word ;
 157+ }
 158+ return word;
 159+ },
126160 // Digit Transform Table, populated by language classes where applicable
127161 'digitTransformTable': null
128162 };

Follow-up revisions

RevisionCommit summaryAuthorDate
r111725Qunit test cases for the grammar support in mw.jqueryMsg added in r111646santhosh04:50, 17 February 2012
r111735Typo fixes, code cleanup as per CR of r111646santhosh09:47, 17 February 2012
r112991[JSGrammar] Various minor tips, tricks and clean up....krinkle23:56, 4 March 2012

Comments

#Comment by Nikerabbit (talk | contribs)   09:25, 17 February 2012
+		/**
 	668	+		 * Transform parsed structure into grammar conversion 
 	669	+		 * Invoked by putting {{grammar:form|word}} in a message
 	670	+		 * @param {Array}  of noders [{Grammar case eg: genitive}, {String word}]
 	671	+		 * @return {String} selected gender form according to current language
 	672	+		 */

On line 668 I would add dot at the end. Line 670 to spaces before "of", should noders be nodes?. Line 671 mentions gender but should talk about the grammatical case or form.

In mediawiki.language.js:

  • Typo: Langauge
  • 2x typo: grammerForms
  • On line 153, no quotes needed around method name.
  • On line 154, extra space after "data["
  • On line 154, what if mw.language.data['foo'] is undefined? Doesn't .get throw JS error then?

Status & tagging log