r79105 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r79104‎ | r79105 | r79106 >
Date:14:37, 28 December 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Removed debug alert and added a bunch of docs
Modified paths:
  • /trunk/extensions/LiveTranslate/includes/ext.livetranslate.js (modified) (history)

Diff [purge]

Index: trunk/extensions/LiveTranslate/includes/ext.livetranslate.js
@@ -21,8 +21,10 @@
2222
2323 var runningJobs = 0;
2424
 25+ // This is to enable a hack to decode quotes.
2526 var textAreaElement = document.createElement( 'textarea' );
2627
 28+ // For the "show original" feature.
2729 var originalHtml = false;
2830
2931 // Compatibility with pre-RL code.
@@ -41,6 +43,10 @@
4244 }
4345 }
4446
 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+ */
4551 setupTranslationFeatures = function() {
4652 $( this ).attr( "disabled", true ).text( mediaWiki.msg( 'livetranslate-button-translating' ) );
4753
@@ -78,6 +84,12 @@
7985 }
8086 }
8187
 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+ */
8294 function initiateTranslating() {
8395 var words = getSpecialWords();
8496 var newLang = $( '#livetranslatelang' ).val();
@@ -105,6 +117,10 @@
106118 }
107119 }
108120
 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+ */
109125 showOriginal = function() {
110126 currentLang = window.sourceLang;
111127 $( '#bodyContent' ).html( originalHtml );
@@ -113,9 +129,13 @@
114130 $( '#ltrevertbutton' ).click( showOriginal );
115131 }
116132
 133+ // Initial binding of the button click events.
117134 $( '#livetranslatebutton' ).click( setupTranslationFeatures );
118135 $( '#ltrevertbutton' ).click( showOriginal );
119136
 137+ /**
 138+ * Inserts notranslate spans around the words specified in the passed array in the page content.
 139+ */
120140 function insertNoTranslateTags( words ) {
121141 for ( i in words ) {
122142 $( '#bodyContent *' ).replaceText(
@@ -127,6 +147,12 @@
128148 }
129149 }
130150
 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+ */
131157 function getSpecialWords() {
132158 var words = [];
133159
@@ -139,19 +165,41 @@
140166 return words;
141167 }
142168
 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+ */
143175 function replaceSpecialWords( translations ) {
144176 $.each($(".notranslate"), function(i,v) {
145 - var currentText = $(v).text();
 177+ var currentText = $//(v).text();
146178 if ( translations[currentText] ) {
147179 $(v).text( translations[currentText] );
148180 }
149181 });
150182 }
151183
 184+ /**
 185+ * Initiates the Google Translate translation.
 186+ *
 187+ * @param sourceLang
 188+ * @param targetLang
 189+ */
152190 function requestGoogleTranslate( sourceLang, targetLang ) {
153191 translateElement( $( '#bodyContent' ), sourceLang, targetLang );
154192 }
155193
 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+ */
156204 function translateElement( element, sourceLang, targetLang ) {
157205 runningJobs++;
158206
@@ -182,6 +230,17 @@
183231 handleTranslationCompletion( targetLang );
184232 }
185233
 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+ */
186245 function translateChunk( untranslatedScentances, chunks, currentMaxSize, sourceLang, targetLang, element ) {
187246 var remainingPart = false;
188247 var partToUse = false;
@@ -226,7 +285,7 @@
227286 handleTranslationCompletion( targetLang );
228287 return;
229288 }
230 -alert(chunk);
 289+
231290 google.language.translate(
232291 // Surround the text stuff so spaces and newlines don't get trimmed away.
233292 '|' + chunk + '|',
@@ -256,6 +315,13 @@
257316 );
258317 }
259318
 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+ */
260326 function handleTranslationCompletion( targetLang ) {
261327 if ( !--runningJobs ) {
262328 currentLang = targetLang;

Follow-up revisions

RevisionCommit summaryAuthorDate
r79106Follow up to r79105 - fixed evil self-inserting commentjeroendedauw14:40, 28 December 2010

Status & tagging log