Skip to content

Commit

Permalink
Merge pull request #5 from jonasraoni/bugfix/master/limit-plural-form…
Browse files Browse the repository at this point in the history
…s-charset

Updated the plural form conversion to accept a limited character set
  • Loading branch information
oscarotero committed Feb 23, 2022
2 parents 84ca009 + 84286e2 commit 2f257df
Showing 1 changed file with 16 additions and 36 deletions.
52 changes: 16 additions & 36 deletions src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function addTranslations(array $translations): self
// Slap on a return keyword and semicolon at the end.
$this->plurals[$domain] = [
'count' => (int) str_replace('nplurals=', '', $count),
'code' => str_replace('plural=', 'return ', str_replace('n', '$n', $code)).';',
'code' => 'return ' . static::fixTerseIfs(rtrim(str_replace('plural=', '', $code), ';')) . ';',
];
}

Expand Down Expand Up @@ -195,7 +195,7 @@ protected function getPluralIndex(?string $domain, int $n, bool $fallback): int
}

if (!isset($this->plurals[$domain]['function'])) {
$code = self::fixTerseIfs($this->plurals[$domain]['code']);
$code = $this->plurals[$domain]['code'];
$this->plurals[$domain]['function'] = eval("return function (\$n) { $code };");
}

Expand All @@ -207,45 +207,25 @@ protected function getPluralIndex(?string $domain, int $n, bool $fallback): int
}

/**
* This function will recursively wrap failure states in brackets if they contain a nested terse if.
* This function prepares the gettext plural form expression to be evaluated by PHP
*
* This because PHP can not handle nested terse if's unless they are wrapped in brackets.
* Nested ternary IFs will be enclosed by parenthesis and the variable "n" will be prefixed by "$".
*
* This code probably only works for the gettext plural decision codes.
*
* return ($n==1 ? 0 : $n%10>=2 && $n%10<=4 && ($n%100<10 || $n%100>=20) ? 1 : 2);
* $n==1 ? 0 : $n%10>=2 && $n%10<=4 && ($n%100<10 || $n%100>=20) ? 1 : 2
* becomes
* return ($n==1 ? 0 : ($n%10>=2 && $n%10<=4 && ($n%100<10 || $n%100>=20) ? 1 : 2));
* $n==1 ? 0 : ($n%10>=2 && $n%10<=4 && ($n%100<10 || $n%100>=20) ? 1 : 2)
*/
private static function fixTerseIfs(string $code, bool $inner = false): string
{
/*
* (?P<expression>[^?]+) Capture everything up to ? as 'expression'
* \? ?
* (?P<success>[^:]+) Capture everything up to : as 'success'
* : :
* (?P<failure>[^;]+) Capture everything up to ; as 'failure'
*/
preg_match('/(?P<expression>[^?]+)\?(?P<success>[^:]+):(?P<failure>[^;]+)/', $code, $matches);

// If no match was found then no terse if was present
if (!isset($matches[0])) {
return $code;
private static function fixTerseIfs(string $pluralForms): string
{
if (preg_match('/[^<>|%&!?:=n()\d\s]/', $pluralForms)) {
throw new InvalidArgumentException('Invalid plural form expression');
}

$expression = $matches['expression'];
$success = $matches['success'];
$failure = $matches['failure'];

// Go look for another terse if in the failure state.
$failure = static::fixTerseIfs($failure, true);
$code = $expression.' ? '.$success.' : '.$failure;

if ($inner) {
return "($code)";
$pieces = explode(':', str_replace('n', '$n', $pluralForms));
$last = array_pop($pieces);
$pluralForms = '';
foreach ($pieces as $piece) {
$pluralForms .= "$piece:(";
}

// note the semicolon. We need that for executing the code.
return "$code;";
return $pluralForms . $last . str_repeat(')', count($pieces));
}
}

0 comments on commit 2f257df

Please sign in to comment.