Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix plural translation without count usage in string
Signed-off-by: Christoph Wurst <[email protected]>
  • Loading branch information
ChristophWurst committed Aug 22, 2022
commit 8198674c0311c41834064e9ebdc4ad9ba9e5aca7
6 changes: 4 additions & 2 deletions lib/private/L10N/L10NString.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
*/
namespace OC\L10N;

use function strpos;

class L10NString implements \JsonSerializable {
/** @var L10N */
protected $l10n;
Expand Down Expand Up @@ -74,11 +76,11 @@ public function __toString(): string {
return 'Can not use pipe character in translations';
}

$beforeIdentity = $identity;
$identity = str_replace('%n', '%count%', $identity);

$parameters = [];
if ($beforeIdentity !== $identity) {
// Make sure to pass the count parameter to Symfony for all plural strings
if (strpos($identity, '|')) {
$parameters = ['%count%' => $this->count];
}

Expand Down
1 change: 1 addition & 0 deletions tests/data/l10n/de.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"translations" : {
"_%n file_::_%n files_": ["%n Datei", "%n Dateien"],
"_%s user found_::_%s users found_" : ["%s Benutzer gefunden","%s Benutzer gefunden"],
"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"
Expand Down
8 changes: 8 additions & 0 deletions tests/lib/L10N/L10nTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ public function testGermanPluralTranslations() {
$this->assertEquals('2 Dateien', (string) $l->n('%n file', '%n files', 2));
}

public function testGermanPluralTranslationsWithoutPluralUsage() {
$transFile = \OC::$SERVERROOT.'/tests/data/l10n/de.json';
$l = new L10N($this->getFactory(), 'test', 'de', 'de_AT', [$transFile]);

$this->assertEquals('1 Benutzer gefunden', (string) $l->n('%s user found', '%s users found', 1, [1]));
$this->assertEquals('> 1000 Benutzer gefunden', (string) $l->n('%s user found', '%s users found', 2000, ['> 1000']));
}

public function testRussianPluralTranslations() {
$transFile = \OC::$SERVERROOT.'/tests/data/l10n/ru.json';
$l = new L10N($this->getFactory(), 'test', 'ru', 'ru_UA', [$transFile]);
Expand Down