Index: trunk/extensions/Translate/FFS.php |
— | — | @@ -617,20 +617,32 @@ |
618 | 618 | * Converts the special plural syntax to array or ruby style plurals |
619 | 619 | */ |
620 | 620 | 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'; |
623 | 637 | $matches = array(); |
624 | | - $match = array(); |
625 | | - // First replace (possibly multiple ) plural instances into placeholders |
| 638 | + $match = null; |
626 | 639 | 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 ); |
631 | 643 | } |
632 | 644 | |
633 | | - // No plurals |
634 | | - if ( $i === 0 ) return false; |
| 645 | + // No plurals, should not happen |
| 646 | + if ( !count($matches) ) return false; |
635 | 647 | |
636 | 648 | // The final array of alternative plurals forms |
637 | 649 | $alts = array(); |
— | — | @@ -640,7 +652,7 @@ |
641 | 653 | // multiple plural bocks which don't have the same set of keys. |
642 | 654 | $pluralChoice = implode( '|', array_keys(self::$pluralWords) ); |
643 | 655 | $regex = "~($pluralChoice)\s*=\s*(.+)~s"; |
644 | | - foreach ( $matches as $i => $plu ) { |
| 656 | + foreach ( $matches as $ph => $plu ) { |
645 | 657 | $forms = explode( '|', $plu[1] ); |
646 | 658 | foreach ( $forms as $form ) { |
647 | 659 | $match = array(); |
— | — | @@ -654,10 +666,20 @@ |
655 | 667 | |
656 | 668 | if ( !isset($alts[$formWord]) ) $alts[$formWord] = $message; |
657 | 669 | $string = $alts[$formWord]; |
658 | | - $alts[$formWord] = str_replace( "d__{$i}__b", $value, $string ); |
| 670 | + $alts[$formWord] = str_replace( $ph, $value, $string ); |
659 | 671 | } |
660 | 672 | } |
661 | 673 | |
| 674 | + // Replace other variables |
| 675 | + foreach ( $alts as &$value ) { |
| 676 | + $value = str_replace( array_keys( $placeholders ), array_values( $placeholders ), $value ); |
| 677 | + } |
| 678 | + |
662 | 679 | return $alts; |
663 | 680 | } |
| 681 | + |
| 682 | + protected function placeholder() { |
| 683 | + static $i = 0; |
| 684 | + return "\x7fUNIQ" . dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff)) . '|' . $i++; |
| 685 | + } |
664 | 686 | } |
\ No newline at end of file |