mediawiki.language: Implement Language::listToText in JS
authorAlex Monk <krenair@wikimedia.org>
Fri, 29 Aug 2014 22:41:05 +0000 (23:41 +0100)
committerTimo Tijhof <krinklemail@gmail.com>
Sun, 31 Aug 2014 17:52:18 +0000 (19:52 +0200)
For I35e55658 in VisualEditor.

Change-Id: I5d894f8a1cba65a3d90dfbf3e43be4e515a327d1

resources/Resources.php
resources/src/mediawiki.language/mediawiki.language.js
tests/qunit/suites/resources/mediawiki/mediawiki.language.test.js

index 4a9827a..cbb8301 100644 (file)
@@ -1106,6 +1106,11 @@ return array(
                                'mediawiki.cldr',
                        ),
                'targets' => array( 'desktop', 'mobile' ),
+               'messages' => array(
+                       'and',
+                       'comma-separator',
+                       'word-separator'
+               ),
        ),
 
        'mediawiki.cldr' => array(
index a0b5569..2e84858 100644 (file)
@@ -144,8 +144,28 @@ $.extend( mw.language, {
                        return grammarForms[form][word] || word;
                }
                return word;
-       }
+       },
 
+       /**
+        * Turn a list of string into a simple list using commas and 'and'.
+        *
+        * See Language::listToText in languages/Language.php
+        *
+        * @param {string[]} list
+        * @return {string}
+        */
+       listToText: function ( list ) {
+               var text = '', i = 0;
+               for ( ; i < list.length; i++ ) {
+                       text += list[i];
+                       if ( list.length - 2 === i ) {
+                               text += mw.msg( 'and' ) + mw.msg( 'word-separator' );
+                       } else if ( list.length - 1 !== i ) {
+                               text += mw.msg( 'comma-separator' );
+                       }
+               }
+               return text;
+       }
 } );
 
 }( mediaWiki, jQuery ) );
index 3bfabe4..31e6fe5 100644 (file)
                        grammarTest( langCode, test );
                }
        } );
+
+       QUnit.test( 'List to text test', 4, function ( assert ) {
+               assert.equal( mw.language.listToText( [] ), '', 'Blank list' );
+               assert.equal( mw.language.listToText( ['a'] ), 'a', 'Single item' );
+               assert.equal( mw.language.listToText( ['a', 'b'] ), 'a and b', 'Two items' );
+               assert.equal( mw.language.listToText( ['a', 'b', 'c'] ), 'a, b and c', 'More than two items' );
+       } );
 }( mediaWiki, jQuery ) );