r45657 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r45656‎ | r45657 | r45658 >
Date:10:01, 11 January 2009
Author:nikerabbit
Status:ok
Tags:
Comment:
* Make <plural> do natively what it is almost always used to do
* Switch to date based versioning
Modified paths:
  • /trunk/extensions/I18nTags/I18nTags.php (modified) (history)
  • /trunk/extensions/I18nTags/I18nTags_body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/I18nTags/I18nTags_body.php
@@ -14,11 +14,24 @@
1515 }
1616
1717 public static function plural( $data, $params, $parser ) {
18 - $n = isset($params['n']) ? intval($params['n']) : intval(rand()/rand()*1020);
 18+ list( $from, $to ) = self::getRange( @$params['n'] );
1919 $args = explode('|', $data);
2020 $lang = self::languageObject( $params );
21 - $t = $lang->convertPlural( $n, $args );
22 - return wfMsgReplaceArgs($t, array($n, 'NOT DEFINED'));
 21+
 22+ $format = isset($params['format']) ? $params['format'] : '%s';
 23+ $format = str_replace( '\n', "\n", $format );
 24+
 25+ $s = '';
 26+ for( $i = $from; $i <= $to; $i++ ) {
 27+ $t = $lang->convertPlural( $i, $args );
 28+ $fmtn = $lang->formatNum($i);
 29+ $s .= str_replace(
 30+ array( '%d', '%s'),
 31+ array( $i, wfMsgReplaceArgs( $t, array($fmtn) ) ),
 32+ $format
 33+ );
 34+ }
 35+ return $s;
2336 }
2437
2538 public static function linktrail( $data, $params, $parser ) {
@@ -67,4 +80,21 @@
6881 global $wgContLang;
6982 return isset( $params['lang'] ) ? Language::factory( $params['lang'] ) : $wgContLang;
7083 }
 84+
 85+ public static function getRange( $s, $min = false, $max = false) {
 86+ $matches = array();
 87+ if ( preg_match( '/(\d+)-(\d+)/', $s, $matches ) ) {
 88+ $from = $matches[1];
 89+ $to = $matches[2];
 90+ } else {
 91+ $from = $to = (int) $s;
 92+ }
 93+
 94+
 95+ if ( $from > $to ) {$UNDEFINED = $to; $to = $from; $from = $UNDEFINED;}
 96+ if ( $min !== false ) $from = max( $min, $from );
 97+ if ( $max !== false ) $to = min( $max, $to );
 98+
 99+ return array( $from, $to );
 100+ }
71101 }
\ No newline at end of file
Index: trunk/extensions/I18nTags/I18nTags.php
@@ -15,7 +15,7 @@
1616 'description' => 'Access the i18n functions for number formatting, ' .
1717 'grammar and plural in any available language',
1818 'descriptionmsg' => 'i18ntags-desc',
19 - 'version' => '2.2',
 19+ 'version' => '2009-01-11',
2020 'author' => 'Niklas Laxström',
2121 'url' => 'http://www.mediawiki.org/wiki/Extension:I18nTags',
2222 );

Status & tagging log