From 8a02e4379148f5aa4936f4ee57a327359808901d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bartosz=20Dziewo=C5=84ski?= Date: Fri, 9 Oct 2015 12:57:06 +0200 Subject: [PATCH] mediawiki.jqueryMsg: Support {{ns:}} 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 --- .../src/mediawiki/mediawiki.jqueryMsg.js | 16 +++++++ .../mediawiki/mediawiki.jqueryMsg.test.js | 46 ++++++++++++++++++- 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/resources/src/mediawiki/mediawiki.jqueryMsg.js b/resources/src/mediawiki/mediawiki.jqueryMsg.js index 8957430c3f..de63a18ce9 100644 --- a/resources/src/mediawiki/mediawiki.jqueryMsg.js +++ b/resources/src/mediawiki/mediawiki.jqueryMsg.js @@ -1227,6 +1227,22 @@ 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 diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.jqueryMsg.test.js b/tests/qunit/suites/resources/mediawiki/mediawiki.jqueryMsg.test.js index 1d44cc1e00..4f273bc5f6 100644 --- a/tests/qunit/suites/resources/mediawiki/mediawiki.jqueryMsg.test.js +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.jqueryMsg.test.js @@ -34,7 +34,21 @@ 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: { @@ -659,6 +673,36 @@ ); } ); + 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' ), + 'Szablon:Foo', + '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 ) { -- 2.20.1