r80169 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r80168‎ | r80169 | r80170 >
Date:15:56, 13 January 2011
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Some small recactoring and fixed language code normalization on dictionary pages
Modified paths:
  • /trunk/extensions/LiveTranslate/LiveTranslate.hooks.php (modified) (history)

Diff [purge]

Index: trunk/extensions/LiveTranslate/LiveTranslate.hooks.php
@@ -13,8 +13,8 @@
1414 final class LiveTranslateHooks {
1515
1616 /**
17 - * Adds the translation interface to articles.
1817 *
 18+ *
1919 * @since 0.1
2020 *
2121 * @param Article &$article
@@ -24,7 +24,7 @@
2525 * @return true
2626 */
2727 public static function onArticleViewHeader( Article &$article, &$outputDone, &$useParserCache ) {
28 - global $wgOut, $wgLang, $egGoogleApiKey, $egLiveTranslateLanguages;
 28+ global $egGoogleApiKey, $egLiveTranslateLanguages;
2929
3030 $title = $article->getTitle();
3131
@@ -32,99 +32,130 @@
3333
3434 // FIXME: Hitting the db on every page load should be avoided
3535 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.
7938 }
8039 else if (
8140 $egGoogleApiKey != ''
8241 && $article->exists()
8342 && ( 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 - );
11043
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 );
12145 }
12246
12347 return true;
12448 }
12549
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+ }
127109
128110 /**
 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+ '&#160;' .
 130+ LiveTranslateFunctions::getLanguageSelector( $currentLang ) .
 131+ '&#160;' .
 132+ Html::element(
 133+ 'button',
 134+ array( 'id' => 'livetranslatebutton' ),
 135+ wfMsg( 'livetranslate-button-translate' )
 136+ ) .
 137+ '&#160;' .
 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+ /**
129160 * Schema update to set up the needed database tables.
130161 *
131162 * @since 0.1

Status & tagging log