r78675 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r78674‎ | r78675 | r78676 >
Date:14:28, 21 December 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Added detection of source and destination language
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
@@ -24,10 +24,12 @@
2525 * @return true
2626 */
2727 public static function onArticleViewHeader( Article &$article, &$outputDone, &$useParserCache ) {
28 - global $wgOut, $egLiveTranslateDirPage;
 28+ global $wgOut, $egLiveTranslateDirPage, $egGoogleApiKey;
2929
3030 $title = $article->getTitle();
3131
 32+ $currentLang = LiveTranslateFunctions::getCurrentLang( $title );
 33+
3234 if ( $article->exists() && $title->getFullText() != $egLiveTranslateDirPage ) {
3335 $wgOut->addHTML(
3436 '<span class="notranslate" id="livetranslatespan">' .
@@ -39,7 +41,7 @@
4042 ),
4143 htmlspecialchars( wfMsg( 'livetranslate-translate-to' ) ) .
4244 '&#160;' .
43 - self::getLanguageSelector() .
 45+ self::getLanguageSelector( $currentLang ) .
4446 '&#160;' .
4547 Html::element(
4648 'button',
@@ -51,6 +53,14 @@
5254 );
5355 }
5456
 57+ $wgOut->addScript(
 58+ Html::linkedScript( 'https://www.google.com/jsapi?key=' . htmlspecialchars( $egGoogleApiKey ) ) .
 59+ Html::inlineScript(
 60+ 'google.load("language", "1");' .
 61+ 'var sourceLang = ' . json_encode( $currentLang ) . ';'
 62+ )
 63+ );
 64+
5565 LiveTranslateFunctions::loadJs();
5666
5767 return true;
@@ -61,17 +71,25 @@
6272 *
6373 * @since 0.1
6474 *
 75+ * @param string $currentLang
 76+ *
6577 * @return string
6678 */
67 - protected static function getLanguageSelector() {
68 - global $wgContLanguageCode;
 79+ protected static function getLanguageSelector( $currentLang ) {
 80+ global $wgUser;
6981
 82+ $targetLang = $currentLang;
 83+
7084 $languages = Language::getLanguageNames( false );
7185
72 - $currentLang = 'en'; // TODO
 86+ if ( $wgUser->isLoggedIn() ) {
 87+ $userLang = $wgUser->getOption( 'language' );
 88+
 89+ if ( array_key_exists( $userLang, $languages ) ) {
 90+ $targetLang = $userLang;
 91+ }
 92+ }
7393
74 - $currentLang = array_key_exists( $currentLang, $languages ) ? $currentLang : $wgContLanguageCode;
75 -
7694 $options = array();
7795 ksort( $languages );
7896
@@ -86,7 +104,7 @@
87105 'options' => $options
88106 ) );
89107
90 - return $languageSelector->getInputHTML( $currentLang );
 108+ return $languageSelector->getInputHTML( $targetLang );
91109 }
92110
93111 /**
@@ -256,10 +274,10 @@
257275 * @return true
258276 */
259277 public static function onOutputPageBeforeHTML( OutputPage &$out, &$text ) {
260 - // TODO: obtain source lang
261 - $sourceLang = 'en';
 278+ global $wgTitle;
262279
263 - $specialWords = self::getSpecialWordsForLang( $sourceLang );
 280+ $currentLang = LiveTranslateFunctions::getCurrentLang( $wgTitle );
 281+ $specialWords = self::getSpecialWordsForLang( $currentLang );
264282
265283 foreach ( $specialWords as $specialWord ) {
266284 $text = str_replace(
Index: trunk/extensions/LiveTranslate/includes/ext.livetranslate.js
@@ -7,7 +7,7 @@
88
99 (function($) { $( document ).ready( function() {
1010
11 - var currentLang = 'en'; // TODO
 11+ var currentLang = window.sourceLang;
1212
1313 var runningJobs = 0;
1414
@@ -54,7 +54,6 @@
5555 }
5656 );
5757 }
58 -
5958 });
6059
6160 function getSpecialWords() {
@@ -89,6 +88,7 @@
9089 // If it's a text node, then translate it.
9190 if ( this.nodeType == 3 && this.wholeText.trim().length > 0 ) {
9291 runningJobs++;
 92+ // Initiate translation of the text node. Max chunk size is 500 - 2 for the anti-trim delimiters.
9393 translateChunk( this.wholeText, [], 498, sourceLang, targetLang, this );
9494 }
9595 // If it's an html element, check to see if it should be ignored, and if not, apply function again.
@@ -111,16 +111,22 @@
112112 sourceLang,
113113 targetLang,
114114 function(result) {
115 - if ( result.translation.length >= 2 ) {
 115+ if ( result.translation && result.translation.length >= 2 ) {
116116 // Remove the trim-preventing stuff and add the result to the chunks array.
117117 chunks.push( result.translation.substr( 1, result.translation.length -2 ) );
118118 }
 119+ else {
 120+ // If the translation failed, keep the original text.
 121+ chunks.push( untranslatedText.substr( 0, chunkSize ) );
 122+ }
119123
120124 if ( chunkSize < currentMaxSize ) {
 125+ // If the current chunk was smaller then the max size, node translation is complete, so update text.
121126 element.replaceWholeText( chunks.join() );
122127 handleTranslationCompletion( targetLang );
123128 }
124129 else {
 130+ // If there is more work to do, move on to the next chunk.
125131 translateChunk( untranslatedText.substr( chunkSize ), chunks, currentMaxSize, sourceLang, targetLang, element );
126132 }
127133 }
Index: trunk/extensions/LiveTranslate/includes/LiveTranslate_Functions.php
@@ -19,13 +19,8 @@
2020 * @since 0.1
2121 */
2222 public static function loadJs() {
23 - global $wgOut, $egGoogleApiKey;
 23+ global $wgOut;
2424
25 - $wgOut->addScript(
26 - Html::linkedScript( 'https://www.google.com/jsapi?key=' . htmlspecialchars( $egGoogleApiKey ) ) .
27 - Html::inlineScript( 'google.load("language", "1");' )
28 - );
29 -
3025 // For backward compatibility with MW < 1.17.
3126 if ( is_callable( array( $wgOut, 'addModules' ) ) ) {
3227 $wgOut->addModules( 'ext.livetranslate' );
@@ -60,6 +55,24 @@
6156 }
6257
6358 $wgOut->addInlineScript( 'var wgLTEMessages = ' . json_encode( $data ) . ';' );
64 - }
 59+ }
6560
 61+ /**
 62+ * Returns the language code for a title.
 63+ *
 64+ * @param Title $title
 65+ *
 66+ * @return string
 67+ */
 68+ public static function getCurrentLang( Title $title ) {
 69+ $subPage = array_pop( explode( '/', $title->getSubpageText() ) );
 70+
 71+ if ( $subPage != '' && array_key_exists( $subPage, Language::getLanguageNames( false ) ) ) {
 72+ return $subPage;
 73+ }
 74+
 75+ global $wgLanguageCode;
 76+ return $wgLanguageCode;
 77+ }
 78+
6679 }
\ No newline at end of file

Follow-up revisions

RevisionCommit summaryAuthorDate
r78678Follow up to r78675jeroendedauw15:19, 21 December 2010

Status & tagging log