BPositive has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/149632

Change subject: [PagePreparation] Files handled as a part of page preparation
......................................................................

[PagePreparation] Files handled as a part of page preparation

The files are prepared for translation by adding translate tags around
translatable content. A function has been written to retrieve the
namespace aliases and the returned aliases are used to build the regex.

Change-Id: I61e3a89a1d013ce88a03b3f42abd7c535dbd797f
---
M resources/js/ext.translate.pagepreparation.js
1 file changed, 54 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Translate 
refs/changes/32/149632/1

diff --git a/resources/js/ext.translate.pagepreparation.js 
b/resources/js/ext.translate.pagepreparation.js
index 77612b2..e707348 100644
--- a/resources/js/ext.translate.pagepreparation.js
+++ b/resources/js/ext.translate.pagepreparation.js
@@ -96,6 +96,57 @@
        }
 
        /**
+        * Fetch all the aliases for a given namespace on the wiki.
+        * @param {string} pageContent
+        * @return {Array} The list of aliases for the given namespace
+        */
+       function getNamespaceAliases( namespace ) {
+               var namespaceID, api = new mw.Api();
+
+               namespaceID = mw.config.get( 'wgNamespaceIds' )[namespace];
+               return api.get( {
+                       action:'query',
+                       meta: 'siteinfo',
+                       siprop: 'namespacealiases'
+               } ).then( function ( data ) {
+                       var aliases = [];
+                       for ( var alias in data.query.namespacealiases ) {
+                               if( data.query.namespacealiases[alias].id === 
namespaceID ) {
+                                       aliases.push( $.escapeRE( 
data.query.namespacealiases[alias]['*'] ) );
+                               }
+                       }
+                       return aliases;
+               } ).promise();
+       }
+
+       /**
+        * Add translate tags around only translatable content for files and 
keep everything else
+        * as a part of the page template.
+        * @param {string} pageContent
+        * @return {string}
+        */
+       function doFiles( pageContent ) {
+
+               $.when( getNamespaceAliases( 'file' ) ).then( function ( 
aliases ) {
+                       var aliasList, captionFilesRegex, fileRegex;
+
+                       aliases.push( 'file' );
+                       aliasList = aliases.join('|');
+
+                       // Add translate tags for files with captions
+                       captionFilesRegex = new RegExp( '\\[\\[(' + aliasList + 
')(.*\\|)(.*?)\\]\\]', 'gi' );
+                       pageContent = pageContent.replace( captionFilesRegex,
+                               
'</translate>\n[[$1$2<translate>$3</translate>]]\n<translate>' );
+
+                       // Add translate tags for files without captions
+                       fileRegex = new RegExp( '/\\[\\[((' + aliasList + 
')[^\\|]*?)\]\]', 'gi' );
+                       pageContent = pageContent.replace( fileRegex, 
'\n</translate>[[$1]]\n<translate>' );
+
+                       return pageContent;
+               } );
+       }
+
+       /**
         * Cleanup done after the page is prepared for translation by the tool.
         * @param {string} pageContent
         * @return {string}
@@ -161,12 +212,14 @@
        $( document ).ready( function () {
                var pageName;
                pageName = mw.config.get( 'wgPageName' );
-               $.when( getPageContent( pageName ) ).done( function ( 
pageContent ) {
+               $.when( getPageContent( pageName ) ).done( function ( content ) 
{
+                       var pageContent = content;
                        pageContent = $.trim( pageContent );
                        pageContent = cleanupTags( pageContent );
                        pageContent = addLanguageBar( pageContent );
                        pageContent = addTranslateTags( pageContent );
                        pageContent = addNewLines( pageContent );
+                       pageContent = doFiles( pageContent );
                        pageContent = fixInternalLinks( pageContent );
                        pageContent = postPreparationCleanup( pageContent );
                        pageContent = $.trim( pageContent );

-- 
To view, visit https://gerrit.wikimedia.org/r/149632
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I61e3a89a1d013ce88a03b3f42abd7c535dbd797f
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Translate
Gerrit-Branch: master
Gerrit-Owner: BPositive <pr4tiklah...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to