|
|
@@ -595,71 +595,6 @@ class Factory implements IFactory { |
|
|
|
return $this->serverRoot . '/core/l10n/'; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* Creates a function from the plural string |
|
|
|
* |
|
|
|
* Parts of the code is copied from Habari: |
|
|
|
* https://github.com/habari/system/blob/master/classes/locale.php |
|
|
|
* @param string $string |
|
|
|
* @return string |
|
|
|
*/ |
|
|
|
public function createPluralFunction($string) { |
|
|
|
if (isset($this->pluralFunctions[$string])) { |
|
|
|
return $this->pluralFunctions[$string]; |
|
|
|
} |
|
|
|
|
|
|
|
if (preg_match('/^\s*nplurals\s*=\s*(\d+)\s*;\s*plural=(.*)$/u', $string, $matches)) { |
|
|
|
// sanitize |
|
|
|
$nplurals = preg_replace('/[^0-9]/', '', $matches[1]); |
|
|
|
$plural = preg_replace('#[^n0-9:\(\)\?\|\&=!<>+*/\%-]#', '', $matches[2]); |
|
|
|
|
|
|
|
$body = str_replace( |
|
|
|
['plural', 'n', '$n$plurals',], |
|
|
|
['$plural', '$n', '$nplurals',], |
|
|
|
'nplurals=' . $nplurals . '; plural=' . $plural |
|
|
|
); |
|
|
|
|
|
|
|
// add parents |
|
|
|
// important since PHP's ternary evaluates from left to right |
|
|
|
$body .= ';'; |
|
|
|
$res = ''; |
|
|
|
$p = 0; |
|
|
|
$length = strlen($body); |
|
|
|
for ($i = 0; $i < $length; $i++) { |
|
|
|
$ch = $body[$i]; |
|
|
|
switch ($ch) { |
|
|
|
case '?': |
|
|
|
$res .= ' ? ('; |
|
|
|
$p++; |
|
|
|
break; |
|
|
|
case ':': |
|
|
|
$res .= ') : ('; |
|
|
|
break; |
|
|
|
case ';': |
|
|
|
$res .= str_repeat(')', $p) . ';'; |
|
|
|
$p = 0; |
|
|
|
break; |
|
|
|
default: |
|
|
|
$res .= $ch; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
$body = $res . 'return ($plural>=$nplurals?$nplurals-1:$plural);'; |
|
|
|
$function = create_function('$n', $body); |
|
|
|
$this->pluralFunctions[$string] = $function; |
|
|
|
return $function; |
|
|
|
} else { |
|
|
|
// default: one plural form for all cases but n==1 (english) |
|
|
|
$function = create_function( |
|
|
|
'$n', |
|
|
|
'$nplurals=2;$plural=($n==1?0:1);return ($plural>=$nplurals?$nplurals-1:$plural);' |
|
|
|
); |
|
|
|
$this->pluralFunctions[$string] = $function; |
|
|
|
return $function; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @inheritDoc |
|
|
|
*/ |