r79038 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r79037‎ | r79038 | r79039 >
Date:00:53, 27 December 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Improved splitting up of text to send to the GT API.
Modified paths:
  • /trunk/extensions/LiveTranslate/includes/ext.livetranslate.js (modified) (history)

Diff [purge]

Index: trunk/extensions/LiveTranslate/includes/ext.livetranslate.js
@@ -119,7 +119,7 @@
120120 function insertNoTranslateTags( words ) {
121121 for ( i in words ) {
122122 $( '#bodyContent *' ).replaceText(
123 - eval( "/\\b" + words[i] + "\\b/gi" ), // If you know how to kill the evil eval, let me know :)
 123+ new RegExp( "\\b" + words[i] + "\\b", "g" ),
124124 function( str ) {
125125 return '<span class="notranslate">' + str + '</span>'
126126 }
@@ -159,8 +159,23 @@
160160 // If it's a text node, then translate it.
161161 if ( this.nodeType == 3 && this.wholeText.trim().length > 0 ) {
162162 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+
163178 // Initiate translation of the text node. Max chunk size is 500 - 2 for the anti-trim delimiters.
164 - translateChunk( this.wholeText, [], 498, sourceLang, targetLang, this );
 179+ translateChunk( this.wholeText, [], chunk.length, sourceLang, targetLang, this );
165180 }
166181 // If it's an html element, check to see if it should be ignored, and if not, apply function again.
167182 else if ( $.inArray( $( this ).attr( 'id' ), [ 'livetranslatediv', 'siteSub', 'jump-to-nav' ] ) == -1
@@ -175,7 +190,7 @@
176191
177192 function translateChunk( untranslatedText, chunks, currentMaxSize, sourceLang, targetLang, element ) {
178193 var chunkSize = Math.min( untranslatedText.length, currentMaxSize );
179 -
 194+
180195 google.language.translate(
181196 // Surround the text stuff so spaces and newlines don't get trimmed away.
182197 '|' + untranslatedText.substr( 0, chunkSize ) + '|',

Follow-up revisions

RevisionCommit summaryAuthorDate
r79078Follow up to r79038jeroendedauw18:01, 27 December 2010

Status & tagging log