Santhosh has uploaded a new change for review.

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

Change subject: Adapt images for the target language
......................................................................

Adapt images for the target language

Translate the namespace used in resource attribute of img tags
as per https://www.mediawiki.org/wiki/Parsoid/MediaWiki_DOM_spec#Images

Change-Id: Ia3fb7878279525630d5ebdc0ff1d1d24c1b1a4be
---
M Resources.php
A modules/tools/ext.cx.tools.images.js
M modules/translation/ext.cx.translation.js
3 files changed, 97 insertions(+), 1 deletion(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ContentTranslation 
refs/changes/90/140090/1

diff --git a/Resources.php b/Resources.php
index ae5d2b7..7f9669b 100644
--- a/Resources.php
+++ b/Resources.php
@@ -112,7 +112,8 @@
                'ext.cx.tools.manager',
                'ext.cx.tools.dictionary',
                'ext.cx.tools.instructions',
-               'ext.cx.tools.link'
+               'ext.cx.tools.link',
+               'ext.cx.tools.images'
        ),
 ) + $resourcePaths;
 
@@ -179,6 +180,12 @@
        ),
 ) + $resourcePaths;
 
+$wgResourceModules['ext.cx.tools.images'] = array(
+       'scripts' => array(
+               'tools/ext.cx.tools.images.js',
+       ),
+) + $resourcePaths;
+
 $wgResourceModules['ext.cx.progressbar'] = array(
        'scripts' => 'tools/ext.cx.progressbar.js',
        'styles' => array(
diff --git a/modules/tools/ext.cx.tools.images.js 
b/modules/tools/ext.cx.tools.images.js
new file mode 100644
index 0000000..b1b80f5
--- /dev/null
+++ b/modules/tools/ext.cx.tools.images.js
@@ -0,0 +1,88 @@
+/**
+ * ContentTranslation Tools
+ * A tool that allows editors to translate pages from one language
+ * to another with the help of machine translation and other translation tools
+ *
+ * @file
+ * @ingroup Extensions
+ * @copyright See AUTHORS.txt
+ * @license GPL-2.0+
+ */
+( function ( $, mw ) {
+       'use strict';
+
+       /**
+        * Canonical namespace for images.
+        */
+       var imageNameSpace = 'File',
+               cachedNamespaces = {};
+
+       /**
+        * Get the namespace translation in a wiki.
+        * Use the canonical name for lookup.
+        * @param {string} targetLanguage
+        * @return {jQuery.Promise}
+        */
+       function getImageNamespaceTranslation( targetLanguage ) {
+               var api = new mw.Api(),
+                       deferred = $.Deferred();
+
+               if ( cachedNamespaces[ targetLanguage ] ) {
+                       return deferred.resolve( cachedNamespaces[ 
targetLanguage ] );
+               }
+
+               api.get( {
+                       action: 'query',
+                       meta: 'siteinfo',
+                       siprop: 'namespaces',
+                       format: 'json'
+               }, {
+                       url: '//' + targetLanguage + '.wikipedia.org/w/api.php',
+                       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 === imageNameSpace 
) {
+                                       cachedNamespaces[ targetLanguage ] = 
namespaceObj[ '*' ];
+                                       deferred.resolve( cachedNamespaces[ 
targetLanguage ] );
+                                       return;
+                               }
+                       }
+                       deferred.resolve( imageNameSpace );
+               } ).fail( function () {
+                       // Fallback to canonical name
+                       deferred.resolve( imageNameSpace );
+               } );
+
+               return deferred.promise();
+       }
+
+       /**
+        * jQuery plugin to adapt images so that parsoid can translate it to
+        * proper wiki text.
+        * @param {string} targetLanguage
+        */
+       $.fn.adaptImage = function ( targetLanguage ) {
+               return this.each( function () {
+                       var $image = $( this );
+
+                       getImageNamespaceTranslation( targetLanguage )
+                               .done( function ( translatedNamespace ) {
+                                       var resource;
+
+                                       resource = $image.attr( 'resource' );
+                                       // Example replacement:
+                                       // ./Archivo:ImageName to 
./Fitxer:ImageName
+                                       resource = resource.replace( 
/(\.\/)([0-9a-zA-Z]+)(:)/g,
+                                               '$1' + translatedNamespace + 
'$3' );
+                                       $image.attr( 'resource', resource );
+                                       // If image has parent link, corrent 
its link target
+                                       $image.parent( 'a' ).attr( 'href', 
resource );
+                               } );
+               } );
+       };
+}( jQuery, mediaWiki ) );
diff --git a/modules/translation/ext.cx.translation.js 
b/modules/translation/ext.cx.translation.js
index bec021e..afb9a3c 100644
--- a/modules/translation/ext.cx.translation.js
+++ b/modules/translation/ext.cx.translation.js
@@ -122,6 +122,7 @@
                } );
                // Adapt the links
                $section.adaptLinks( mw.cx.targetLanguage );
+               $section.find( 'img' ).adaptImage( mw.cx.targetLanguage );
                // Trigger input event so that the alignemnt is right.
                $section.on( 'input', keepAlignment )
                        .trigger( 'input' );

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia3fb7878279525630d5ebdc0ff1d1d24c1b1a4be
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

Reply via email to