Fix gettext's plural forms for more than 2 plural forms. Props moeffju and nbachiyski. fixes #4005

git-svn-id: http://svn.automattic.com/wordpress/trunk@5266 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
rob1n 2007-04-13 23:29:47 +00:00
parent 24e1d66471
commit 62f5c944dc

View File

@ -45,6 +45,7 @@ class gettext_reader {
var $originals = NULL; // offset of original table var $originals = NULL; // offset of original table
var $translations = NULL; // offset of translation table var $translations = NULL; // offset of translation table
var $pluralheader = NULL; // cache header field for plural forms var $pluralheader = NULL; // cache header field for plural forms
var $select_string_function = NULL; // cache function, which chooses plural forms
var $total = 0; // total string count var $total = 0; // total string count
var $table_originals = NULL; // table for original strings (offsets) var $table_originals = NULL; // table for original strings (offsets)
var $table_translations = NULL; // table for translated strings (offsets) var $table_translations = NULL; // table for translated strings (offsets)
@ -283,12 +284,38 @@ class gettext_reader {
} else { } else {
$header = $this->get_translation_string(0); $header = $this->get_translation_string(0);
} }
$header .= "\n"; //make sure our regex matches
if (eregi("plural-forms: ([^\n]*)\n", $header, $regs)) if (eregi("plural-forms: ([^\n]*)\n", $header, $regs))
$expr = $regs[1]; $expr = $regs[1];
else else
$expr = "nplurals=2; plural=n == 1 ? 0 : 1;"; $expr = "nplurals=2; plural=n == 1 ? 0 : 1;";
$this->pluralheader = $expr;
// add parentheses
// important since PHP's ternary evaluates from left to right
$expr.= ';';
$res= '';
$p= 0;
for ($i= 0; $i < strlen($expr); $i++) {
$ch= $expr[$i];
switch ($ch) {
case '?':
$res.= ' ? (';
$p++;
break;
case ':':
$res.= ') : (';
break;
case ';':
$res.= str_repeat( ')', $p) . ';';
$p= 0;
break;
default:
$res.= $ch;
} }
}
$this->pluralheader = $res;
}
return $this->pluralheader; return $this->pluralheader;
} }
@ -300,21 +327,22 @@ class gettext_reader {
* @return int array index of the right plural form * @return int array index of the right plural form
*/ */
function select_string($n) { function select_string($n) {
if (is_null($this->select_string_function)) {
$string = $this->get_plural_forms(); $string = $this->get_plural_forms();
$string = str_replace('nplurals',"\$total",$string); if (preg_match("/nplurals\s*=\s*(\d+)\s*\;\s*plural\s*=\s*(.*?)\;+/", $string, $matches)) {
$string = str_replace("n",$n,$string); $nplurals = $matches[1];
$string = str_replace('plural',"\$plural",$string); $expression = $matches[2];
$expression = str_replace("n", '$n', $expression);
# poEdit doesn't put any semicolons, which } else {
# results in parse error in eval $nplurals = 2;
$string .= ';'; $expression = ' $n == 1 ? 0 : 1 ';
}
$total = 0; $func_body = "
$plural = 0; \$plural = ($expression);
return (\$plural <= $nplurals)? \$plural : \$plural - 1;";
eval("$string"); $this->select_string_function = create_function('$n', $func_body);
if ($plural >= $total) $plural = $total - 1; }
return $plural; return call_user_func($this->select_string_function, $n);
} }
/** /**