Index: trunk/extensions/LiveTranslate/includes/ext.livetranslate.js |
— | — | @@ -21,8 +21,10 @@ |
22 | 22 | |
23 | 23 | var runningJobs = 0; |
24 | 24 | |
| 25 | + // This is to enable a hack to decode quotes. |
25 | 26 | var textAreaElement = document.createElement( 'textarea' ); |
26 | 27 | |
| 28 | + // For the "show original" feature. |
27 | 29 | var originalHtml = false; |
28 | 30 | |
29 | 31 | // Compatibility with pre-RL code. |
— | — | @@ -41,6 +43,10 @@ |
42 | 44 | } |
43 | 45 | } |
44 | 46 | |
| 47 | + /** |
| 48 | + * Queries the special words in the source language, finds them in the page, |
| 49 | + * and wraps the into notranslate spans. Then initiates the translation process. |
| 50 | + */ |
45 | 51 | setupTranslationFeatures = function() { |
46 | 52 | $( this ).attr( "disabled", true ).text( mediaWiki.msg( 'livetranslate-button-translating' ) ); |
47 | 53 | |
— | — | @@ -78,6 +84,12 @@ |
79 | 85 | } |
80 | 86 | } |
81 | 87 | |
| 88 | + /** |
| 89 | + * Initiates the translation process. |
| 90 | + * First all special words are found and send to the local API, |
| 91 | + * and then replaced by their translation in the response. Then |
| 92 | + * the Google Translate translation is initiated. |
| 93 | + */ |
82 | 94 | function initiateTranslating() { |
83 | 95 | var words = getSpecialWords(); |
84 | 96 | var newLang = $( '#livetranslatelang' ).val(); |
— | — | @@ -105,6 +117,10 @@ |
106 | 118 | } |
107 | 119 | } |
108 | 120 | |
| 121 | + /** |
| 122 | + * Shows the original page content, simply by setting the html to a stored copy of the original. |
| 123 | + * Also re-binds the jQuery events, as they get lost when doing the html replace. |
| 124 | + */ |
109 | 125 | showOriginal = function() { |
110 | 126 | currentLang = window.sourceLang; |
111 | 127 | $( '#bodyContent' ).html( originalHtml ); |
— | — | @@ -113,9 +129,13 @@ |
114 | 130 | $( '#ltrevertbutton' ).click( showOriginal ); |
115 | 131 | } |
116 | 132 | |
| 133 | + // Initial binding of the button click events. |
117 | 134 | $( '#livetranslatebutton' ).click( setupTranslationFeatures ); |
118 | 135 | $( '#ltrevertbutton' ).click( showOriginal ); |
119 | 136 | |
| 137 | + /** |
| 138 | + * Inserts notranslate spans around the words specified in the passed array in the page content. |
| 139 | + */ |
120 | 140 | function insertNoTranslateTags( words ) { |
121 | 141 | for ( i in words ) { |
122 | 142 | $( '#bodyContent *' ).replaceText( |
— | — | @@ -127,6 +147,12 @@ |
128 | 148 | } |
129 | 149 | } |
130 | 150 | |
| 151 | + /** |
| 152 | + * Finds the special words in the page contents by getting the contents of all |
| 153 | + * notranslate spans and pushing them onto an array. |
| 154 | + * |
| 155 | + * @returns {Array} |
| 156 | + */ |
131 | 157 | function getSpecialWords() { |
132 | 158 | var words = []; |
133 | 159 | |
— | — | @@ -139,19 +165,41 @@ |
140 | 166 | return words; |
141 | 167 | } |
142 | 168 | |
| 169 | + /** |
| 170 | + * Replaced the special words in the page content by looping over them, |
| 171 | + * and checking if there is a matching translation in the provided object. |
| 172 | + * |
| 173 | + * @param translations |
| 174 | + */ |
143 | 175 | function replaceSpecialWords( translations ) { |
144 | 176 | $.each($(".notranslate"), function(i,v) { |
145 | | - var currentText = $(v).text(); |
| 177 | + var currentText = $//(v).text(); |
146 | 178 | if ( translations[currentText] ) { |
147 | 179 | $(v).text( translations[currentText] ); |
148 | 180 | } |
149 | 181 | }); |
150 | 182 | } |
151 | 183 | |
| 184 | + /** |
| 185 | + * Initiates the Google Translate translation. |
| 186 | + * |
| 187 | + * @param sourceLang |
| 188 | + * @param targetLang |
| 189 | + */ |
152 | 190 | function requestGoogleTranslate( sourceLang, targetLang ) { |
153 | 191 | translateElement( $( '#bodyContent' ), sourceLang, targetLang ); |
154 | 192 | } |
155 | 193 | |
| 194 | + /** |
| 195 | + * Translates a single DOM element using Google Translate. |
| 196 | + * Loops through child elements and recursivly calls itself to translate these. |
| 197 | + * |
| 198 | + * TODO: be smarter with the requests, and make sure they don't get broken up unecesarrily. |
| 199 | + * |
| 200 | + * @param element |
| 201 | + * @param sourceLang |
| 202 | + * @param targetLang |
| 203 | + */ |
156 | 204 | function translateElement( element, sourceLang, targetLang ) { |
157 | 205 | runningJobs++; |
158 | 206 | |
— | — | @@ -182,6 +230,17 @@ |
183 | 231 | handleTranslationCompletion( targetLang ); |
184 | 232 | } |
185 | 233 | |
| 234 | + /** |
| 235 | + * Determines a chunk to translate of an DOM elements contents and calls the Google Translate API. |
| 236 | + * Then calls itself if there is any remaining word to be done. |
| 237 | + * |
| 238 | + * @param untranslatedScentances |
| 239 | + * @param chunks |
| 240 | + * @param currentMaxSize |
| 241 | + * @param sourceLang |
| 242 | + * @param targetLang |
| 243 | + * @param element |
| 244 | + */ |
186 | 245 | function translateChunk( untranslatedScentances, chunks, currentMaxSize, sourceLang, targetLang, element ) { |
187 | 246 | var remainingPart = false; |
188 | 247 | var partToUse = false; |
— | — | @@ -226,7 +285,7 @@ |
227 | 286 | handleTranslationCompletion( targetLang ); |
228 | 287 | return; |
229 | 288 | } |
230 | | -alert(chunk); |
| 289 | + |
231 | 290 | google.language.translate( |
232 | 291 | // Surround the text stuff so spaces and newlines don't get trimmed away. |
233 | 292 | '|' + chunk + '|', |
— | — | @@ -256,6 +315,13 @@ |
257 | 316 | ); |
258 | 317 | } |
259 | 318 | |
| 319 | + /** |
| 320 | + * Should be called every time a DOM element has been translated. |
| 321 | + * By use of the runningJobs var, completion of the translation process is detected, |
| 322 | + * and further handled by this function. |
| 323 | + * |
| 324 | + * @param targetLang |
| 325 | + */ |
260 | 326 | function handleTranslationCompletion( targetLang ) { |
261 | 327 | if ( !--runningJobs ) { |
262 | 328 | currentLang = targetLang; |