Index: trunk/extensions/LiveTranslate/includes/ext.livetranslate.js |
— | — | @@ -155,27 +155,21 @@ |
156 | 156 | function translateElement( element, sourceLang, targetLang ) { |
157 | 157 | runningJobs++; |
158 | 158 | |
| 159 | + // Max chunk size is 500 - 2 for the anti-trim delimiters. |
| 160 | + var maxChunkLength = 498; |
| 161 | + |
159 | 162 | element.contents().each( function() { |
160 | 163 | // If it's a text node, then translate it. |
161 | 164 | if ( this.nodeType == 3 && this.wholeText.trim().length > 0 ) { |
162 | 165 | 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 | + ); |
180 | 174 | } |
181 | 175 | // If it's an html element, check to see if it should be ignored, and if not, apply function again. |
182 | 176 | else if ( $.inArray( $( this ).attr( 'id' ), [ 'livetranslatediv', 'siteSub', 'jump-to-nav' ] ) == -1 |
— | — | @@ -188,12 +182,54 @@ |
189 | 183 | handleTranslationCompletion( targetLang ); |
190 | 184 | } |
191 | 185 | |
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); |
195 | 231 | google.language.translate( |
196 | 232 | // Surround the text stuff so spaces and newlines don't get trimmed away. |
197 | | - '|' + untranslatedText.substr( 0, chunkSize ) + '|', |
| 233 | + '|' + chunk + '|', |
198 | 234 | sourceLang, |
199 | 235 | targetLang, |
200 | 236 | function(result) { |
— | — | @@ -203,10 +239,10 @@ |
204 | 240 | } |
205 | 241 | else { |
206 | 242 | // If the translation failed, keep the original text. |
207 | | - chunks.push( untranslatedText.substr( 0, chunkSize ) ); |
| 243 | + chunks.push( chunk ); |
208 | 244 | } |
209 | 245 | |
210 | | - if ( chunkSize < currentMaxSize ) { |
| 246 | + if ( untranslatedScentances.length == 0 ) { |
211 | 247 | // If the current chunk was smaller then the max size, node translation is complete, so update text. |
212 | 248 | textAreaElement.innerHTML = chunks.join(); // This is a hack to decode quotes. |
213 | 249 | element.replaceData( 0, element.length, textAreaElement.value ); |
— | — | @@ -214,7 +250,7 @@ |
215 | 251 | } |
216 | 252 | else { |
217 | 253 | // 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 ); |
219 | 255 | } |
220 | 256 | } |
221 | 257 | ); |