Santhosh has uploaded a new change for review.

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

Change subject: Adapt the link on the source text copied as template for 
translation
......................................................................

Adapt the link on the source text copied as template for translation

* Introduce $.fn.adaptLinks
* Link adaptation does not have any UI now. It happens when user clicks
  on the placeholders in translation column or sections in the source column.
  When source text is copied, links are translated-technically the href values
  of links are modified

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


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

diff --git a/Resources.php b/Resources.php
index f8bbb8d..99ef882 100644
--- a/Resources.php
+++ b/Resources.php
@@ -90,6 +90,7 @@
        'scripts' => array(
                'tools/ext.cx.tools.js',
                'tools/ext.cx.tools.helpmessage.js',
+               'tools/ext.cx.tools.linkadaptor.js',
        ),
        'styles' => array(
                'tools/styles/ext.cx.tools.less',
diff --git a/modules/tools/ext.cx.tools.linkadaptor.js 
b/modules/tools/ext.cx.tools.linkadaptor.js
new file mode 100644
index 0000000..8e16f8c
--- /dev/null
+++ b/modules/tools/ext.cx.tools.linkadaptor.js
@@ -0,0 +1,105 @@
+/**
+ * ContentTranslation Tools
+ * Link Adaptor
+ *
+ * @file
+ * @ingroup Extensions
+ * @copyright See AUTHORS.txt
+ * @license GPL-2.0+
+ */
+( function ( $, mw ) {
+       'use strict';
+
+       /**
+        * Link Adaptor
+        * @class
+        */
+       function LinkAdaptor() {
+               // TODO This will get expanded as we add link adaptation UX.
+       }
+
+       /**
+        * Adapt the given title to a target language
+        * @param {string|string[]} titles A titles or or array of titles
+        * @param {string} targetLanguage Language to which the links are to be 
adapted
+        * @return {jQuery.Promise}
+        */
+       LinkAdaptor.prototype.adapt = function ( titles, targetLanguage ) {
+               var api, deferred;
+
+               api = new mw.Api();
+               deferred = $.Deferred();
+               if ( !$.isArray( titles ) ) {
+                       titles = new Array( titles );
+               }
+               api.get( {
+                       action: 'query',
+                       titles: titles.join( '|' ),
+                       prop: 'langlinks',
+                       lllang: targetLanguage,
+                       format: 'json'
+               } ).done( function ( response ) {
+                       var linkPairs = {};
+                       if ( response.query ) {
+                               $.each( response.query.pages, function ( key, 
value ) {
+                                       linkPairs[ value.title ] = 
value.langlinks && value.langlinks[ 0 ] && value.langlinks[ 0 ][ '*' ];
+                               } );
+                       }
+                       deferred.resolve( linkPairs );
+               } ).fail( function ( error ) {
+                       mw.log( 'Error while adapting links:' + error );
+                       // No need to make this error visible beyond logging
+                       deferred.resolve( {} );
+               } );
+               return deferred.promise();
+       };
+
+       /**
+        * Remove the leading slashes or dots if any.
+        */
+       function cleanupLinkHref( href ) {
+               return href.replace( './', '' );
+       }
+
+       /**
+        * jQuery plugin to adapt all the links with rel="mw:WikiLink"
+        * @param {string} targetLanguage
+        */
+       $.fn.adaptLinks = function ( targetLanguage ) {
+               var linkAdaptor = new LinkAdaptor();
+               return this.each( function () {
+                       var $this = $( this ),
+                               $links,
+                               sourceTitles;
+
+                       $links = $this.find( 'a[rel="mw:WikiLink"]' );
+                       // Collect all sourceTitles;
+                       sourceTitles = $links.map( function () {
+                               var href = $( this ).attr( 'href' );
+                               // some cleanup
+                               return cleanupLinkHref( href );
+                       } ).get();
+
+                       function apply( adaptations ) {
+                               $links.map( function () {
+                                       var $this = $( this ),
+                                               href = $this.attr( 'href' );
+
+                                       href = cleanupLinkHref( href );
+                                       if ( adaptations[ href ] ) {
+                                               $( this ).prop( 'href', 
adaptations[ href ] );
+                                               // TODO: The links has parsoid 
data attributes that holds the original href.
+                                               // We may need to update that 
too. Example:
+                                               // 
data-parsoid="{"stx":"simple","a":{"href":"./Seed"},"sa":{"href":"Seed"},
+                                               // 
"dsr":[3376,3385,2,3],"tail":"s"}
+                                       } else {
+                                               // Remove the link
+                                               $( this ).after( $( this 
).text() ).remove();
+                                       }
+                               } );
+                       }
+                       linkAdaptor.adapt( sourceTitles, targetLanguage ).done( 
apply );
+               } );
+       };
+
+}( jQuery, mediaWiki ) );
diff --git a/modules/translation/ext.cx.translation.js 
b/modules/translation/ext.cx.translation.js
index eb98235..287d6ad 100644
--- a/modules/translation/ext.cx.translation.js
+++ b/modules/translation/ext.cx.translation.js
@@ -99,7 +99,7 @@
                                $( this ).html( translation );
                        }
                } );
-
+               $section.adaptLinks( mw.cx.targetLanguage );
                // Trigger input event so that the alignemnt is right.
                $section.trigger( 'input' );
 

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

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