Index: trunk/extensions/I18nTags/I18nTags.php |
— | — | @@ -0,0 +1,87 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Some tags to access i18n function in language files |
| 6 | + * |
| 7 | + * @package MediaWiki |
| 8 | + * @subpackage Extensions |
| 9 | + * @author Niklas Laxström |
| 10 | + */ |
| 11 | + |
| 12 | +if( defined( 'MEDIAWIKI' ) ) { |
| 13 | + |
| 14 | + $wgExtensionFunctions[] = 'efSetupPIT'; |
| 15 | + |
| 16 | + $wgExtensionCredits['parserhook'][] = array( |
| 17 | + 'name' => 'Parser i18n tags', |
| 18 | + 'description' => 'Access the i18n functions for number formatting, ' . |
| 19 | + 'grammar and plural in any available language', |
| 20 | + 'version' => '2.1', |
| 21 | + 'author' => 'Niklas Laxström', |
| 22 | + ); |
| 23 | + |
| 24 | + function efSetupPIT() { |
| 25 | + global $wgParser; |
| 26 | + $wgParser->setHook( 'formatnum', 'efFormatnum' ); |
| 27 | + $wgParser->setHook( 'grammar', 'efGrammar' ); |
| 28 | + $wgParser->setHook( 'plural', 'efPlural' ); |
| 29 | + $wgParser->setHook( 'linktrail', 'efLinkTrail' ); |
| 30 | + } |
| 31 | + |
| 32 | + function efFormatnum( $data, $params, $parser ) { |
| 33 | + $lang = efGetLangObject( $params ); |
| 34 | + return $lang->formatNum($data); |
| 35 | + } |
| 36 | + |
| 37 | + function efGrammar( $data, $params, $parser ) { |
| 38 | + if ( isset($params['case']) ) { |
| 39 | + $case = $params['case']; |
| 40 | + } else { |
| 41 | + $case = ""; |
| 42 | + } |
| 43 | + $lang = efGetLangObject( $params ); |
| 44 | + return $lang->convertGrammar($data, $case); |
| 45 | + } |
| 46 | + |
| 47 | + function efPlural( $data, $params, $parser ) { |
| 48 | + if ( isset($params['n']) ) { |
| 49 | + $n = intval($params['n']); |
| 50 | + } else { |
| 51 | + $n = intval(rand()/rand()*1020); |
| 52 | + } |
| 53 | + $args = explode('|', $data); |
| 54 | + while ( count($args) < 5 ) { $args[] = $args[count($args)-1]; } |
| 55 | + $lang = efGetLangObject( $params ); |
| 56 | + $t = $lang->convertPlural( $n, $args[0], $args[1], $args[2], $args[3], $args[4]); |
| 57 | + return wfMsgReplaceArgs($t, array($n, 'NOT DEFINED')); |
| 58 | + } |
| 59 | + |
| 60 | + function efLinkTrail( $data, $params, $parser ) { |
| 61 | + $lang = efGetLangObject( $params ); |
| 62 | + $regex = $lang->linkTrail(); |
| 63 | + |
| 64 | + $inside = ''; |
| 65 | + if ( '' != $data ) { |
| 66 | + $predata = array(); |
| 67 | + preg_match( '/^\[\[([^\]|]+)(\|[^\]]+)?\]\](.*)$/sDu', $data, $predata ); |
| 68 | + $m = array(); |
| 69 | + if ( preg_match( $regex, $predata[3], $m ) ) { |
| 70 | + $inside = $m[1]; |
| 71 | + $data = $m[2]; |
| 72 | + } |
| 73 | + } |
| 74 | + $predata = isset( $predata[2] ) ? $predata[2] : isset( $predata[1] ) ? $predata[1] : $predata[0]; |
| 75 | + return "<b>$predata$inside</b>$data"; |
| 76 | + } |
| 77 | + |
| 78 | + function efGetLangObject( $params ) { |
| 79 | + global $wgContLang; |
| 80 | + return isset( $params['lang'] ) ? Language::factory( $params['lang'] ) : $wgContLang; |
| 81 | + } |
| 82 | + |
| 83 | +} else { |
| 84 | + echo( "This file is an extension to the MediaWiki software and cannot be used standalone.\n" ); |
| 85 | + exit( 1 ); |
| 86 | +} |
| 87 | + |
| 88 | +?> |
\ No newline at end of file |
Property changes on: trunk/extensions/I18nTags/I18nTags.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 89 | + native |