Santhosh has uploaded a new change for review.

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

Change subject: After MT template applied fire a hook for post processing the 
section
......................................................................

After MT template applied fire a hook for post processing the section

Most of this processing was out of sequence after we integrated apertium.
Because of this image adaptation and alignment were misbehaving.

Change-Id: Ib4b33638cd9fbdc713eacceb62d41b0ca2a46c05
---
M modules/tools/ext.cx.tools.mt.js
M modules/translation/ext.cx.translation.js
2 files changed, 51 insertions(+), 47 deletions(-)


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

diff --git a/modules/tools/ext.cx.tools.mt.js b/modules/tools/ext.cx.tools.mt.js
index e5d8027..70475ec 100644
--- a/modules/tools/ext.cx.tools.mt.js
+++ b/modules/tools/ext.cx.tools.mt.js
@@ -39,19 +39,23 @@
                }
 
                $section = $( this );
-               sourceContent = $section[ 0 ].outerHTML;
 
+
+               if ( !mw.cx.tools.mt.enabled() ) {
+                       mw.hook( 'mw.cx.translation.postMT' ).fire( $section );
+                       return;
+               }
+
+               sourceContent = $section[ 0 ].outerHTML;
                mw.cx.mt( mw.cx.sourceLanguage, mw.cx.targetLanguage, 
sourceContent )
                        .done( function ( translation ) {
                                if ( translation ) {
-                                       $section.html( $( translation 
).children().html() )
-                                               .adaptLinks( 
mw.cx.targetLanguage );
+                                       $section.html( $( translation 
).children().html() );
                                }
-                       } ).fail( function () {
-                               $section.adaptLinks( mw.cx.targetLanguage );
                        } ).always( function () {
                                $section.data( 'cx-mt', true );
                                mw.hook( 'mw.cx.translation.change' ).fire( 
$section );
+                               mw.hook( 'mw.cx.translation.postMT' ).fire( 
$section );
                        } );
 
                return this;
diff --git a/modules/translation/ext.cx.translation.js 
b/modules/translation/ext.cx.translation.js
index d380765..118ae21 100644
--- a/modules/translation/ext.cx.translation.js
+++ b/modules/translation/ext.cx.translation.js
@@ -94,8 +94,8 @@
        ContentTranslationEditor.prototype.listen = function () {
                var cxTranslation = this;
 
-               mw.hook( 'mw.cx.translation.add' ).add( $.proxy( this.update, 
this ) );
-
+               mw.hook( 'mw.cx.translation.add' ).add( $.proxy( 
this.applyTranslationTemplate, this ) );
+               mw.hook( 'mw.cx.translation.postMT' ).add( $.proxy( 
this.postProcessMT, this ) );
                mw.hook( 'mw.cx.source.loaded' ).add( function () {
                        // Delay adding placeholders. If we calculate the 
section
                        // dimensions before all css and screenpainting is done,
@@ -127,52 +127,21 @@
        };
 
        /**
-        * Update the translation section with the machine translation
-        * @param {string} sourceId source section identifier
-        * @param {boolean} machineTranslate Whether to do machine translation; 
default is false.
+        * Post-process the section after MT is applied
+        * @param {jQuery} $section
         */
-       ContentTranslationEditor.prototype.update = function ( sourceId, 
machineTranslate ) {
-               var $sourceSection, targetSectionId, $section;
+       ContentTranslationEditor.prototype.postProcessMT = function ( $section 
) {
+               var $sourceSection;
 
-               if ( machineTranslate === undefined ) {
-                       machineTranslate = true;
-               }
-
-               $sourceSection = $( '#' + sourceId );
-               targetSectionId = jquerySelectorForId( sourceId, 'cx' );
-               $section = $( targetSectionId );
-
-               // Replace the placeholder with the source section
-               $section.replaceWith( $sourceSection
-                       .clone()
-                       .attr( {
-                               id: 'cx' + sourceId,
-                               'data-source': sourceId
-                       } )
-               );
-
-               // $section was replaced. Get the updated instance.
-               $section = $( targetSectionId );
-
-               if ( machineTranslate ) {
-                       $section.each( function () {
-                               if ( mw.cx.tools.mt.enabled() ) {
-                                       $section.machineTranslate();
-                               }
-                       } );
-               }
-
+               // Adapt links
+               $section.adaptLinks( mw.cx.targetLanguage );
+               // Adapt images
                $section.find( 'img' ).adaptImage( mw.cx.targetLanguage );
-
+               // Adapt references
+               $sourceSection = $( '#' + $section.data( 'source' ) );
                if ( $.fn.adaptReferences ) { // This is an experimental feature
                        $section.find( '[typeof="mw:Extension/ref"]' 
).adaptReferences();
                }
-
-               // Trigger input event so that the alignment will be correct
-               $section.on( 'input', $.debounce( 200, false, function () {
-                       $( this ).data( 'cx-mt', false );
-                       mw.hook( 'mw.cx.translation.change' ).fire( $( this ) );
-               } ) );
 
                // If the section is editable, initiate an editor.
                // Otherwise make it non-editable. Example: templates
@@ -181,6 +150,12 @@
                } else {
                        $section.cxEditor();
                }
+
+               // Trigger input event so that the alignment will be correct
+               $section.on( 'input', $.debounce( 200, false, function () {
+                       $( this ).data( 'cx-mt', false );
+                       mw.hook( 'mw.cx.translation.change' ).fire( $( this ) );
+               } ) );
 
                // Search for text that was selected using the mouse.
                // Delay it to run every 250 ms so it won't fire all the time 
while typing.
@@ -200,6 +175,31 @@
        };
 
        /**
+        * Update the translation section with the machine translation template
+        * @param {string} sourceId source section identifier
+        */
+       ContentTranslationEditor.prototype.applyTranslationTemplate = function 
( sourceId ) {
+               var $sourceSection, targetSectionId, $section;
+
+               $sourceSection = $( '#' + sourceId );
+               targetSectionId = jquerySelectorForId( sourceId, 'cx' );
+               $section = $( targetSectionId );
+
+               // Replace the placeholder with the source section
+               $section.replaceWith( $sourceSection
+                       .clone()
+                       .attr( {
+                               id: 'cx' + sourceId,
+                               'data-source': sourceId
+                       } )
+               );
+
+               // $section was replaced. Get the updated instance.
+               $section = $( targetSectionId );
+               $section.machineTranslate();
+       };
+
+       /**
         * Generate a jquery selector for all sections
         * @return {string} the section selector string
         */

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

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