Santhosh has uploaded a new change for review.

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

Change subject: Translation progress caculation algorithm implementation
......................................................................

Translation progress caculation algorithm implementation

* Algorithm 
https://www.mediawiki.org/wiki/Content_translation/Progress_calculation
* Refactored all translation section changes to an event
  'mw.cx.translation.change'

Change-Id: I6193f732a2d611c80ebfbadb1e7576db60954730
---
M Resources.php
M modules/translation/ext.cx.translation.js
A modules/translation/ext.cx.translation.progress.js
3 files changed, 110 insertions(+), 19 deletions(-)


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

diff --git a/Resources.php b/Resources.php
index e4726ed..5c9fb97 100644
--- a/Resources.php
+++ b/Resources.php
@@ -78,6 +78,7 @@
 $wgResourceModules['ext.cx.translation'] = array(
        'scripts' => 'translation/ext.cx.translation.js',
        'dependencies' => array(
+               'ext.cx.translation.progress',
                'jquery.uls.data',
                'mediawiki.Uri',
        ),
@@ -89,6 +90,10 @@
        ),
 ) + $resourcePaths;
 
+$wgResourceModules['ext.cx.translation.progress'] = array(
+       'scripts' => 'translation/ext.cx.translation.progress.js',
+) + $resourcePaths;
+
 $wgResourceModules['ext.cx.tools.manager'] = array(
        'scripts' => array(
                'tools/ext.cx.tools.manager.js',
diff --git a/modules/translation/ext.cx.translation.js 
b/modules/translation/ext.cx.translation.js
index fbf43a1..2bc826b 100644
--- a/modules/translation/ext.cx.translation.js
+++ b/modules/translation/ext.cx.translation.js
@@ -11,6 +11,15 @@
 ( function ( $, mw ) {
        'use strict';
 
+       var delay = ( function () {
+               var timer = 0;
+
+               return function ( callback, milliseconds ) {
+                       clearTimeout( timer );
+                       timer = setTimeout( callback, milliseconds );
+               };
+       }() );
+
        /**
         * ContentTranslationEditor
         *
@@ -102,7 +111,9 @@
                                $placeholder = getPlaceholder( sourceId );
 
                        $( targetSectionId ).replaceWith( $placeholder );
+                       mw.hook( 'mw.cx.translation.change' ).fire( $( 
targetSectionId ) );
                } );
+               mw.hook( 'mw.cx.translation.change' ).add( keepAlignment );
        };
 
        /**
@@ -136,8 +147,12 @@
                $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' );
+               $section.on( 'input', function () {
+                       var $section = $( this );
+                       delay( function () {
+                               mw.hook( 'mw.cx.translation.change' ).fire( 
$section );
+                       }, 200 );
+               } );
                // If the section is editable, initiate an editor
                // Otherwise make it non-editable. Example: templates
                if ( $sourceSection.data( 'editable' ) === false ) {
@@ -145,25 +160,14 @@
                } else {
                        $section.cxEditor();
                }
-               // Calculate the progress of the translation
-               this.calculateCompletion();
-               mw.hook( 'mw.cx.translation.change' ).fire();
+
+               $section.trigger( 'input' );
                $section.on( 'click', function () {
                        var selection = window.getSelection().toString();
                        if ( selection ) {
                                mw.hook( 'mw.cx.search.link' ).fire( 
selection.toLowerCase() );
                        }
                } );
-       };
-
-       // TODO This is a dummy completeness calculation.
-       ContentTranslationEditor.prototype.calculateCompletion = function () {
-               var completeness;
-
-               completeness = $( '.cx-column--translation' ).html().length /
-                       $( '.cx-column--source' ).html().length * 100;
-               completeness = completeness > 100 ? 100 : completeness;
-               mw.hook( 'mw.cx.progress' ).fire( completeness );
        };
 
        /**
@@ -320,11 +324,12 @@
         * Keep the height of the source and translation sections equal
         * so that they will appear top aligned.
         */
-       function keepAlignment() {
-               var $sourceSection, sectionHeight, sourceSectionHeight, 
$section, steps = 0;
+       function keepAlignment( $section ) {
+               var $sourceSection, sectionHeight, sourceSectionHeight, steps = 
0;
 
-               /*jshint validthis:true */
-               $section = $( this );
+               if ( !$section ) {
+                       return;
+               }
 
                if ( $section.prop( 'tagName' ) === 'TABLE' ) {
                        // 'min-height' is undefined for table elements
diff --git a/modules/translation/ext.cx.translation.progress.js 
b/modules/translation/ext.cx.translation.progress.js
new file mode 100644
index 0000000..cb0bb82
--- /dev/null
+++ b/modules/translation/ext.cx.translation.progress.js
@@ -0,0 +1,81 @@
+/**
+ * ContentTranslation - Calculate the progress
+ * 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';
+
+       var totalSourceWeight = 0,
+               translationThreshold = 0.05;
+
+       /**
+        * Get the total source weight
+        * @return {int} total source weight
+        */
+       function getTotalSourceWeight() {
+               var $sourceContainer,
+                       $sections;
+
+               if ( totalSourceWeight ) {
+                       return totalSourceWeight;
+               }
+               $sourceContainer = $( '.cx-column--source .cx-column__content' 
);
+               $sections = $sourceContainer.children( 
mw.cx.getSectionSelector() );
+
+               $sections.each( function () {
+                       totalSourceWeight += $( this ).text().length;
+               } );
+
+               return totalSourceWeight;
+       }
+
+       /**
+        * Calculate the translation progress
+        */
+       function calculateProgress() {
+               var completedTranslation = 0,
+                       percentage;
+
+               $( '.cx-column--translation [data-cx-weight]' ).each( function 
() {
+                       completedTranslation += $( this ).data( 'cx-weight' );
+               } );
+               percentage = completedTranslation /
+                       getTotalSourceWeight() * 100;
+               mw.hook( 'mw.cx.progress' ).fire( percentage );
+       }
+
+       /**
+        * Update/Change handler for section
+        * @param {jQuery} $section The source section
+        */
+       function onSectionUpdate( $section ) {
+               var $sourceSection, sourceLength, translationLength;
+
+               if ( !$section ) {
+                       return;
+               }
+               $sourceSection = $( '#' + $section.data( 'source' ) );
+               translationLength = $section.text().length;
+               sourceLength = $sourceSection.text().length;
+
+               // Check if the translation is above the defined threshold to 
count.
+               if ( translationLength / sourceLength < translationThreshold ) {
+                       // Do not count the section as translated
+                       $section.removeAttr( 'data-cx-weight' );
+               } else {
+                       $section.attr( 'data-cx-weight', sourceLength );
+               }
+               // Calculate the total translation progress
+               calculateProgress();
+       }
+
+       $( function () {
+               mw.hook( 'mw.cx.translation.change' ).add( onSectionUpdate );
+       } );
+}( jQuery, mediaWiki ) );

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

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