diff --git a/lib/private/L10N/L10N.php b/lib/private/L10N/L10N.php index 3b32ec2827..72f124f775 100644 --- a/lib/private/L10N/L10N.php +++ b/lib/private/L10N/L10N.php @@ -32,7 +32,7 @@ namespace OC\L10N; use OCP\IL10N; use OCP\L10N\IFactory; use Punic\Calendar; -use Symfony\Component\Translation\PluralizationRules; +use Symfony\Component\Translation\IdentityTranslator; class L10N implements IL10N { @@ -48,11 +48,8 @@ class L10N implements IL10N { /** @var string Locale of this object */ protected $locale; - /** @var string Plural forms (string) */ - private $pluralFormString = 'nplurals=2; plural=(n != 1);'; - - /** @var string Plural forms (function) */ - private $pluralFormFunction = null; + /** @var IdentityTranslator */ + private $identityTranslator; /** @var string[] */ private $translations = []; @@ -214,20 +211,16 @@ class L10N implements IL10N { } /** - * Returnsed function accepts the argument $n - * - * Called by \OC_L10N_String - * @return \Closure the plural form function + * @internal + * @return IdentityTranslator */ - public function getPluralFormFunction(): \Closure { - if (\is_null($this->pluralFormFunction)) { - $lang = $this->getLanguageCode(); - $this->pluralFormFunction = function ($n) use ($lang) { - return PluralizationRules::get($n, $lang); - }; + public function getIdentityTranslator(): IdentityTranslator { + if (\is_null($this->identityTranslator)) { + $this->identityTranslator = new IdentityTranslator(); + $this->identityTranslator->setLocale($this->getLocaleCode()); } - return $this->pluralFormFunction; + return $this->identityTranslator; } /** @@ -242,9 +235,6 @@ class L10N implements IL10N { return false; } - if (!empty($json['pluralForm'])) { - $this->pluralFormString = $json['pluralForm']; - } $this->translations = array_merge($this->translations, $json['translations']); return true; } diff --git a/lib/private/L10N/L10NString.php b/lib/private/L10N/L10NString.php index a82228189b..0eadadf9be 100644 --- a/lib/private/L10N/L10NString.php +++ b/lib/private/L10N/L10NString.php @@ -32,7 +32,7 @@ namespace OC\L10N; class L10NString implements \JsonSerializable { - /** @var \OC\L10N\L10N */ + /** @var L10N */ protected $l10n; /** @var string */ @@ -45,38 +45,31 @@ class L10NString implements \JsonSerializable { protected $count; /** - * @param \OC\L10N\L10N $l10n + * @param L10N $l10n * @param string|string[] $text * @param array $parameters * @param int $count */ - public function __construct(\OC\L10N\L10N $l10n, $text, $parameters, $count = 1) { + public function __construct(L10N $l10n, $text, array $parameters, int $count = 1) { $this->l10n = $l10n; $this->text = $text; $this->parameters = $parameters; $this->count = $count; } - /** - * @return string - */ - public function __toString() { + public function __toString(): string { $translations = $this->l10n->getTranslations(); + $identityTranslator = $this->l10n->getIdentityTranslator(); - $text = $this->text; - if (array_key_exists($this->text, $translations)) { - if (is_array($translations[$this->text])) { - $fn = $this->l10n->getPluralFormFunction(); - $id = $fn($this->count); - $text = $translations[$this->text][$id]; - } else { - $text = $translations[$this->text]; - } - } + $parameters = $this->parameters; + // Add $count as %count% as per \Symfony\Contracts\Translation\TranslatorInterface + $parameters['%count%'] = $this->count; - // Replace %n first (won't interfere with vsprintf) - $text = str_replace('%n', (string)$this->count, $text); - return vsprintf($text, $this->parameters); + // Use the indexed version as per \Symfony\Contracts\Translation\TranslatorInterface + $identity = implode('|', $translations[$this->text]); + $identity = str_replace('%n', '%count%', $identity); + + return $identityTranslator->trans($identity, $parameters); }