Santhosh has uploaded a new change for review. https://gerrit.wikimedia.org/r/292548
Change subject: Fetch and rememeber the templace namespace alias for target language ...................................................................... Fetch and rememeber the templace namespace alias for target language This makes the splitting of Template title to get namespace-less name more safe Now we have this namespace fetching duplicated in image tool module too. We need to refactor and create utility to get target namespaces. Follow up: If6a2166e922b753dad2a85f Bug: T136817 Change-Id: I41ea6b3ecf0e27f17d037057425b525dd94cebd4 --- M modules/tools/ext.cx.tools.template.js 1 file changed, 56 insertions(+), 5 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ContentTranslation refs/changes/48/292548/1 diff --git a/modules/tools/ext.cx.tools.template.js b/modules/tools/ext.cx.tools.template.js index d6b8e04..9694838 100644 --- a/modules/tools/ext.cx.tools.template.js +++ b/modules/tools/ext.cx.tools.template.js @@ -10,7 +10,8 @@ ( function ( $, mw ) { 'use strict'; - var cachedTemplates = {}; + var targetTemplateNamespace, + cachedTemplates = {}; /** * TemplateTool encapsulates the handling of templates in translation. @@ -63,6 +64,51 @@ }; /** + * Get the namespace translation in a wiki. + * Use the canonical name for lookup. + * + * @param {string} targetLanguage + * @return {jQuery.Promise} + */ + function getTemplaceNamespaceTranslation( targetLanguage ) { + var deferred = $.Deferred(); + + if ( targetTemplateNamespace ) { + return deferred.resolve( targetTemplateNamespace ).promise(); + } + + // TODO: Refactor to avoid global reference + mw.cx.siteMapper.getApi( targetLanguage ).get( { + action: 'query', + meta: 'siteinfo', + siprop: 'namespaces', + format: 'json' + }, { + dataType: 'jsonp', + // This prevents warnings about the unrecognized parameter "_" + cache: true + } ).done( function ( response ) { + var namespaceId, namespaceObj; + + for ( namespaceId in response.query.namespaces ) { + namespaceObj = response.query.namespaces[ namespaceId ]; + if ( namespaceObj.canonical === 'Template' ) { + targetTemplateNamespace = namespaceObj[ '*' ]; + deferred.resolve( targetTemplateNamespace ); + return; + } + } + + deferred.resolve( targetTemplateNamespace ); + } ).fail( function () { + // Fallback to canonical name + deferred.resolve( 'Template' ); + } ); + + return deferred.promise(); + } + + /** * Get the template mapping if any set by source.filter module */ TemplateTool.prototype.getTemplateMapping = function () { @@ -113,11 +159,16 @@ * Adapt the template name to the equivalent in the target wiki */ TemplateTool.prototype.adaptTitle = function ( targetTitle ) { - var templateName; + var self = this; // Update the name of the template. We need template name without namespace - templateName = targetTitle.split( ':' )[ 1 ] || targetTitle; - this.templateData.parts[ 0 ].template.target.wt = templateName; - this.$template.attr( 'data-mw', JSON.stringify( this.templateData ) ); + getTemplaceNamespaceTranslation( mw.cx.targetLanguage ) + .done( function ( translatedNamespace ) { + var templateName; + + templateName = targetTitle.replace( translatedNamespace + ':', '' ); + self.templateData.parts[ 0 ].template.target.wt = templateName; + self.$template.attr( 'data-mw', JSON.stringify( self.templateData ) ); + } ); }; /** -- To view, visit https://gerrit.wikimedia.org/r/292548 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I41ea6b3ecf0e27f17d037057425b525dd94cebd4 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/ContentTranslation Gerrit-Branch: master Gerrit-Owner: Santhosh <santhosh.thottin...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits