Index: trunk/extensions/LiveTranslate/LiveTranslate.hooks.php |
— | — | @@ -13,8 +13,8 @@ |
14 | 14 | final class LiveTranslateHooks { |
15 | 15 | |
16 | 16 | /** |
17 | | - * Adds the translation interface to articles. |
18 | 17 | * |
| 18 | + * |
19 | 19 | * @since 0.1 |
20 | 20 | * |
21 | 21 | * @param Article &$article |
— | — | @@ -24,7 +24,7 @@ |
25 | 25 | * @return true |
26 | 26 | */ |
27 | 27 | public static function onArticleViewHeader( Article &$article, &$outputDone, &$useParserCache ) { |
28 | | - global $wgOut, $wgLang, $egGoogleApiKey, $egLiveTranslateLanguages; |
| 28 | + global $egGoogleApiKey, $egLiveTranslateLanguages; |
29 | 29 | |
30 | 30 | $title = $article->getTitle(); |
31 | 31 | |
— | — | @@ -32,99 +32,130 @@ |
33 | 33 | |
34 | 34 | // FIXME: Hitting the db on every page load should be avoided |
35 | 35 | if ( in_array( $title->getFullText(), LiveTranslateFunctions::getLocalMemoryNames() ) ) { |
36 | | - $parser = LTTMParser::newFromType( LiveTranslateFunctions::getMemoryType( $title->getFullText() ) ); |
37 | | - $tm = $parser->parse( $article->getContent() ); |
38 | | - $tus = $tm->getTranslationUnits(); |
39 | | - |
40 | | - if ( count( $tus ) == 0 ) { |
41 | | - $wgOut->addWikiMsg( 'livetranslate-dictionary-empty' ); |
42 | | - } |
43 | | - else { |
44 | | - $wgOut->addWikiMsg( |
45 | | - 'livetranslate-dictionary-count', |
46 | | - $wgLang->formatNum( count( $tus ) ) , |
47 | | - $wgLang->formatNum( count( $tus[0]->getVariants() ) ) |
48 | | - ); |
49 | | - |
50 | | - $notAllowedLanguages = array(); |
51 | | - |
52 | | - foreach ( $tus[0]->getVariants() as $languageCode => $translations ) { |
53 | | - if ( !in_array( $languageCode, $egLiveTranslateLanguages ) ) { |
54 | | - $notAllowedLanguages[] = $languageCode; |
55 | | - } |
56 | | - } |
57 | | - |
58 | | - if ( count( $notAllowedLanguages ) > 0 ) { |
59 | | - $languages = Language::getLanguageNames( false ); |
60 | | - |
61 | | - foreach ( $notAllowedLanguages as &$notAllowedLang ) { |
62 | | - if ( array_key_exists( $notAllowedLang, $languages ) ) { |
63 | | - $notAllowedLang = $languages[$notAllowedLang]; |
64 | | - } |
65 | | - } |
66 | | - |
67 | | - $wgOut->addHTML( |
68 | | - Html::element( |
69 | | - 'span', |
70 | | - array( 'style' => 'color:darkred' ), |
71 | | - wfMsgExt( 'livetranslate-dictionary-unallowed-langs', 'parsemag', $wgLang->listToText( $notAllowedLanguages ), count( $notAllowedLanguages ) ) |
72 | | - ) |
73 | | - |
74 | | - ); |
75 | | - } |
76 | | - } |
77 | | - |
78 | | - $outputDone = true; |
| 36 | + self::displayDictionaryPage( $article, $title ); |
| 37 | + $outputDone = true; // The translations themselves should not be shown. |
79 | 38 | } |
80 | 39 | else if ( |
81 | 40 | $egGoogleApiKey != '' |
82 | 41 | && $article->exists() |
83 | 42 | && ( count( $egLiveTranslateLanguages ) > 1 || ( count( $egLiveTranslateLanguages ) == 1 && $egLiveTranslateLanguages[0] != $currentLang ) ) ) { |
84 | | - $wgOut->addHTML( |
85 | | - Html::rawElement( |
86 | | - 'div', |
87 | | - array( |
88 | | - 'id' => 'livetranslatediv', |
89 | | - 'style' => 'display:inline; float:right', |
90 | | - 'class' => 'notranslate' |
91 | | - ), |
92 | | - htmlspecialchars( wfMsg( 'livetranslate-translate-to' ) ) . |
93 | | - ' ' . |
94 | | - LiveTranslateFunctions::getLanguageSelector( $currentLang ) . |
95 | | - ' ' . |
96 | | - Html::element( |
97 | | - 'button', |
98 | | - array( 'id' => 'livetranslatebutton' ), |
99 | | - wfMsg( 'livetranslate-button-translate' ) |
100 | | - ) . |
101 | | - ' ' . |
102 | | - Html::element( |
103 | | - 'button', |
104 | | - array( 'id' => 'ltrevertbutton', 'style' => 'display:none' ), |
105 | | - wfMsg( 'livetranslate-button-revert' ) |
106 | | - ) |
107 | | - ) . |
108 | | - '<br /><br /><div id="googlebranding" style="display:inline; float:right"></div>' |
109 | | - ); |
110 | 43 | |
111 | | - $wgOut->addScript( |
112 | | - Html::linkedScript( 'https://www.google.com/jsapi?key=' . htmlspecialchars( $egGoogleApiKey ) ) . |
113 | | - Html::inlineScript( |
114 | | - 'google.load("language", "1"); |
115 | | - google.setOnLoadCallback(function(){google.language.getBranding("googlebranding");});' . |
116 | | - 'var sourceLang = ' . json_encode( $currentLang ) . ';' |
117 | | - ) |
118 | | - ); |
119 | | - |
120 | | - LiveTranslateFunctions::loadJs(); |
| 44 | + self::displayTranslationControl( $currentLang ); |
121 | 45 | } |
122 | 46 | |
123 | 47 | return true; |
124 | 48 | } |
125 | 49 | |
126 | | - |
| 50 | + /** |
| 51 | + * Displays some shorts statistics about the dictionary page. |
| 52 | + * |
| 53 | + * @since 0.4 |
| 54 | + * |
| 55 | + * @param Article $article |
| 56 | + * @param Title $title |
| 57 | + */ |
| 58 | + protected static function displayDictionaryPage( Article &$article, Title $title ) { |
| 59 | + global $wgOut, $wgLang, $egLiveTranslateLanguages; |
| 60 | + |
| 61 | + $parser = LTTMParser::newFromType( LiveTranslateFunctions::getMemoryType( $title->getFullText() ) ); |
| 62 | + $tm = $parser->parse( $article->getContent() ); |
| 63 | + $tus = $tm->getTranslationUnits(); |
| 64 | + |
| 65 | + if ( count( $tus ) == 0 ) { |
| 66 | + $wgOut->addWikiMsg( 'livetranslate-dictionary-empty' ); |
| 67 | + } |
| 68 | + else { |
| 69 | + $wgOut->addWikiMsg( |
| 70 | + 'livetranslate-dictionary-count', |
| 71 | + $wgLang->formatNum( count( $tus ) ) , |
| 72 | + $wgLang->formatNum( count( $tus[0]->getVariants() ) ) |
| 73 | + ); |
| 74 | + |
| 75 | + $notAllowedLanguages = array(); |
| 76 | + |
| 77 | + foreach ( $tus[0]->getVariants() as $languageCode => $translations ) { |
| 78 | + $languageCode = strtolower( $languageCode ); |
| 79 | + $mappings = LiveTranslateFunctions::getInputLangMapping(); |
| 80 | + |
| 81 | + if ( array_key_exists( $languageCode, $mappings ) ) { |
| 82 | + $languageCode = $mappings[$languageCode]; |
| 83 | + } |
| 84 | + |
| 85 | + if ( !in_array( $languageCode, $egLiveTranslateLanguages ) ) { |
| 86 | + $notAllowedLanguages[] = $languageCode; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if ( count( $notAllowedLanguages ) > 0 ) { |
| 91 | + $languages = Language::getLanguageNames( false ); |
| 92 | + |
| 93 | + foreach ( $notAllowedLanguages as &$notAllowedLang ) { |
| 94 | + if ( array_key_exists( $notAllowedLang, $languages ) ) { |
| 95 | + $notAllowedLang = $languages[$notAllowedLang]; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + $wgOut->addHTML( |
| 100 | + Html::element( |
| 101 | + 'span', |
| 102 | + array( 'style' => 'color:darkred' ), |
| 103 | + wfMsgExt( 'livetranslate-dictionary-unallowed-langs', 'parsemag', $wgLang->listToText( $notAllowedLanguages ), count( $notAllowedLanguages ) ) |
| 104 | + ) |
| 105 | + ); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
127 | 109 | |
128 | 110 | /** |
| 111 | + * Outputs the Live Translate translation control. |
| 112 | + * |
| 113 | + * @since 0.4 |
| 114 | + * |
| 115 | + * @param string $currentLang |
| 116 | + */ |
| 117 | + protected static function displayTranslationControl( $currentLang ) { |
| 118 | + global $wgOut, $egGoogleApiKey; |
| 119 | + |
| 120 | + $wgOut->addHTML( |
| 121 | + Html::rawElement( |
| 122 | + 'div', |
| 123 | + array( |
| 124 | + 'id' => 'livetranslatediv', |
| 125 | + 'style' => 'display:inline; float:right', |
| 126 | + 'class' => 'notranslate' |
| 127 | + ), |
| 128 | + htmlspecialchars( wfMsg( 'livetranslate-translate-to' ) ) . |
| 129 | + ' ' . |
| 130 | + LiveTranslateFunctions::getLanguageSelector( $currentLang ) . |
| 131 | + ' ' . |
| 132 | + Html::element( |
| 133 | + 'button', |
| 134 | + array( 'id' => 'livetranslatebutton' ), |
| 135 | + wfMsg( 'livetranslate-button-translate' ) |
| 136 | + ) . |
| 137 | + ' ' . |
| 138 | + Html::element( |
| 139 | + 'button', |
| 140 | + array( 'id' => 'ltrevertbutton', 'style' => 'display:none' ), |
| 141 | + wfMsg( 'livetranslate-button-revert' ) |
| 142 | + ) |
| 143 | + ) . |
| 144 | + '<br /><br /><div id="googlebranding" style="display:inline; float:right"></div>' |
| 145 | + ); |
| 146 | + |
| 147 | + $wgOut->addScript( |
| 148 | + Html::linkedScript( 'https://www.google.com/jsapi?key=' . htmlspecialchars( $egGoogleApiKey ) ) . |
| 149 | + Html::inlineScript( |
| 150 | + 'google.load("language", "1"); |
| 151 | + google.setOnLoadCallback(function(){google.language.getBranding("googlebranding");});' . |
| 152 | + 'var sourceLang = ' . json_encode( $currentLang ) . ';' |
| 153 | + ) |
| 154 | + ); |
| 155 | + |
| 156 | + LiveTranslateFunctions::loadJs(); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
129 | 160 | * Schema update to set up the needed database tables. |
130 | 161 | * |
131 | 162 | * @since 0.1 |