r81666 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r81665‎ | r81666 | r81667 >
Date:00:39, 8 February 2011
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Attempt to have some magic words to show or hide the translation control. Does not work since the magic words are parsed after the article header hook is called, so needs modification to actually work
Modified paths:
  • /trunk/extensions/LiveTranslate/LiveTranslate.hooks.php (modified) (history)
  • /trunk/extensions/LiveTranslate/LiveTranslate.i18n.magic.php (added) (history)
  • /trunk/extensions/LiveTranslate/LiveTranslate.php (modified) (history)
  • /trunk/extensions/LiveTranslate/RELEASE-NOTES (modified) (history)

Diff [purge]

Index: trunk/extensions/LiveTranslate/LiveTranslate.i18n.magic.php
@@ -0,0 +1,20 @@
 2+<?php
 3+
 4+/**
 5+ * Internationalization file for the magic words in the Live Translate extension.
 6+ *
 7+ * @file LiveTranslate.i18n.magic.php
 8+ * @ingroup LiveTranslate
 9+ *
 10+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 11+ */
 12+
 13+$magicWords = array();
 14+
 15+/** English (English)
 16+ * @author Jeroen De Dauw
 17+ */
 18+$magicWords['en'] = array(
 19+ 'LT_NOTRANSLATIONCONTROL' => array( 0, '__NOTRANSLATIONCONTROL__' ),
 20+ 'LT_SHOWTRANSLATIONCONTROL' => array( 0, '__SHOWTRANSLATIONCONTROL__' ),
 21+);
Property changes on: trunk/extensions/LiveTranslate/LiveTranslate.i18n.magic.php
___________________________________________________________________
Added: svn:eol-style
122 + native
Index: trunk/extensions/LiveTranslate/LiveTranslate.php
@@ -25,7 +25,7 @@
2626 die( 'Not an entry point.' );
2727 }
2828
29 -define( 'LiveTranslate_VERSION', '0.5.1' );
 29+define( 'LiveTranslate_VERSION', '0.6 alpha' );
