r79078 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r79077‎ | r79078 | r79079 >
Date:18:01, 27 December 2010
Author:jeroendedauw
Status:deferred (Comments)
Tags:
Comment:
Follow up to r79038
Modified paths:
  • /trunk/extensions/LiveTranslate/includes/ext.livetranslate.js (modified) (history)

Diff [purge]

Index: trunk/extensions/LiveTranslate/includes/ext.livetranslate.js
@@ -155,27 +155,21 @@
156156 function translateElement( element, sourceLang, targetLang ) {
157157 runningJobs++;
158158
 159+ // Max chunk size is 500 - 2 for the anti-trim delimiters.
 160+ var maxChunkLength = 498;
 161+
159162 element.contents().each( function() {
160163 // If it's a text node, then translate it.
161164 if ( this.nodeType == 3 && this.wholeText.trim().length > 0 ) {
162165 runningJobs++;
163 -
164 - var sentances = this.wholeText.split( new RegExp( "(\\S.+?[.!?])(?=\\s+|$)", "gi" ) );
165 - var chunk = '';
166 -
167 - for ( i in sentances ) {
168 - var longerChunk = chunk + sentances[i];
169 -
170 - if ( longerChunk.length < 498 ) {
171 - chunk = longerChunk;
172 - }
173 - else {
174 - break;
175 - }
176 - }
177 -
178 - // Initiate translation of the text node. Max chunk size is 500 - 2 for the anti-trim delimiters.
179 - translateChunk( this.wholeText, [], chunk.length, sourceLang, targetLang, this );
 166+ translateChunk(
 167+ this.wholeText.split( new RegExp( "(\\S.+?[.!?])(?=\\s+|$)", "gi" ) ),
 168+ [],
 169+ maxChunkLength,
 170+ sourceLang,
 171+ targetLang,
 172+ this
 173+ );
180174 }
181175 // If it's an html element, check to see if it should be ignored, and if not, apply function again.
182176 else if ( $.inArray( $( this ).attr( 'id' ), [ 'livetranslatediv', 'siteSub', 'jump-to-nav' ] ) == -1
@@ -188,12 +182,54 @@
189183 handleTranslationCompletion( targetLang );
190184 }
191185
192 - function translateChunk( untranslatedText, chunks, currentMaxSize, sourceLang, targetLang, element ) {
193 - var chunkSize = Math.min( untranslatedText.length, currentMaxSize );
194 -
 186+ function translateChunk( untranslatedScentances, chunks, currentMaxSize, sourceLang, targetLang, element ) {
 187+ var remainingPart = false;
 188+ var partToUse = false;
 189+ var scentanceCount = 0;
 190+ var currentLength = 0;
 191+
 192+ for ( i in untranslatedScentances ) {
 193+ scentanceCount++;
 194+
 195+ if ( currentLength + untranslatedScentances[i].length < currentMaxSize ) {
 196+ currentLength += untranslatedScentances[i].length;
 197+ }
 198+ else if ( untranslatedScentances[i].length > 0 ) {
 199+ if ( currentLength == 0 ) {
 200+ partToUse = untranslatedScentances[i].substr( 0, currentMaxSize - currentLength );
 201+ remainingPart = untranslatedScentances[i].substr( currentMaxSize - currentLength );
 202+ }
 203+
 204+ break;
 205+ }
 206+ }
 207+
 208+ var chunk = '';
 209+
 210+ for ( i = 0; i < scentanceCount; i++ ) {
 211+ var part = untranslatedScentances.shift();
 212+
 213+ if ( i != scentanceCount - 1 || partToUse === false ) {
 214+ chunk += part;
 215+ }
 216+ }
 217+
 218+ if ( remainingPart !== false ) {
 219+ untranslatedScentances.unshift( remainingPart );
 220+ }
 221+
 222+ if ( partToUse !== false ) {
 223+ chunk += partToUse;
 224+ }
 225+
 226+ if ( chunk.length == 0 ) {
 227+ handleTranslationCompletion( targetLang );
 228+ return;
 229+ }
 230+alert(chunk);
195231 google.language.translate(
196232 // Surround the text stuff so spaces and newlines don't get trimmed away.
197 - '|' + untranslatedText.substr( 0, chunkSize ) + '|',
 233+ '|' + chunk + '|',
198234 sourceLang,
199235 targetLang,
200236 function(result) {
@@ -203,10 +239,10 @@
204240 }
205241 else {
206242 // If the translation failed, keep the original text.
207 - chunks.push( untranslatedText.substr( 0, chunkSize ) );
 243+ chunks.push( chunk );
208244 }
209245
210 - if ( chunkSize < currentMaxSize ) {
 246+ if ( untranslatedScentances.length == 0 ) {
211247 // If the current chunk was smaller then the max size, node translation is complete, so update text.
212248 textAreaElement.innerHTML = chunks.join(); // This is a hack to decode quotes.
213249 element.replaceData( 0, element.length, textAreaElement.value );
@@ -214,7 +250,7 @@
215251 }
216252 else {
217253 // If there is more work to do, move on to the next chunk.
218 - translateChunk( untranslatedText.substr( chunkSize ), chunks, currentMaxSize, sourceLang, targetLang, element );
 254+ translateChunk( untranslatedScentances, chunks, currentMaxSize, sourceLang, targetLang, element );
219255 }
220256 }
221257 );

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r79038Improved splitting up of text to send to the GT API.jeroendedauw00:53, 27 December 2010

Comments

#Comment by Jeroen De Dauw (talk | contribs)   14:39, 28 December 2010

Removed debug alert in r79105

Status & tagging log