"Symfony\Component\Translation\PluralizationRules" is deprecated

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2021-03-30 20:09:42 +02:00
parent 94322971a5
commit 157147cb8e
No known key found for this signature in database
GPG Key ID: 7076EA9751AACDDA
2 changed files with 23 additions and 40 deletions

View File

@ -32,7 +32,7 @@ namespace OC\L10N;
use OCP\IL10N; use OCP\IL10N;
use OCP\L10N\IFactory; use OCP\L10N\IFactory;
use Punic\Calendar; use Punic\Calendar;
use Symfony\Component\Translation\PluralizationRules; use Symfony\Component\Translation\IdentityTranslator;
class L10N implements IL10N { class L10N implements IL10N {
@ -48,11 +48,8 @@ class L10N implements IL10N {
/** @var string Locale of this object */ /** @var string Locale of this object */
protected $locale; protected $locale;
/** @var string Plural forms (string) */ /** @var IdentityTranslator */
private $pluralFormString = 'nplurals=2; plural=(n != 1);'; private $identityTranslator;
/** @var string Plural forms (function) */
private $pluralFormFunction = null;
/** @var string[] */ /** @var string[] */
private $translations = []; private $translations = [];
@ -214,20 +211,16 @@ class L10N implements IL10N {
} }
/** /**
* Returnsed function accepts the argument $n * @internal
* * @return IdentityTranslator
* Called by \OC_L10N_String
* @return \Closure the plural form function
*/ */
public function getPluralFormFunction(): \Closure { public function getIdentityTranslator(): IdentityTranslator {
if (\is_null($this->pluralFormFunction)) { if (\is_null($this->identityTranslator)) {
$lang = $this->getLanguageCode(); $this->identityTranslator = new IdentityTranslator();
$this->pluralFormFunction = function ($n) use ($lang) { $this->identityTranslator->setLocale($this->getLocaleCode());
return PluralizationRules::get($n, $lang);
};
} }
return $this->pluralFormFunction; return $this->identityTranslator;
} }
/** /**
@ -242,9 +235,6 @@ class L10N implements IL10N {
return false; return false;
} }
if (!empty($json['pluralForm'])) {
$this->pluralFormString = $json['pluralForm'];
}
$this->translations = array_merge($this->translations, $json['translations']); $this->translations = array_merge($this->translations, $json['translations']);
return true; return true;
} }

View File

@ -32,7 +32,7 @@
namespace OC\L10N; namespace OC\L10N;
class L10NString implements \JsonSerializable { class L10NString implements \JsonSerializable {
/** @var \OC\L10N\L10N */ /** @var L10N */
protected $l10n; protected $l10n;
/** @var string */ /** @var string */
@ -45,38 +45,31 @@ class L10NString implements \JsonSerializable {
protected $count; protected $count;
/** /**
* @param \OC\L10N\L10N $l10n * @param L10N $l10n
* @param string|string[] $text * @param string|string[] $text
* @param array $parameters * @param array $parameters
* @param int $count * @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->l10n = $l10n;
$this->text = $text; $this->text = $text;
$this->parameters = $parameters; $this->parameters = $parameters;
$this->count = $count; $this->count = $count;
} }
/** public function __toString(): string {
* @return string
*/
public function __toString() {
$translations = $this->l10n->getTranslations(); $translations = $this->l10n->getTranslations();
$identityTranslator = $this->l10n->getIdentityTranslator();
$text = $this->text; $parameters = $this->parameters;
if (array_key_exists($this->text, $translations)) { // Add $count as %count% as per \Symfony\Contracts\Translation\TranslatorInterface
if (is_array($translations[$this->text])) { $parameters['%count%'] = $this->count;
$fn = $this->l10n->getPluralFormFunction();
$id = $fn($this->count);
$text = $translations[$this->text][$id];
} else {
$text = $translations[$this->text];
}
}
// Replace %n first (won't interfere with vsprintf) // Use the indexed version as per \Symfony\Contracts\Translation\TranslatorInterface
$text = str_replace('%n', (string)$this->count, $text); $identity = implode('|', $translations[$this->text]);
return vsprintf($text, $this->parameters); $identity = str_replace('%n', '%count%', $identity);
return $identityTranslator->trans($identity, $parameters);
} }