r90290 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r90289‎ | r90290 | r90291 >
Date:16:29, 17 June 2011
Author:jeroendedauw
Status:deferred
Tags:
Comment:
added idleness detection to complete when a json request failed
Modified paths:
  • /trunk/extensions/LiveTranslate/RELEASE-NOTES (modified) (history)
  • /trunk/extensions/LiveTranslate/includes/ext.livetranslate.js (modified) (history)
  • /trunk/extensions/LiveTranslate/includes/ext.lt.ms.js (modified) (history)

Diff [purge]

Index: trunk/extensions/LiveTranslate/RELEASE-NOTES
@@ -4,7 +4,7 @@
55 Latest version of the release notes: http://svn.wikimedia.org/viewvc/mediawiki/trunk/extensions/LiveTranslate/RELEASE-NOTES?view=co
66
77
8 -=== Version 1.1.0 ===
 8+=== Version 1.1 ===
99 2011-06-16
1010
1111 * Added support for the Microsoft Translation service.
Index: trunk/extensions/LiveTranslate/includes/ext.livetranslate.js
@@ -233,10 +233,10 @@
234234 translator.done = handleTranslationCompletion;
235235 ltdebug( 'Initiating remote translation' );
236236 translator.translateElement( $( '#bodyContent' ), sourceLang, targetLang );
237 - ltdebug( 'Remote translation completed' );
238237 }
239238
240239 function handleTranslationCompletion( targetLang ) {
 240+ ltdebug( 'Remote translation completed' );
241241 currentLang = targetLang;
242242 $( '#livetranslatebutton' ).attr( "disabled", false ).text( mediaWiki.msg( 'livetranslate-button-translate' ) );
243243 $( '#ltrevertbutton' ).css( 'display', 'inline' );
Index: trunk/extensions/LiveTranslate/includes/ext.lt.ms.js
@@ -13,6 +13,8 @@
1414 this.done = function( targetLang ){};
1515
1616 this.runningJobs = 0;
 17+ this.checkingForIdle = false;
 18+ this.lastCompletion;
1719
1820 /**
1921 * Determines a chunk to translate of an DOM elements contents and calls the Microsoft Translate API.
@@ -76,43 +78,57 @@
7779 this.handleTranslationCompletion( targetLang );
7880 return;
7981 }
80 -
 82+
8183 // Keep track of leading and tailing spaces, as they often get modified by the GT API.
8284 var leadingSpace = chunk.substr( 0, 1 ) == ' ' ? ' ' : '';
8385 var tailingSpace = ( chunk.length > 1 && chunk.substr( chunk.length - 1, 1 ) == ' ' ) ? ' ' : '';
8486
85 - $.getJSON(
86 - 'http://api.microsofttranslator.com/V2/Ajax.svc/Translate?oncomplete=?',
87 - {
88 - 'appId': window.ltMsAppId,
89 - 'from': sourceLang,
90 - 'to': targetLang,
91 - 'text': jQuery.trim( chunk ) // Trim, so the result does not contain preceding or tailing spaces.
92 - },
93 - function( translation ) {
94 - ltdebug( 'MS: Translated chunk' );
95 -
96 - if ( translation ) {
97 - chunks.push( leadingSpace + translation + tailingSpace );
98 - }
99 - else {
100 - // If the translation failed, keep the original text.
101 - chunks.push( chunk );
102 - }
103 -
104 - if ( untranslatedsentences.length == 0 ) {
105 - // If the current chunk was smaller then the max size, node translation is complete, so update text.
106 - window.textAreaElement.innerHTML = chunks.join( '' ); // This is a hack to decode quotes.
107 - element.replaceData( 0, element.length, window.textAreaElement.value );
 87+ var chunckTranslationDone = function( translation ) {
 88+ ltdebug( 'MS: Translated chunk' );
 89+
 90+ if ( translation ) {
 91+ chunks.push( leadingSpace + translation + tailingSpace );
 92+ }
 93+ else {
 94+ // If the translation failed, keep the original text.
 95+ chunks.push( chunk );
 96+ }
 97+
 98+ if ( untranslatedsentences.length == 0 ) {
 99+ // If the current chunk was smaller then the max size, node translation is complete, so update text.
 100+ window.textAreaElement.innerHTML = chunks.join( '' ); // This is a hack to decode quotes.
 101+ element.replaceData( 0, element.length, window.textAreaElement.value );
108102
109 - self.handleTranslationCompletion( targetLang );
110 - }
111 - else {
112 - // If there is more work to do, move on to the next chunk.
113 - self.translateChunk( untranslatedsentences, chunks, currentMaxSize, sourceLang, targetLang, element );
114 - }
 103+ ltdebug( 'MS: Translated element' );
 104+ self.handleTranslationCompletion( targetLang );
115105 }
116 - );
 106+ else {
 107+ // If there is more work to do, move on to the next chunk.
 108+ self.translateChunk( untranslatedsentences, chunks, currentMaxSize, sourceLang, targetLang, element );
 109+ }
 110+ };
 111+
 112+ // Trim, so the result does not contain preceding or tailing spaces.
 113+ var trimmedChunk = jQuery.trim( chunk );
 114+
 115+// if ( trimmedChunk.length < 50 ) {
 116+// self.handleTranslationCompletion( targetLang );
 117+// //chunckTranslationDone( trimmedChunk );
 118+// }
 119+// else {
 120+ $.getJSON(
 121+ 'http://api.microsofttranslator.com/V2/Ajax.svc/Translate?oncomplete=?',
 122+ {
 123+ 'appId': window.ltMsAppId,
 124+ 'from': sourceLang,
 125+ 'to': targetLang,
 126+ 'text': trimmedChunk
 127+ },
 128+ chunckTranslationDone
 129+ );
 130+// }
 131+
 132+
117133 }
118134
119135 /**
@@ -163,6 +179,33 @@
164180 this.handleTranslationCompletion( targetLang );
165181 }
166182
 183+ this.invokeDone = function( targetLang ) {
 184+ ltdebug( 'MS: translation process done' );
 185+ ltdebug( this.runningJobs );
 186+ this.runningJobs = 0;
 187+ this.done( targetLang );
 188+ }
 189+
 190+ this.checkForIdleness = function( targetLang, hits ) {
 191+ ltdebug( 'MS: checkForIdleness' );
 192+ ltdebug( 'MS: last + 250: ' + ( this.lastCompletion + 250 ) );
 193+ ltdebug( 'MS: now: ' + (new Date()).getTime() );
 194+
 195+ if ( this.lastCompletion + 250 < (new Date()).getTime() ) {
 196+ hits++;
 197+ }
 198+ else {
 199+ hits = 0;
 200+ }
 201+
 202+ if ( hits > 4 ) {
 203+ this.invokeDone( targetLang );
 204+ }
 205+ else if ( this.runningJobs > 0 ) {
 206+ setTimeout( function() { self.checkForIdleness( targetLang, hits ); }, 250 );
 207+ }
 208+ }
 209+
167210 /**
168211 * Should be called every time a DOM element has been translated.
169212 * By use of the runningJobs var, completion of the translation process is detected,
@@ -171,10 +214,19 @@
172215 * @param {string} targetLang
173216 */
174217 this.handleTranslationCompletion = function( targetLang ) {
 218+ if ( !this.checkingForIdle && this.runningJobs > 1 && this.runningJobs < 20 ) {
 219+ this.checkingForIdle = true;
 220+ setTimeout( function() { self.checkForIdleness( targetLang, 0 ); }, 250 );
 221+ }
 222+
 223+ if ( this.checkingForIdle ) {
 224+ this.lastCompletion = (new Date()).getTime();
 225+ }
 226+
175227 if ( !--this.runningJobs ) {
176 - ltdebug( 'MS: translation process done' );
177 - this.done( targetLang );
 228+ this.invokeDone( targetLang );
178229 }
179230 }
180231
 232+
181233 }; })( jQuery );
\ No newline at end of file

Follow-up revisions

RevisionCommit summaryAuthorDate
r90291follow up to r90290jeroendedauw16:35, 17 June 2011

Status & tagging log