Index: trunk/extensions/Multilang/Multilang.php |
— | — | @@ -0,0 +1,29 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Adapted form of the MultiLang extension from Arnomane |
| 6 | + * |
| 7 | + * @package MediaWiki |
| 8 | + * @subpackage Extensions |
| 9 | + * @author Rob Church <robchur@gmail.com> |
| 10 | + */ |
| 11 | + |
| 12 | +if( defined( 'MEDIAWIKI' ) ) { |
| 13 | + |
| 14 | + $wgAutoloadClasses['Multilang'] = dirname( __FILE__ ) . '/Multilang.class.php'; |
| 15 | + $wgExtensionFunctions[] = 'efMultilang'; |
| 16 | + |
| 17 | + function efMultilang() { |
| 18 | + global $wgMultilang, $wgParser, $wgHooks; |
| 19 | + # Use of a StubObject means we can have a single, persistent instance |
| 20 | + # that will remember what's going on between parse runs, and we can |
| 21 | + # defer initialisation until we need to call a hook function |
| 22 | + $wgMultilang = new StubObject( 'wgMultilang', 'Multilang' ); |
| 23 | + $wgParser->setHook( 'language', array( &$wgMultilang, 'languageBlock' ) ); |
| 24 | + $wgParser->setHook( 'multilang', array( &$wgMultilang, 'outputBlock' ) ); |
| 25 | + $wgHooks['ParserClearState'][] = array( &$wgMultilang, 'clearState' ); |
| 26 | + } |
| 27 | + |
| 28 | +} |
| 29 | + |
| 30 | +?> |
\ No newline at end of file |
Property changes on: trunk/extensions/Multilang/Multilang.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 31 | + native |
Index: trunk/extensions/Multilang/Multilang.class.php |
— | — | @@ -0,0 +1,98 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +class Multilang { |
| 5 | + |
| 6 | + /** |
| 7 | + * Alternative text blocks |
| 8 | + * Index is the language code to which it corresponds |
| 9 | + */ |
| 10 | + private $text = array(); |
| 11 | + |
| 12 | + /** |
| 13 | + * Fallback language |
| 14 | + */ |
| 15 | + private $fallback = ''; |
| 16 | + |
| 17 | + /** |
| 18 | + * Register a new alternative text block |
| 19 | + * (Handles the parser hook for <language></language>) |
| 20 | + * |
| 21 | + * @param $text Content |
| 22 | + * @param $args Tag attributes |
| 23 | + * @param $parser Parent parser |
| 24 | + * @return string |
| 25 | + */ |
| 26 | + public function languageBlock( $text, $args, &$parser ) { |
| 27 | + if( isset( $args['lang'] ) ) { |
| 28 | + $lang = strtolower( $args['lang'] ); |
| 29 | + $this->text[$lang] = $text; |
| 30 | + $this->updateFallback( $lang ); |
| 31 | + } else { |
| 32 | + # Disaster! We *have* to know the language code, otherwise |
| 33 | + # we have no idea when to show the text in question |
| 34 | + } |
| 35 | + return ''; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Output the appropriate text block |
| 40 | + * (Handles the parser hook for <multilang />) |
| 41 | + * |
| 42 | + * @param $text Content |
| 43 | + * @param $args Tag attributes |
| 44 | + * @param $parser Parent parser |
| 45 | + * @return string |
| 46 | + */ |
| 47 | + public function outputBlock( $text, $args, &$parser ) { |
| 48 | + # This is a bit of a hack; the fact that the cache hash stores |
| 49 | + # the language code implies that this should be better exposed |
| 50 | + $lang = $parser->mOptions->mUser->getOption( 'language' ); |
| 51 | + $text = $this->getText( $lang ); |
| 52 | + $output = $parser->parse( $text, $parser->mTitle, $parser->mOptions, true, false ); |
| 53 | + return $output->getText(); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Get the text block corresponding to the requested language code, |
| 58 | + * or the fallback if needed |
| 59 | + * |
| 60 | + * @param $lang Language code |
| 61 | + * @return string |
| 62 | + */ |
| 63 | + private function getText( $lang ) { |
| 64 | + return isset( $this->text[$lang] ) ? $this->text[$lang] : $this->getFallback(); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Get the fallback text |
| 69 | + * |
| 70 | + * @return string |
| 71 | + */ |
| 72 | + private function getFallback() { |
| 73 | + return isset( $this->text[$this->fallback] ) ? $this->text[$this->fallback] : ''; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * When a new text block is made available, look at the language |
| 78 | + * code, and see if it's a better fallback than the one we have |
| 79 | + * |
| 80 | + * @param $lang Language code |
| 81 | + */ |
| 82 | + private function updateFallback( $lang ) { |
| 83 | + global $wgContLang; |
| 84 | + if( $this->fallback == '' || $lang == $wgContLang->getCode() ) { |
| 85 | + $this->fallback = $lang; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Clear all internal state information |
| 91 | + */ |
| 92 | + public function clearState() { |
| 93 | + $this->text = array(); |
| 94 | + $this->fallback = ''; |
| 95 | + } |
| 96 | + |
| 97 | +} |
| 98 | + |
| 99 | +?> |
\ No newline at end of file |
Property changes on: trunk/extensions/Multilang/Multilang.class.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 100 | + native |