3030
3131 $wgExtensionCredits['other'][] = array(
3232 'path' => __FILE__,
@@ -44,6 +44,7 @@
4545 unset( $useExtensionPath );
4646
4747 $wgExtensionMessagesFiles['LiveTranslate'] = $egLiveTranslateIP . '/LiveTranslate.i18n.php';
 48+$wgExtensionMessagesFiles['LiveTranslateMagic'] = $egLiveTranslateIP . '/LiveTranslate.i18n.magic.php';
4849
4950 $wgAutoloadClasses['LiveTranslateHooks'] = $egLiveTranslateIP . '/LiveTranslate.hooks.php';
5051 $wgAutoloadClasses['ApiImportTranslationMemories'] = $egLiveTranslateIP . '/api/ApiImportTranslationMemories.php';
@@ -72,6 +73,9 @@
7374 $wgHooks['ArticleViewHeader'][] = 'LiveTranslateHooks::onArticleViewHeader';
7475 $wgHooks['LoadExtensionSchemaUpdates'][] = 'LiveTranslateHooks::onSchemaUpdate';
7576 $wgHooks['ArticleSaveComplete'][] = 'LiveTranslateHooks::onArticleSaveComplete';
 77+$wgHooks['LanguageGetMagic'][] = 'LiveTranslateHooks::addMagicWords';
 78+$wgHooks['InternalParseBeforeLinks'][] = 'LiveTranslateHooks::stripMagicWords';
 79+$wgHooks['OutputPageParserOutput'][] = 'LiveTranslateHooks::onOutputPageParserOutput';
7680
7781 $egLTJSMessages = array(
7882 'livetranslate-button-translate',
@@ -103,4 +107,6 @@
104108 define( 'TMT_TMX', 1 );
105109 define( 'TMT_GCSV', 2 );
106110
 111+$egLiveTranslateMagicWords = array();
 112+
107113 require_once 'LiveTranslate_Settings.php';
Index: trunk/extensions/LiveTranslate/LiveTranslate.hooks.php
@@ -25,7 +25,7 @@
2626 */
2727 public static function onArticleViewHeader( Article &$article, &$outputDone, &$useParserCache ) {
2828 global $egGoogleApiKey, $egLiveTranslateLanguages;
29 -
 29+
3030 $title = $article->getTitle();
3131
3232 $currentLang = LiveTranslateFunctions::getCurrentLang( $title );
@@ -40,13 +40,20 @@
4141 && $article->exists()
4242 && ( count( $egLiveTranslateLanguages ) > 1 || ( count( $egLiveTranslateLanguages ) == 1 && $egLiveTranslateLanguages[0] != $currentLang ) ) ) {
4343
44 - global $egLTNSWithTranslationControl, $egLTUnknownNSShowControl;
45 - $ns = $title->getNamespace();
46 -
47 - if ( ( array_key_exists( $ns, $egLTNSWithTranslationControl ) && $egLTNSWithTranslationControl[$ns] )
48 - || ( !array_key_exists( $ns, $egLTNSWithTranslationControl ) && $egLTUnknownNSShowControl )
49 - ) {
50 - self::displayTranslationControl( $currentLang );
 44+ global $wgParser;
 45+ $po = $wgParser->getOutput();
 46+ $magicWords = isset( $po->mLTMagicWords ) ? $po->mLTMagicWords : array();
 47+
 48+ if ( !in_array( 'LT_NOTRANSLATIONCONTROL', $magicWords ) ) {
 49+ global $egLTNSWithTranslationControl, $egLTUnknownNSShowControl;
 50+ $ns = $title->getNamespace();
 51+
 52+ if ( in_array( 'LT_SHOWTRANSLATIONCONTROL', $magicWords )
 53+ || ( array_key_exists( $ns, $egLTNSWithTranslationControl ) && $egLTNSWithTranslationControl[$ns] )
 54+ || ( !array_key_exists( $ns, $egLTNSWithTranslationControl ) && $egLTUnknownNSShowControl )
 55+ ) {
 56+ self::displayTranslationControl( $currentLang );
 57+ }
5158 }
5259 }
5360
@@ -291,5 +298,61 @@
292299
293300 return true;
294301 }
 302+
 303+ /**
 304+ * Registers the magic words to show and hide the translation control.
 305+ *
 306+ * TODO: apparently there is some new way of doing this for quite a while,
 307+ * which is not linked in the docs. If someone cares to explain the new stuff,
 308+ * I'll be happy to update this.
 309+ *
 310+ * @since 0.6
 311+ *
 312+ * @param array &$magicWords
 313+ * @param string $langCode
 314+ *
 315+ * @return true
 316+ */
 317+ public static function addMagicWords( array &$magicWords, $langCode ) {
 318+ $magicWords['LT_NOTRANSLATIONCONTROL'] = array( 0, '__NOTRANSLATIONCONTROL__' );
 319+ $magicWords['LT_SHOWTRANSLATIONCONTROL'] =array( 0, '__SHOWTRANSLATIONCONTROL__' );
295320
 321+ return true;
 322+ }
 323+
 324+ /**
 325+ * Strips the magic words added by Live Translate from the page text.
 326+ *
 327+ * @since 0.6
 328+ *
 329+ * @param Parser &$parser
 330+ * @param string &$text
 331+ *
 332+ * @return true
 333+ */
 334+ public static function stripMagicWords( Parser &$parser, &$text ) {
 335+ global $egLiveTranslateMagicWords;
 336+
 337+ $mw = MagicWord::get( 'LT_NOTRANSLATIONCONTROL' );
 338+ if ( $mw->matchAndRemove( $text ) ) {
 339+ $egLiveTranslateMagicWords[] = 'LT_NOTRANSLATIONCONTROL';
 340+ }
 341+
 342+ $mw = MagicWord::get( 'LT_SHOWTRANSLATIONCONTROL' );
 343+ if ( $mw->matchAndRemove( $text ) ) {
 344+ $egLiveTranslateMagicWords[] = 'LT_SHOWTRANSLATIONCONTROL';
 345+ }
 346+
 347+ $po = $parser->getOutput();
 348+ $po->mLTMagicWords = $egLiveTranslateMagicWords;
 349+
 350+ return true;
 351+ }
 352+
 353+ public static function onOutputPageParserOutput( $outputpage, $parseroutput ) {
 354+ $magicWords = isset( $parseroutput->mLTMagicWords ) ? $parseroutput->mLTMagicWords : array();
 355+
 356+ return true;
 357+ }
 358+
296359 }
Index: trunk/extensions/LiveTranslate/RELEASE-NOTES
@@ -4,6 +4,11 @@
55 Latest version of the release notes: http://svn.wikimedia.org/viewvc/mediawiki/trunk/extensions/LiveTranslate/RELEASE-NOTES?view=co
66
77
 8+=== Version 0.6 ===
 9+2011-xx-xx
 10+
 11+* Added settings to specify per-namespace if the translation control should be shown or not.
 12+
813 === Version 0.5 ===
914 2011-01-16
1015

Status & tagging log