From d876c2543cf3210ffa7bc62da7e4d6eb668b73e7 Mon Sep 17 00:00:00 2001 From: Jay Hemnani <193022578+jayhemnani9910@users.noreply.github.com> Date: Mon, 30 Nov 2026 12:38:07 +0530 Subject: [PATCH] Keep plural translations when merging with a template A .pot read by PoLoader has msgstr[n] "", so its pluralTranslations is [''], which empty() treats as set, and merging it with a PO dropped the PO's plural translations. --- src/Translation.php | 18 +++++++++++++++++- tests/TranslationTest.php | 15 +++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/Translation.php b/src/Translation.php index f42ee2d1..cdcf580d 100644 --- a/src/Translation.php +++ b/src/Translation.php @@ -283,7 +283,9 @@ public function mergeWith(Translation $translation, int $strategy = 0): Translat $merged->previousPlural = $translation->previousPlural; } - if (empty($merged->pluralTranslations) || (!empty($translation->pluralTranslations) && $override)) { + if (!self::hasPluralValues($merged->pluralTranslations) + || (self::hasPluralValues($translation->pluralTranslations) && $override) + ) { $merged->pluralTranslations = $translation->pluralTranslations; } @@ -292,6 +294,20 @@ public function mergeWith(Translation $translation, int $strategy = 0): Translat return $merged; } + /** + * Checks whether any plural translation is set. A PO template has empty msgstr[n] lines, which do not count. + */ + private static function hasPluralValues(array $values): bool + { + foreach ($values as $value) { + if (self::hasValue($value)) { + return true; + } + } + + return false; + } + /** * Checks whether a string is set and not empty. Unlike a truthiness check, "0" is a valid value. */ diff --git a/tests/TranslationTest.php b/tests/TranslationTest.php index 21306885..b4a15d5a 100644 --- a/tests/TranslationTest.php +++ b/tests/TranslationTest.php @@ -125,4 +125,19 @@ public function testMergeKeepsZeroStrings(): void $this->assertSame('0', $merged->getTranslation()); $this->assertSame('0', $merged->getPlural()); } + + public function testMergeKeepsPluralTranslationsOverEmptyTemplate(): void + { + $template = Translation::create(null, '%d file'); + $template->setPlural('%d files'); + $template->translatePlural(''); + + $translation = Translation::create(null, '%d file'); + $translation->setPlural('%d files'); + $translation->translatePlural('%d fichiers'); + + $merged = $template->mergeWith($translation); + + $this->assertSame(['%d fichiers'], $merged->getPluralTranslations()); + } }