r78646 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r78645‎ | r78646 | r78647 >
Date:23:20, 20 December 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Switch to v1 of the GT API and added code to walk through the page and find the actual text to translate
Modified paths:
  • /trunk/extensions/LiveTranslate/LiveTranslate.hooks.php (modified) (history)
  • /trunk/extensions/LiveTranslate/includes/LiveTranslate_Functions.php (modified) (history)
  • /trunk/extensions/LiveTranslate/includes/ext.livetranslate.js (modified) (history)

Diff [purge]

Index: trunk/extensions/LiveTranslate/LiveTranslate.hooks.php
@@ -30,11 +30,12 @@
3131
3232 if ( $article->exists() && $title->getFullText() != $egLiveTranslateDirPage ) {
3333 $wgOut->addHTML(
 34+ '<span class="notranslate" id="livetranslatespan">' .
3435 Html::rawElement(
3536 'div',
3637 array(
3738 'id' => 'livetranslatediv',
38 - 'style' => 'display:inline; float:right'
 39+ 'style' => 'display:inline; float:right',
3940 ),
4041 htmlspecialchars( wfMsg( 'livetranslate-translate-to' ) ) .
4142 '&#160;' .
@@ -45,7 +46,8 @@
4647 array( 'id' => 'livetranslatebutton', 'style' => 'height: 27px' ),
4748 wfMsg( 'livetranslate-button-translate' )
4849 )
49 - )
 50+ ) .
 51+ '</span>'
5052 );
5153 }
5254
Index: trunk/extensions/LiveTranslate/includes/ext.livetranslate.js
@@ -26,7 +26,7 @@
2727 }
2828
2929 $('#livetranslatebutton').click(function() {
30 - $( this ).attr( "disabled", true );
 30+ $( this ).attr( "disabled", true );
3131
3232 var words = getSpecialWords();
3333 var newLang = $( '#livetranslatelang' ).val();
@@ -59,7 +59,9 @@
6060 var words = [];
6161
6262 $.each($( '.notranslate' ), function( i, v ) {
63 - words.push( $(v).text() );
 63+ if ( $(v).attr( 'id' ) != 'livetranslatespan' ) {
 64+ words.push( $(v).text() );
 65+ }
6466 });
6567
6668 return words;
@@ -75,36 +77,58 @@
7678 }
7779
7880 function requestGoogleTranslate( sourceLang, targetLang ) {
79 - $.getJSON(
80 - 'https://www.googleapis.com/language/translate/v2?callback=?',
81 - {
82 - 'key': window.wgGoogleApiKey,
83 - 'format': 'html',
84 - 'q': '',//$( '#bodyContent' ).text(),
85 - 'source': sourceLang,
86 - 'target': targetLang,
87 - },
88 - function( response ) {
89 - if ( response.data ) {
90 - for ( i in response.data.translations ) {
91 - // TODO
92 - //alert( response.data.translations[i].translatedText );
93 - }
 81+ translateElement( $( '#bodyContent' ), sourceLang, targetLang );
 82+ }
 83+
 84+ function translateElement( element, sourceLang, targetLang ) {
 85+ if ( element.children().length == 0 ) {
 86+
 87+ }
 88+ else {
 89+ element.children().each( function() {
 90+ if ( $.inArray( $( this ).attr( 'id' ), [ 'livetranslatediv', 'siteSub', 'jump-to-nav' ] ) == -1
 91+ && $.inArray( $( this ).attr( 'class' ), [ 'notranslate', 'printfooter' ] ) == -1
 92+ && $( this ).text().trim().length > 0 ) {
 93+ translateChunk( $( this ).text(), [], 500, sourceLang, targetLang, $( this ) );
9494 }
 95+ } );
 96+ }
 97+ }
 98+
 99+ function translateChunk( untranslatedText, chunks, currentMaxSize, sourceLang, targetLang, element ) {
 100+ var chunkSize = Math.min( untranslatedText.length, currentMaxSize );
 101+
 102+ google.language.translate(
 103+ untranslatedText.substr( 0, chunkSize ),
 104+ sourceLang,
 105+ targetLang,
 106+ function(result) {
 107+ if ( result.error ) {
 108+ if ( result.error.code == '400' ) {
 109+ translateChunk( untranslatedText, chunks, Math.ceil( currentMaxSize / 2 ), sourceLang, targetLang, element );
 110+ }
 111+ }
95112 else {
96 - if ( response.error ) {
97 - alert( response.error.message ); // TODO: i18n
 113+ chunks.push( result.translation );
 114+
 115+ if ( chunkSize < currentMaxSize ) {
 116+ element.text( chunks.join() ); // TODO: set actual content
98117 }
99118 else {
100 - // TODO: unknown error
 119+ translateChunk( untranslatedText.substr( chunkSize ), chunks, currentMaxSize, sourceLang, targetLang, element );
101120 }
102121 }
103 -
104 - currentLang = targetLang;
105 -
106 - $( '#livetranslatebutton' ).attr( "disabled", false );
107122 }
108 - );
 123+ );
109124 }
110125
 126+ function handleTranslationCompletion( translation, targetLang ) {
 127+ alert(translation);
 128+ //$( '#bodyContent' ).innerHTML = result.translation;
 129+
 130+ currentLang = targetLang;
 131+
 132+ $( '#livetranslatebutton' ).attr( "disabled", false );
 133+ }
 134+
111135 } ); })(jQuery);
\ No newline at end of file
Index: trunk/extensions/LiveTranslate/includes/LiveTranslate_Functions.php
@@ -21,8 +21,9 @@
2222 public static function loadJs() {
2323 global $wgOut, $egGoogleApiKey;
2424
25 - $wgOut->addInlineScript(
26 - 'var wgGoogleApiKey = ' . json_encode( $egGoogleApiKey ) . ';'
 25+ $wgOut->addScript(
 26+ Html::linkedScript( 'https://www.google.com/jsapi?key=' . htmlspecialchars( $egGoogleApiKey ) ) .
 27+ Html::inlineScript( 'google.load("language", "1");' )
2728 );
2829
2930 // For backward compatibility with MW < 1.17.

Follow-up revisions

RevisionCommit summaryAuthorDate
r78652Follow up to r78646jeroendedauw00:18, 21 December 2010

Status & tagging log