Index: trunk/extensions/LiveTranslate/RELEASE-NOTES |
— | — | @@ -4,7 +4,7 @@ |
5 | 5 | Latest version of the release notes: http://svn.wikimedia.org/viewvc/mediawiki/trunk/extensions/LiveTranslate/RELEASE-NOTES?view=co |
6 | 6 | |
7 | 7 | |
8 | | -=== Version 1.1.0 === |
| 8 | +=== Version 1.1 === |
9 | 9 | 2011-06-16 |
10 | 10 | |
11 | 11 | * Added support for the Microsoft Translation service. |
Index: trunk/extensions/LiveTranslate/includes/ext.livetranslate.js |
— | — | @@ -233,10 +233,10 @@ |
234 | 234 | translator.done = handleTranslationCompletion; |
235 | 235 | ltdebug( 'Initiating remote translation' ); |
236 | 236 | translator.translateElement( $( '#bodyContent' ), sourceLang, targetLang ); |
237 | | - ltdebug( 'Remote translation completed' ); |
238 | 237 | } |
239 | 238 | |
240 | 239 | function handleTranslationCompletion( targetLang ) { |
| 240 | + ltdebug( 'Remote translation completed' ); |
241 | 241 | currentLang = targetLang; |
242 | 242 | $( '#livetranslatebutton' ).attr( "disabled", false ).text( mediaWiki.msg( 'livetranslate-button-translate' ) ); |
243 | 243 | $( '#ltrevertbutton' ).css( 'display', 'inline' ); |
Index: trunk/extensions/LiveTranslate/includes/ext.lt.ms.js |
— | — | @@ -13,6 +13,8 @@ |
14 | 14 | this.done = function( targetLang ){}; |
15 | 15 | |
16 | 16 | this.runningJobs = 0; |
| 17 | + this.checkingForIdle = false; |
| 18 | + this.lastCompletion; |
17 | 19 | |
18 | 20 | /** |
19 | 21 | * Determines a chunk to translate of an DOM elements contents and calls the Microsoft Translate API. |
— | — | @@ -76,43 +78,57 @@ |
77 | 79 | this.handleTranslationCompletion( targetLang ); |
78 | 80 | return; |
79 | 81 | } |
80 | | - |
| 82 | + |
81 | 83 | // Keep track of leading and tailing spaces, as they often get modified by the GT API. |
82 | 84 | var leadingSpace = chunk.substr( 0, 1 ) == ' ' ? ' ' : ''; |
83 | 85 | var tailingSpace = ( chunk.length > 1 && chunk.substr( chunk.length - 1, 1 ) == ' ' ) ? ' ' : ''; |
84 | 86 | |
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 ); |
108 | 102 | |
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 ); |
115 | 105 | } |
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 | + |
117 | 133 | } |
118 | 134 | |
119 | 135 | /** |
— | — | @@ -163,6 +179,33 @@ |
164 | 180 | this.handleTranslationCompletion( targetLang ); |
165 | 181 | } |
166 | 182 | |
| 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 | + |
167 | 210 | /** |
168 | 211 | * Should be called every time a DOM element has been translated. |
169 | 212 | * By use of the runningJobs var, completion of the translation process is detected, |
— | — | @@ -171,10 +214,19 @@ |
172 | 215 | * @param {string} targetLang |
173 | 216 | */ |
174 | 217 | 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 | + |
175 | 227 | if ( !--this.runningJobs ) { |
176 | | - ltdebug( 'MS: translation process done' ); |
177 | | - this.done( targetLang ); |
| 228 | + this.invokeDone( targetLang ); |
178 | 229 | } |
179 | 230 | } |
180 | 231 | |
| 232 | + |
181 | 233 | }; })( jQuery ); |
\ No newline at end of file |