Index: trunk/extensions/LiveTranslate/LiveTranslate.hooks.php |
— | — | @@ -24,10 +24,12 @@ |
25 | 25 | * @return true |
26 | 26 | */ |
27 | 27 | public static function onArticleViewHeader( Article &$article, &$outputDone, &$useParserCache ) { |
28 | | - global $wgOut, $egLiveTranslateDirPage; |
| 28 | + global $wgOut, $egLiveTranslateDirPage, $egGoogleApiKey; |
29 | 29 | |
30 | 30 | $title = $article->getTitle(); |
31 | 31 | |
| 32 | + $currentLang = LiveTranslateFunctions::getCurrentLang( $title ); |
| 33 | + |
32 | 34 | if ( $article->exists() && $title->getFullText() != $egLiveTranslateDirPage ) { |
33 | 35 | $wgOut->addHTML( |
34 | 36 | '<span class="notranslate" id="livetranslatespan">' . |
— | — | @@ -39,7 +41,7 @@ |
40 | 42 | ), |
41 | 43 | htmlspecialchars( wfMsg( 'livetranslate-translate-to' ) ) . |
42 | 44 | ' ' . |
43 | | - self::getLanguageSelector() . |
| 45 | + self::getLanguageSelector( $currentLang ) . |
44 | 46 | ' ' . |
45 | 47 | Html::element( |
46 | 48 | 'button', |
— | — | @@ -51,6 +53,14 @@ |
52 | 54 | ); |
53 | 55 | } |
54 | 56 | |
| 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 | + |
55 | 65 | LiveTranslateFunctions::loadJs(); |
56 | 66 | |
57 | 67 | return true; |
— | — | @@ -61,17 +71,25 @@ |
62 | 72 | * |
63 | 73 | * @since 0.1 |
64 | 74 | * |
| 75 | + * @param string $currentLang |
| 76 | + * |
65 | 77 | * @return string |
66 | 78 | */ |
67 | | - protected static function getLanguageSelector() { |
68 | | - global $wgContLanguageCode; |
| 79 | + protected static function getLanguageSelector( $currentLang ) { |
| 80 | + global $wgUser; |
69 | 81 | |
| 82 | + $targetLang = $currentLang; |
| 83 | + |
70 | 84 | $languages = Language::getLanguageNames( false ); |
71 | 85 | |
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 | + } |
73 | 93 | |
74 | | - $currentLang = array_key_exists( $currentLang, $languages ) ? $currentLang : $wgContLanguageCode; |
75 | | - |
76 | 94 | $options = array(); |
77 | 95 | ksort( $languages ); |
78 | 96 | |
— | — | @@ -86,7 +104,7 @@ |
87 | 105 | 'options' => $options |
88 | 106 | ) ); |
89 | 107 | |
90 | | - return $languageSelector->getInputHTML( $currentLang ); |
| 108 | + return $languageSelector->getInputHTML( $targetLang ); |
91 | 109 | } |
92 | 110 | |
93 | 111 | /** |
— | — | @@ -256,10 +274,10 @@ |
257 | 275 | * @return true |
258 | 276 | */ |
259 | 277 | public static function onOutputPageBeforeHTML( OutputPage &$out, &$text ) { |
260 | | - // TODO: obtain source lang |
261 | | - $sourceLang = 'en'; |
| 278 | + global $wgTitle; |
262 | 279 | |
263 | | - $specialWords = self::getSpecialWordsForLang( $sourceLang ); |
| 280 | + $currentLang = LiveTranslateFunctions::getCurrentLang( $wgTitle ); |
| 281 | + $specialWords = self::getSpecialWordsForLang( $currentLang ); |
264 | 282 | |
265 | 283 | foreach ( $specialWords as $specialWord ) { |
266 | 284 | $text = str_replace( |
Index: trunk/extensions/LiveTranslate/includes/ext.livetranslate.js |
— | — | @@ -7,7 +7,7 @@ |
8 | 8 | |
9 | 9 | (function($) { $( document ).ready( function() { |
10 | 10 | |
11 | | - var currentLang = 'en'; // TODO |
| 11 | + var currentLang = window.sourceLang; |
12 | 12 | |
13 | 13 | var runningJobs = 0; |
14 | 14 | |
— | — | @@ -54,7 +54,6 @@ |
55 | 55 | } |
56 | 56 | ); |
57 | 57 | } |
58 | | - |
59 | 58 | }); |
60 | 59 | |
61 | 60 | function getSpecialWords() { |
— | — | @@ -89,6 +88,7 @@ |
90 | 89 | // If it's a text node, then translate it. |
91 | 90 | if ( this.nodeType == 3 && this.wholeText.trim().length > 0 ) { |
92 | 91 | runningJobs++; |
| 92 | + // Initiate translation of the text node. Max chunk size is 500 - 2 for the anti-trim delimiters. |
93 | 93 | translateChunk( this.wholeText, [], 498, sourceLang, targetLang, this ); |
94 | 94 | } |
95 | 95 | // If it's an html element, check to see if it should be ignored, and if not, apply function again. |
— | — | @@ -111,16 +111,22 @@ |
112 | 112 | sourceLang, |
113 | 113 | targetLang, |
114 | 114 | function(result) { |
115 | | - if ( result.translation.length >= 2 ) { |
| 115 | + if ( result.translation && result.translation.length >= 2 ) { |
116 | 116 | // Remove the trim-preventing stuff and add the result to the chunks array. |
117 | 117 | chunks.push( result.translation.substr( 1, result.translation.length -2 ) ); |
118 | 118 | } |
| 119 | + else { |
| 120 | + // If the translation failed, keep the original text. |
| 121 | + chunks.push( untranslatedText.substr( 0, chunkSize ) ); |
| 122 | + } |
119 | 123 | |
120 | 124 | if ( chunkSize < currentMaxSize ) { |
| 125 | + // If the current chunk was smaller then the max size, node translation is complete, so update text. |
121 | 126 | element.replaceWholeText( chunks.join() ); |
122 | 127 | handleTranslationCompletion( targetLang ); |
123 | 128 | } |
124 | 129 | else { |
| 130 | + // If there is more work to do, move on to the next chunk. |
125 | 131 | translateChunk( untranslatedText.substr( chunkSize ), chunks, currentMaxSize, sourceLang, targetLang, element ); |
126 | 132 | } |
127 | 133 | } |
Index: trunk/extensions/LiveTranslate/includes/LiveTranslate_Functions.php |
— | — | @@ -19,13 +19,8 @@ |
20 | 20 | * @since 0.1 |
21 | 21 | */ |
22 | 22 | public static function loadJs() { |
23 | | - global $wgOut, $egGoogleApiKey; |
| 23 | + global $wgOut; |
24 | 24 | |
25 | | - $wgOut->addScript( |
26 | | - Html::linkedScript( 'https://www.google.com/jsapi?key=' . htmlspecialchars( $egGoogleApiKey ) ) . |
27 | | - Html::inlineScript( 'google.load("language", "1");' ) |
28 | | - ); |
29 | | - |
30 | 25 | // For backward compatibility with MW < 1.17. |
31 | 26 | if ( is_callable( array( $wgOut, 'addModules' ) ) ) { |
32 | 27 | $wgOut->addModules( 'ext.livetranslate' ); |
— | — | @@ -60,6 +55,24 @@ |
61 | 56 | } |
62 | 57 | |
63 | 58 | $wgOut->addInlineScript( 'var wgLTEMessages = ' . json_encode( $data ) . ';' ); |
64 | | - } |
| 59 | + } |
65 | 60 | |
| 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 | + |
66 | 79 | } |
\ No newline at end of file |