Santhosh has uploaded a new change for review. https://gerrit.wikimedia.org/r/119029
Change subject: ROT13 dummy translation service integration ...................................................................... ROT13 dummy translation service integration Integrate ROT13 MT interface. This version of ROT13 destroy the html in the input by converting tags also. An html parser need to be used. Added that as a TODO Change-Id: Id522cbd2464387a23bb7b4621d4d6664abe42a64 --- M modules/translation/ext.cx.translation.js M server/models/DataModelManager.js M server/mt/CXMTInterface.js M server/mt/providers/ROT13.js 4 files changed, 70 insertions(+), 33 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ContentTranslation refs/changes/29/119029/1 diff --git a/modules/translation/ext.cx.translation.js b/modules/translation/ext.cx.translation.js index 9b317ad..75c3c52 100644 --- a/modules/translation/ext.cx.translation.js +++ b/modules/translation/ext.cx.translation.js @@ -41,9 +41,9 @@ this.$container.append( $( '<h2>' ) - .attr( 'contenteditable', true ) - .addClass( 'cx-column__title' ) - .text( $( '.cx-column--source .cx-column__title' ).text() ) + .attr( 'contenteditable', true ) + .addClass( 'cx-column__title' ) + .text( $( '.cx-column--source .cx-column__title' ).text() ) ); if ( mw.cx.targetLanguage ) { @@ -64,7 +64,6 @@ } $content = $( '<div>' ) - //.attr( 'contenteditable', true ) .addClass( 'cx-column__content' ) .html( '\n' ); // Make sure that it's visible to the tests @@ -75,7 +74,7 @@ }; ContentTranslationEditor.prototype.listen = function () { - mw.hook( 'mw.cx.translation.add' ).add( $.proxy( this.update, this ) ); + mw.hook( 'mw.cx.translation.add' ).add( $.proxy( this.update, this ) ); mw.hook( 'mw.cx.source.loaded' ).add( $.proxy( this.addPlaceholders, this ) ); this.$content.on( 'input', function () { mw.hook( 'mw.cx.translation.change' ).fire(); @@ -84,20 +83,33 @@ }; ContentTranslationEditor.prototype.update = function ( sourceId ) { - var sourceHtml = $( '#' + sourceId ).html(); + $( '#t' + sourceId ).empty(); - $( '#t' + sourceId ).html( sourceHtml ); - mw.hook( 'mw.cx.progress' ).fire( 100 ); + $( '#' + sourceId ).find( '.cx-segment' ).each( function () { + $( '#t' + sourceId ).append( mw.cx.data.mt[ $( this ).data( 'segmentid' ) ] ); + } ).trigger( 'input' ); + + this.calculateCompletion(); mw.hook( 'mw.cx.translation.change' ).fire(); }; + // 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 ); + }; + ContentTranslationEditor.prototype.addPlaceholders = function () { - var cxSections = 'p, h1, h2, h3, div, table, figure, ul' ; + var cxSections = 'p, h1, h2, h3, div, table, figure, ul'; + this.$content.html( mw.cx.data.segmentedContent ); this.$content.find( cxSections ).each( function () { var $section = $( this ), sourceSectionId = $section.attr( 'id' ), - $sourceSection = $( '#' + sourceSectionId ); + $sourceSection = $( '#' + sourceSectionId ); $section.css( 'min-height', $section.height() ); $section.attr( { 'id': 't' + sourceSectionId, @@ -108,7 +120,7 @@ keepAlignment( $section ); // Bind events to the placeholder sections - $sourceSection.click( function() { + $sourceSection.click( function () { $( '#t' + $( this ).attr( 'id' ) ).click(); } ); $section.on( 'mouseenter', function () { @@ -127,7 +139,7 @@ }; function keepAlignment( $section ) { - $section .on( 'input', function () { + $section.on( 'input', function () { var $sourceSection, sectionHeight, sourceSectionHeight; $section.css( 'min-height', '' ); @@ -149,13 +161,11 @@ data = $this.data( 'cxTranslation' ); if ( !data ) { - $this.data( 'cxTranslation', - ( data = new ContentTranslationEditor( this, options ) ) - ); + $this.data( 'cxTranslation', ( data = new ContentTranslationEditor( this, options ) ) ); } if ( typeof options === 'string' ) { - data[options].call( $this ); + data[ options ].call( $this ); } } ); }; diff --git a/server/models/DataModelManager.js b/server/models/DataModelManager.js index a84410e..7e0fb14 100644 --- a/server/models/DataModelManager.js +++ b/server/models/DataModelManager.js @@ -9,7 +9,8 @@ 'use strict'; -var CXSegmenter = require( __dirname + '/../segmentation/CXSegmenter.js' ).CXSegmenter; +var CXSegmenter = require( __dirname + '/../segmentation/CXSegmenter.js' ).CXSegmenter, + CXMTInterface = require( __dirname + '/../mt/CXMTInterface.js' ).CXMTInterface; /** * CXDataModelManager * @class @@ -26,6 +27,7 @@ CXDataModelManager.prototype.init = function () { var dataModelManager = this, segmenter, + mtInterface, PageLoader, pageloader; // TODO: refactor this @@ -54,10 +56,16 @@ dataModelManager.publish(); // TODO: Dispatch the context to a number of task runners // Once each task runners finish, publish. - }, function ( /*jqXHR, textStatus, errorThrown*/ ) { + mtInterface = new CXMTInterface( dataModelManager.context ); + mtInterface.translate( dataModelManager.dataModel.segments ).then( function ( translations ) { + dataModelManager.dataModel.mt = translations; + dataModelManager.publish(); + } ); + }, function () { console.error( '[CX] Error in retrieving the page ' + dataModelManager.context.sourcePage ); } ); + } } ); }; diff --git a/server/mt/CXMTInterface.js b/server/mt/CXMTInterface.js index 3a7a1c2..1bee78b 100644 --- a/server/mt/CXMTInterface.js +++ b/server/mt/CXMTInterface.js @@ -5,16 +5,16 @@ 'use strict'; -var ROT13Service = require( __dirname+ '/providers/ROT13.js' ).ROT13Service; +var ROT13Service = require( __dirname + '/providers/ROT13.js' ).ROT13Service; /** * @class CxTranslator */ -function CXMTInterface ( config ) { - this.config = config; +function CXMTInterface( context ) { + this.context = context; } -CXMTInterface.prototype.translate = function ( sourceLang, targetLang, sourceText/*, onSuccess, onError */) { - return ( new ROT13Service(this.config) ).translate( sourceLang, targetLang, sourceText ); +CXMTInterface.prototype.translate = function ( segments ) { + return ( new ROT13Service( this.context ) ).translate( segments ); }; -module.exports = { 'CXMTInterface': CXMTInterface }; +module.exports.CXMTInterface = CXMTInterface; diff --git a/server/mt/providers/ROT13.js b/server/mt/providers/ROT13.js index 4bba13c..9b34794 100644 --- a/server/mt/providers/ROT13.js +++ b/server/mt/providers/ROT13.js @@ -1,25 +1,44 @@ -/* - * Content Translation Machine Translation Interface. +/** + * ROT13 Translation service * + * A dummy interface to test the CX MT */ 'use strict'; +var Q = require( 'q' ); + /** * @class ROT13Service */ -function ROT13Service(config) { +function ROT13Service( config ) { this.config = config; } -ROT13Service.prototype.translate = function ( sourceLang, targetLang, sourceText /*, onSuccess, onError */ ) { - return sourceText.replace(/[a-zA-Z]/g, function (c) { - return String.fromCharCode( (c <= 'Z' ? 90 : 122) >= (c = c.charCodeAt(0) + 13) - ? c - : c - 26); +ROT13Service.prototype.translate = function ( segments ) { + var rot13 = this, + deferred = Q.defer(), + mt = {}; + // Simulate a 1000 millisecond delay. + setTimeout( function () { + for ( var segmentId in segments ) { + mt[ segmentId ] = rot13.ROT13( segments[ segmentId ].source ); + } + deferred.resolve( mt ); + }, 1000 ); + + return deferred.promise; +}; + +ROT13Service.prototype.ROT13 = function ( text ) { + // TODO: Below regex converts < and > symbols too. That destroy the html inside + // the text(like links). This conversion should be done inside an html parser + // so that ROT13Service can work with html too. + return text.replace( /[a-zA-Z><]/g, function ( c ) { + return String.fromCharCode( ( c <= 'Z' ? 90 : 122 ) >= ( c = c.charCodeAt( 0 ) + 13 ) ? c : c - 26 ); } ); }; module.exports = { 'ROT13Service': ROT13Service -}; \ No newline at end of file +}; -- To view, visit https://gerrit.wikimedia.org/r/119029 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Id522cbd2464387a23bb7b4621d4d6664abe42a64 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