From f28e53441387b943c9dceb415535145c3d394ec5 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 26 Apr 2022 11:05:02 +0200 Subject: [PATCH 1/2] Add a unit test for translation with trailing colon Signed-off-by: Joas Schilling --- tests/lib/L10N/L10nTest.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/lib/L10N/L10nTest.php b/tests/lib/L10N/L10nTest.php index 9f6337ba804d0..f410c523c0511 100644 --- a/tests/lib/L10N/L10nTest.php +++ b/tests/lib/L10N/L10nTest.php @@ -35,6 +35,13 @@ protected function getFactory() { return new Factory($config, $request, $userSession, \OC::$SERVERROOT); } + public function testSimpleTranslationWithTrailingColon(): void { + $transFile = \OC::$SERVERROOT.'/tests/data/l10n/de.json'; + $l = new L10N($this->getFactory(), 'test', 'de', 'de_AT', [$transFile]); + + $this->assertEquals('Files:', $l->t('Files:')); + } + public function testGermanPluralTranslations() { $transFile = \OC::$SERVERROOT.'/tests/data/l10n/de.json'; $l = new L10N($this->getFactory(), 'test', 'de', 'de_AT', [$transFile]); From 85b517ad10ad5abd0eef2c4f7d838cf7711e44ba Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 26 Apr 2022 11:06:00 +0200 Subject: [PATCH 2/2] Fix translations with trailing colons Signed-off-by: Joas Schilling --- lib/private/L10N/L10NString.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/private/L10N/L10NString.php b/lib/private/L10N/L10NString.php index 33ccc4b140964..73199fdbd1c09 100644 --- a/lib/private/L10N/L10NString.php +++ b/lib/private/L10N/L10NString.php @@ -74,10 +74,16 @@ public function __toString(): string { return 'Can not use pipe character in translations'; } + $beforeIdentity = $identity; $identity = str_replace('%n', '%count%', $identity); + $parameters = []; + if ($beforeIdentity !== $identity) { + $parameters = ['%count%' => $this->count]; + } + // $count as %count% as per \Symfony\Contracts\Translation\TranslatorInterface - $text = $identityTranslator->trans($identity, ['%count%' => $this->count]); + $text = $identityTranslator->trans($identity, $parameters); return vsprintf($text, $this->parameters); }