diff options
-rw-r--r-- | build/translation-checker.php | 5 | ||||
-rw-r--r-- | lib/private/L10N/L10N.php | 30 | ||||
-rw-r--r-- | lib/private/L10N/L10NString.php | 39 | ||||
-rw-r--r-- | lib/private/Share20/Manager.php | 4 | ||||
-rw-r--r-- | tests/data/l10n/de.json | 5 | ||||
-rw-r--r-- | tests/lib/L10N/L10nTest.php | 21 | ||||
-rw-r--r-- | tests/lib/Share20/ManagerTest.php | 6 |
7 files changed, 70 insertions, 40 deletions
diff --git a/build/translation-checker.php b/build/translation-checker.php index 1f7ec343af5..53c139a1d63 100644 --- a/build/translation-checker.php +++ b/build/translation-checker.php @@ -48,6 +48,11 @@ foreach ($directories as $dir) { $content = file_get_contents($file->getPathname()); $json = json_decode($content, true); + $translations = json_encode($json['translations']); + if (strpos($translations, '|') !== false) { + $errors[] = $file->getPathname() . "\n" . ' ' . 'Contains a | in the translations' . "\n"; + } + if (json_last_error() !== JSON_ERROR_NONE) { $errors[] = $file->getPathname() . "\n" . ' ' . json_last_error_msg() . "\n"; } else { diff --git a/lib/private/L10N/L10N.php b/lib/private/L10N/L10N.php index 3b32ec2827f..72f124f7754 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 a82228189b6..9d219ea1772 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,37 +45,44 @@ 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; + // Use the indexed version as per \Symfony\Contracts\Translation\TranslatorInterface + $identity = $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]; + $identity = $translations[$this->text]; + } + + if (is_array($identity)) { + $pipeCheck = implode('', $identity); + if (strpos($pipeCheck, '|') !== false) { + return 'Can not use pipe character in translations'; } + + $identity = implode('|', $identity); + } elseif (strpos($identity, '|') !== false) { + return 'Can not use pipe character in translations'; } - // Replace %n first (won't interfere with vsprintf) - $text = str_replace('%n', (string)$this->count, $text); + $identity = str_replace('%n', '%count%', $identity); + + // $count as %count% as per \Symfony\Contracts\Translation\TranslatorInterface + $text = $identityTranslator->trans($identity, ['%count%' => $this->count]); + return vsprintf($text, $this->parameters); } diff --git a/lib/private/Share20/Manager.php b/lib/private/Share20/Manager.php index 7211a4eb1e8..00020c3a8f6 100644 --- a/lib/private/Share20/Manager.php +++ b/lib/private/Share20/Manager.php @@ -441,7 +441,7 @@ class Manager implements IManager { $date->setTime(0, 0, 0); $date->add(new \DateInterval('P' . $defaultExpireDays . 'D')); if ($date < $expirationDate) { - $message = $this->l->t('Can’t set expiration date more than %s days in the future', [$defaultExpireDays]); + $message = $this->l->n('Can’t set expiration date more than %n day in the future', 'Can’t set expiration date more than %n days in the future', $defaultExpireDays); throw new GenericShareException($message, $message, 404); } } @@ -517,7 +517,7 @@ class Manager implements IManager { $date->setTime(0, 0, 0); $date->add(new \DateInterval('P' . $this->shareApiLinkDefaultExpireDays() . 'D')); if ($date < $expirationDate) { - $message = $this->l->t('Can’t set expiration date more than %s days in the future', [$this->shareApiLinkDefaultExpireDays()]); + $message = $this->l->n('Can’t set expiration date more than %n day in the future', 'Can’t set expiration date more than %n days in the future', $this->shareApiLinkDefaultExpireDays()); throw new GenericShareException($message, $message, 404); } } diff --git a/tests/data/l10n/de.json b/tests/data/l10n/de.json index c2b6f34c081..d9e75d303c5 100644 --- a/tests/data/l10n/de.json +++ b/tests/data/l10n/de.json @@ -1,6 +1,9 @@ { "translations" : { - "_%n file_::_%n files_": ["%n Datei", "%n Dateien"] + "_%n file_::_%n files_": ["%n Datei", "%n Dateien"], + "Ordered placeholders one %s two %s": "Placeholder one %s two %s", + "Reordered placeholders one %s two %s": "Placeholder two %2$s one %1$s", + "Reordered placeholders one %1$s two %2$s": "Placeholder two %2$s one %1$s" }, "pluralForm" : "nplurals=2; plural=(n != 1);" } diff --git a/tests/lib/L10N/L10nTest.php b/tests/lib/L10N/L10nTest.php index 0de09386fba..3fb22b3f66e 100644 --- a/tests/lib/L10N/L10nTest.php +++ b/tests/lib/L10N/L10nTest.php @@ -76,6 +76,27 @@ class L10nTest extends TestCase { $this->assertEquals('5 oken', (string)$l->n('%n window', '%n windows', 5)); } + public function dataPlaceholders(): array { + return [ + ['Ordered placeholders one %s two %s', 'Placeholder one 1 two 2'], + ['Reordered placeholders one %s two %s', 'Placeholder two 2 one 1'], + ['Reordered placeholders one %1$s two %2$s', 'Placeholder two 2 one 1'], + ]; + } + + /** + * @dataProvider dataPlaceholders + * + * @param $string + * @param $expected + */ + public function testPlaceholders($string, $expected): void { + $transFile = \OC::$SERVERROOT.'/tests/data/l10n/de.json'; + $l = new L10N($this->getFactory(), 'test', 'de', 'de_AT', [$transFile]); + + $this->assertEquals($expected, $l->t($string, ['1', '2'])); + } + public function localizationData() { return [ // timestamp as string diff --git a/tests/lib/Share20/ManagerTest.php b/tests/lib/Share20/ManagerTest.php index e17f179b600..a4dd621522d 100644 --- a/tests/lib/Share20/ManagerTest.php +++ b/tests/lib/Share20/ManagerTest.php @@ -126,6 +126,10 @@ class ManagerTest extends \Test\TestCase { ->willReturnCallback(function ($text, $parameters = []) { return vsprintf($text, $parameters); }); + $this->l->method('n') + ->willReturnCallback(function ($singular, $plural, $count, $parameters = []) { + return vsprintf(str_replace('%n', $count, ($count === 1) ? $singular : $plural), $parameters); + }); $this->factory = new DummyFactory(\OC::$server); @@ -1957,7 +1961,7 @@ class ManagerTest extends \Test\TestCase { $data = []; // No exclude groups - $data[] = ['no', null, null, null, false]; + $data[] = ['no', null, null, [], false]; // empty exclude list, user no groups $data[] = ['yes', '', json_encode(['']), [], false]; |