r57341 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r57340‎ | r57341 | r57342 >
Date:19:05, 3 October 2009
Author:nikerabbit
Status:ok
Tags:
Comment:
Better handling of rails plurals
Modified paths:
  • /trunk/extensions/Translate/FFS.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/FFS.php
@@ -617,20 +617,32 @@
618618 * Converts the special plural syntax to array or ruby style plurals
619619 */
620620 protected function unflattenPlural( $key, $message ) {
621 - $regex = '~\{\{PLURAL\|(.*?)\|}}~s';
622 - $i = 0;
 621+ // Quick escape
 622+ if ( strpos( $message, '{{PLURAL' ) === false ) return false;
 623+
 624+ // Replace all variables with placeholders. Possible source of bugs
 625+ // if other characters that given below are used.
 626+ $regex = '~\{\{a-zA-Z_-}}~';
 627+ $placeholders = array();
 628+ $match = null;
 629+ while ( preg_match( $other, $message, $match ) ) {
 630+ $key = self::placeholder();
 631+ $placeholders[$key] = $match[0];
 632+ $message = preg_replace( $other, $key, $message );
 633+ }
 634+
 635+ // Then replace (possible multiple) plural instances into placeholders
 636+ $regex = '~\{\{PLURAL\|(.*?)}}~s';
623637 $matches = array();
624 - $match = array();
625 - // First replace (possibly multiple ) plural instances into placeholders
 638+ $match = null;
626639 while ( preg_match( $regex, $message, $match ) ) {
627 - $matches[$i] = $match;
628 - $message = preg_replace( $regex, "d__{$i}__b", $message );
629 - $match = array();
630 - $i++;
 640+ $key = self::placeholder();
 641+ $matches[$key] = $match;
 642+ $message = preg_replace( $regex, $key, $message );
631643 }
632644
633 - // No plurals
634 - if ( $i === 0 ) return false;
 645+ // No plurals, should not happen
 646+ if ( !count($matches) ) return false;
635647
636648 // The final array of alternative plurals forms
637649 $alts = array();
@@ -640,7 +652,7 @@
641653 // multiple plural bocks which don't have the same set of keys.
642654 $pluralChoice = implode( '|', array_keys(self::$pluralWords) );
643655 $regex = "~($pluralChoice)\s*=\s*(.+)~s";
644 - foreach ( $matches as $i => $plu ) {
 656+ foreach ( $matches as $ph => $plu ) {
645657 $forms = explode( '|', $plu[1] );
646658 foreach ( $forms as $form ) {
647659 $match = array();
@@ -654,10 +666,20 @@
655667
656668 if ( !isset($alts[$formWord]) ) $alts[$formWord] = $message;
657669 $string = $alts[$formWord];
658 - $alts[$formWord] = str_replace( "d__{$i}__b", $value, $string );
 670+ $alts[$formWord] = str_replace( $ph, $value, $string );
659671 }
660672 }
661673
 674+ // Replace other variables
 675+ foreach ( $alts as &$value ) {
 676+ $value = str_replace( array_keys( $placeholders ), array_values( $placeholders ), $value );
 677+ }
 678+
662679 return $alts;
663680 }
 681+
 682+ protected function placeholder() {
 683+ static $i = 0;
 684+ return "\x7fUNIQ" . dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff)) . '|' . $i++;
 685+ }
664686 }
\ No newline at end of file

Status & tagging log