Skip to content
Merged
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(l10n): Fix plural issue with different locale and language
We need to use the language code here instead of the locale,
because Symfony does not distinguish between the two and would
otherwise e.g. with locale "Czech" and language "German" try to
pick a non-existing plural rule, because Czech has 4 plural forms
and German only 2.

Signed-off-by: Joas Schilling <[email protected]>
  • Loading branch information
nickvergessen committed Jun 21, 2023
commit ab8205fd35a3815673fc17fa78adc5bc770cfdc7
7 changes: 6 additions & 1 deletion lib/private/L10N/L10N.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,12 @@ public function getTranslations(): array {
public function getIdentityTranslator(): IdentityTranslator {
if (\is_null($this->identityTranslator)) {
$this->identityTranslator = new IdentityTranslator();
$this->identityTranslator->setLocale($this->getLocaleCode());
// We need to use the language code here instead of the locale,
// because Symfony does not distinguish between the two and would
// otherwise e.g. with locale "Czech" and language "German" try to
// pick a non-existing plural rule, because Czech has 4 plural forms
// and German only 2.
$this->identityTranslator->setLocale($this->getLanguageCode());
}

return $this->identityTranslator;
Expand Down
9 changes: 9 additions & 0 deletions tests/lib/L10N/L10nTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ public function testCzechPluralTranslations() {
$this->assertEquals('5 oken', (string)$l->n('%n window', '%n windows', 5));
}

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

$this->assertEquals('1 Datei', (string) $l->n('%n file', '%n files', 1));
$this->assertEquals('2 Dateien', (string) $l->n('%n file', '%n files', 2));
$this->assertEquals('5 Dateien', (string) $l->n('%n file', '%n files', 5));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before the patch this here fails:

1) Test\L10N\L10nTest::testGermanPluralWithCzechLocaleTranslations
Symfony\Component\Translation\Exception\InvalidArgumentException: Unable to choose a translation for "%count% Datei|%count% Dateien" with locale "cs_CZ" for value "5". Double check that this translation has the correct plural options (e.g. "There is one apple|There are %count% apples").

}

public function dataPlaceholders(): array {
return [
['Ordered placeholders one %s two %s', 'Placeholder one 1 two 2'],
Expand Down