mediawiki.jqueryMsg: Support {{ns:}}
authorBartosz Dziewoński <matma.rex@gmail.com>
Fri, 9 Oct 2015 10:57:06 +0000 (12:57 +0200)
committerBartosz Dziewoński <matma.rex@gmail.com>
Fri, 9 Oct 2015 11:08:44 +0000 (13:08 +0200)
Often used in links like [[{{ns:template}}:Foo]], in order for the
link to use namespace name in wiki content language rather than
localised to user language.

Change-Id: Icc360724c75755a6ffc31792d8439cba142642ae

resources/src/mediawiki/mediawiki.jqueryMsg.js
tests/qunit/suites/resources/mediawiki/mediawiki.jqueryMsg.test.js

index 8957430..de63a18 100644 (file)
                        return mw.jqueryMsg.getMessageFunction()( msg.charAt( 0 ).toLowerCase() + msg.slice( 1 ) );
                },
 
+               /**
+                * Get localized namespace name from canonical name or namespace number.
+                * Invoked by putting `{{ns:foo}}` into a message
+                *
+                * @param {Array} nodes List of nodes
+                * @return {string} Localized namespace name
+                */
+               ns: function ( nodes ) {
+                       var ns = $.trim( textify( nodes[ 0 ] ) );
+                       if ( !/^\d+$/.test( ns ) ) {
+                               ns = mw.config.get( 'wgNamespaceIds' )[ ns.replace( / /g, '_' ).toLowerCase() ];
+                       }
+                       ns = mw.config.get( 'wgFormattedNamespaces' )[ ns ];
+                       return ns || '';
+               },
+
                /**
                 * Takes an unformatted number (arab, no group separators and . as decimal separator)
                 * and outputs it in the localized digit script and formatted with decimal
index 1d44cc1..4f273bc 100644 (file)
                        mw.language = this.originalMwLanguage;
                },
                config: {
-                       wgArticlePath: '/wiki/$1'
+                       wgArticlePath: '/wiki/$1',
+                       // jscs:disable requireCamelCaseOrUpperCaseIdentifiers
+                       wgNamespaceIds: {
+                               template: 10,
+                               template_talk: 11,
+                               // Localised
+                               szablon: 10,
+                               dyskusja_szablonu: 11
+                       },
+                       // jscs:enable requireCamelCaseOrUpperCaseIdentifiers
+                       wgFormattedNamespaces: {
+                               // Localised
+                               10: 'Szablon',
+                               11: 'Dyskusja szablonu'
+                       }
                },
                // Messages that are reused in multiple tests
                messages: {
                );
        } );
 
+       QUnit.test( 'Ns', 4, function ( assert ) {
+               mw.messages.set( 'ns-template-talk', '{{ns:Template talk}}' );
+               assert.equal(
+                       formatParse( 'ns-template-talk' ),
+                       'Dyskusja szablonu',
+                       'ns: returns localised namespace when used with a canonical namespace name'
+               );
+
+               mw.messages.set( 'ns-10', '{{ns:10}}' );
+               assert.equal(
+                       formatParse( 'ns-10' ),
+                       'Szablon',
+                       'ns: returns localised namespace when used with a namespace number'
+               );
+
+               mw.messages.set( 'ns-unknown', '{{ns:doesnt-exist}}' );
+               assert.equal(
+                       formatParse( 'ns-unknown' ),
+                       '',
+                       'ns: returns empty string for unknown namespace name'
+               );
+
+               mw.messages.set( 'ns-in-a-link', '[[{{ns:template}}:Foo]]' );
+               assert.equal(
+                       formatParse( 'ns-in-a-link' ),
+                       '<a title="Szablon:Foo" href="/wiki/Szablon:Foo">Szablon:Foo</a>',
+                       'ns: works when used inside a wikilink'
+               );
+       } );
+
        // Tests that getMessageFunction is used for non-plain messages with curly braces or
        // square brackets, but not otherwise.
        QUnit.test( 'mw.Message.prototype.parser monkey-patch', 22, function ( assert ) {