Index: trunk/extensions/LiveTranslate/LiveTranslate.hooks.php |
— | — | @@ -30,11 +30,12 @@ |
31 | 31 | |
32 | 32 | if ( $article->exists() && $title->getFullText() != $egLiveTranslateDirPage ) { |
33 | 33 | $wgOut->addHTML( |
| 34 | + '<span class="notranslate" id="livetranslatespan">' . |
34 | 35 | Html::rawElement( |
35 | 36 | 'div', |
36 | 37 | array( |
37 | 38 | 'id' => 'livetranslatediv', |
38 | | - 'style' => 'display:inline; float:right' |
| 39 | + 'style' => 'display:inline; float:right', |
39 | 40 | ), |
40 | 41 | htmlspecialchars( wfMsg( 'livetranslate-translate-to' ) ) . |
41 | 42 | ' ' . |
— | — | @@ -45,7 +46,8 @@ |
46 | 47 | array( 'id' => 'livetranslatebutton', 'style' => 'height: 27px' ), |
47 | 48 | wfMsg( 'livetranslate-button-translate' ) |
48 | 49 | ) |
49 | | - ) |
| 50 | + ) . |
| 51 | + '</span>' |
50 | 52 | ); |
51 | 53 | } |
52 | 54 | |
Index: trunk/extensions/LiveTranslate/includes/ext.livetranslate.js |
— | — | @@ -26,7 +26,7 @@ |
27 | 27 | } |
28 | 28 | |
29 | 29 | $('#livetranslatebutton').click(function() { |
30 | | - $( this ).attr( "disabled", true ); |
| 30 | + $( this ).attr( "disabled", true ); |
31 | 31 | |
32 | 32 | var words = getSpecialWords(); |
33 | 33 | var newLang = $( '#livetranslatelang' ).val(); |
— | — | @@ -59,7 +59,9 @@ |
60 | 60 | var words = []; |
61 | 61 | |
62 | 62 | $.each($( '.notranslate' ), function( i, v ) { |
63 | | - words.push( $(v).text() ); |
| 63 | + if ( $(v).attr( 'id' ) != 'livetranslatespan' ) { |
| 64 | + words.push( $(v).text() ); |
| 65 | + } |
64 | 66 | }); |
65 | 67 | |
66 | 68 | return words; |
— | — | @@ -75,36 +77,58 @@ |
76 | 78 | } |
77 | 79 | |
78 | 80 | 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 ) ); |
94 | 94 | } |
| 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 | + } |
95 | 112 | 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 |
98 | 117 | } |
99 | 118 | else { |
100 | | - // TODO: unknown error |
| 119 | + translateChunk( untranslatedText.substr( chunkSize ), chunks, currentMaxSize, sourceLang, targetLang, element ); |
101 | 120 | } |
102 | 121 | } |
103 | | - |
104 | | - currentLang = targetLang; |
105 | | - |
106 | | - $( '#livetranslatebutton' ).attr( "disabled", false ); |
107 | 122 | } |
108 | | - ); |
| 123 | + ); |
109 | 124 | } |
110 | 125 | |
| 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 | + |
111 | 135 | } ); })(jQuery); |
\ No newline at end of file |
Index: trunk/extensions/LiveTranslate/includes/LiveTranslate_Functions.php |
— | — | @@ -21,8 +21,9 @@ |
22 | 22 | public static function loadJs() { |
23 | 23 | global $wgOut, $egGoogleApiKey; |
24 | 24 | |
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");' ) |
27 | 28 | ); |
28 | 29 | |
29 | 30 | // For backward compatibility with MW < 1.17. |