From b0728a8379157fef0caee532e979efa76c1d219d Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Wed, 18 Nov 2026 12:58:57 +0000 Subject: [PATCH 1/2] Treat "0" as a valid string Strings were checked for truthiness in several places, so "0" was handled like an empty string: * The PO loader dropped untranslated entries with `msgid "0"`. * The PO generator wrote `msgstr ""` for a "0" translation and left out a "0" context, plural or previous string. * The MO generator skipped entries translated as "0", as well as a "0" context or plural. * `Translation::mergeWith()` replaced a "0" translation or plural with the other one, and ignored them when overriding. --- src/Generator/MoGenerator.php | 8 +++++--- src/Generator/PoGenerator.php | 35 +++++++++++++++++++++++---------- src/Loader/PoLoader.php | 8 ++------ src/Translation.php | 18 ++++++++++++----- tests/BasePoLoaderTestCase.php | 33 +++++++++++++++++++++++++++++++ tests/MoGeneratorTest.php | 28 ++++++++++++++++++++++++++ tests/PoGeneratorTest.php | 36 ++++++++++++++++++++++++++++++++++ tests/TranslationTest.php | 26 ++++++++++++++++++++++++ 8 files changed, 168 insertions(+), 24 deletions(-) diff --git a/src/Generator/MoGenerator.php b/src/Generator/MoGenerator.php index e8151016..8064544a 100644 --- a/src/Generator/MoGenerator.php +++ b/src/Generator/MoGenerator.php @@ -32,11 +32,13 @@ public function generateString(Translations $translations): string } foreach ($translations as $translation) { - if (!$translation->getTranslation() || $translation->isDisabled()) { + if (!$translation->isTranslated() || $translation->isDisabled()) { continue; } - if ($context = $translation->getContext()) { + $context = $translation->getContext(); + + if ($context !== null && $context !== '') { $originalString = "{$context}\x04{$translation->getOriginal()}"; } else { $originalString = $translation->getOriginal(); @@ -142,7 +144,7 @@ public function generateString(Translations $translations): string private static function hasPluralTranslations(Translation $translation): bool { - if (!$translation->getPlural()) { + if ($translation->getPlural() === null || $translation->getPlural() === '') { return false; } diff --git a/src/Generator/PoGenerator.php b/src/Generator/PoGenerator.php index 000c1a6a..6372429c 100644 --- a/src/Generator/PoGenerator.php +++ b/src/Generator/PoGenerator.php @@ -65,33 +65,38 @@ public function generateString(Translations $translations): string $prefix = $translation->isDisabled() ? '#~ ' : ''; - if ($context = $translation->getPreviousContext()) { - $lines[] = sprintf('%s#| msgctxt %s', $prefix, self::encode($context)); + $previousContext = $translation->getPreviousContext(); + if (self::hasValue($previousContext)) { + $lines[] = sprintf('%s#| msgctxt %s', $prefix, self::encode($previousContext)); } - if ($original = $translation->getPreviousOriginal()) { - $lines[] = sprintf('%s#| msgid %s', $prefix, self::encode($original)); + $previousOriginal = $translation->getPreviousOriginal(); + if (self::hasValue($previousOriginal)) { + $lines[] = sprintf('%s#| msgid %s', $prefix, self::encode($previousOriginal)); } - if ($plural = $translation->getPreviousPlural()) { - $lines[] = sprintf('%s#| msgid_plural %s', $prefix, self::encode($plural)); + $previousPlural = $translation->getPreviousPlural(); + if (self::hasValue($previousPlural)) { + $lines[] = sprintf('%s#| msgid_plural %s', $prefix, self::encode($previousPlural)); } - if ($context = $translation->getContext()) { + $context = $translation->getContext(); + if (self::hasValue($context)) { $lines[] = sprintf('%smsgctxt %s', $prefix, self::encode($context)); } self::appendLines($lines, $prefix, 'msgid', $translation->getOriginal()); - if ($plural = $translation->getPlural()) { + $plural = $translation->getPlural(); + if (self::hasValue($plural)) { self::appendLines($lines, $prefix, 'msgid_plural', $plural); - self::appendLines($lines, $prefix, 'msgstr[0]', $translation->getTranslation() ?: ''); + self::appendLines($lines, $prefix, 'msgstr[0]', $translation->getTranslation() ?? ''); foreach ($translation->getPluralTranslations($pluralSize) as $k => $v) { self::appendLines($lines, $prefix, sprintf('msgstr[%d]', $k + 1), $v); } } else { - self::appendLines($lines, $prefix, 'msgstr', $translation->getTranslation() ?: ''); + self::appendLines($lines, $prefix, 'msgstr', $translation->getTranslation() ?? ''); } $lines[] = ''; @@ -100,6 +105,16 @@ public function generateString(Translations $translations): string return implode("\n", $lines); } + /** + * Checks whether a string is set and not empty. Unlike a truthiness check, "0" is a valid value. + * + * @phpstan-assert-if-true string $value + */ + private static function hasValue(?string $value): bool + { + return $value !== null && $value !== ''; + } + /** * Add one or more lines depending whether the string is multiline or not. */ diff --git a/src/Loader/PoLoader.php b/src/Loader/PoLoader.php index ca7c281e..67968cb6 100644 --- a/src/Loader/PoLoader.php +++ b/src/Loader/PoLoader.php @@ -218,14 +218,10 @@ public static function decode(string $value): string private static function isEmpty(Translation $translation): bool { - if (!empty($translation->getOriginal())) { + if ($translation->getOriginal() !== '') { return false; } - if (!empty($translation->getTranslation())) { - return false; - } - - return true; + return !$translation->isTranslated(); } } diff --git a/src/Translation.php b/src/Translation.php index 69cb6b90..e3243d62 100644 --- a/src/Translation.php +++ b/src/Translation.php @@ -261,23 +261,23 @@ public function mergeWith(Translation $translation, int $strategy = 0): Translat $override = (bool) ($strategy & Merge::TRANSLATIONS_OVERRIDE); - if (!$merged->translation || ($translation->translation && $override)) { + if (!self::hasValue($merged->translation) || (self::hasValue($translation->translation) && $override)) { $merged->translation = $translation->translation; } - if (!$merged->plural || ($translation->plural && $override)) { + if (!self::hasValue($merged->plural) || (self::hasValue($translation->plural) && $override)) { $merged->plural = $translation->plural; } - if (!$merged->previousContext || ($translation->previousContext && $override)) { + if (!self::hasValue($merged->previousContext) || (self::hasValue($translation->previousContext) && $override)) { $merged->previousContext = $translation->previousContext; } - if (!$merged->previousOriginal || ($translation->previousOriginal && $override)) { + if (!self::hasValue($merged->previousOriginal) || (self::hasValue($translation->previousOriginal) && $override)) { $merged->previousOriginal = $translation->previousOriginal; } - if (!$merged->previousPlural || ($translation->previousPlural && $override)) { + if (!self::hasValue($merged->previousPlural) || (self::hasValue($translation->previousPlural) && $override)) { $merged->previousPlural = $translation->previousPlural; } @@ -289,4 +289,12 @@ public function mergeWith(Translation $translation, int $strategy = 0): Translat return $merged; } + + /** + * Checks whether a string is set and not empty. Unlike a truthiness check, "0" is a valid value. + */ + private static function hasValue(?string $value): bool + { + return $value !== null && $value !== ''; + } } diff --git a/tests/BasePoLoaderTestCase.php b/tests/BasePoLoaderTestCase.php index da672a6b..dce3f588 100644 --- a/tests/BasePoLoaderTestCase.php +++ b/tests/BasePoLoaderTestCase.php @@ -286,4 +286,37 @@ public function testMultilineDisabledTranslations(): void $translation->getTranslation() ); } + + public function testZeroStrings(): void + { + $po = <<<'EOT' +msgid "" +msgstr "" +"Language: gl\n" + +msgid "0" +msgstr "" + +msgctxt "start of week" +msgid "1" +msgstr "0" + +msgctxt "0" +msgid "Zero context" +msgstr "Translated" +EOT; + $translations = $this->createPoLoader()->loadString($po); + + $this->assertCount(3, $translations); + + $translation = $translations->find(null, '0'); + $this->assertNotNull($translation); + $this->assertFalse($translation->isTranslated()); + + $translation = $translations->find('start of week', '1'); + $this->assertNotNull($translation); + $this->assertSame('0', $translation->getTranslation()); + + $this->assertNotNull($translations->find('0', 'Zero context')); + } } diff --git a/tests/MoGeneratorTest.php b/tests/MoGeneratorTest.php index 36871802..88565d7b 100644 --- a/tests/MoGeneratorTest.php +++ b/tests/MoGeneratorTest.php @@ -54,4 +54,32 @@ public function testMoGenerator(): void $this->assertCount(4, $result); $this->assertCount(5, $result->getHeaders()); } + + public function testZeroStrings(): void + { + $translations = Translations::create(); + + $translation = Translation::create('0', '1'); + $translation->translate('0'); + $translations->add($translation); + + $translation = Translation::create(null, 'One'); + $translation->setPlural('0'); + $translation->translate('Singular'); + $translation->translatePlural('Plural'); + $translations->add($translation); + + $result = (new MoLoader())->loadString((new MoGenerator())->generateString($translations)); + + $this->assertCount(2, $result); + + $translation = $result->find('0', '1'); + $this->assertNotNull($translation); + $this->assertSame('0', $translation->getTranslation()); + + $translation = $result->find(null, 'One'); + $this->assertNotNull($translation); + $this->assertSame('0', $translation->getPlural()); + $this->assertSame(['Plural'], $translation->getPluralTranslations()); + } } diff --git a/tests/PoGeneratorTest.php b/tests/PoGeneratorTest.php index b6ee22e4..04be77b7 100644 --- a/tests/PoGeneratorTest.php +++ b/tests/PoGeneratorTest.php @@ -114,4 +114,40 @@ public function testStringEncode(string $encoded, string $decoded): void { $this->assertSame($encoded, PoGenerator::encode($decoded)); } + + public function testZeroStrings(): void + { + $translations = Translations::create(); + + $translation = Translation::create('0', '1'); + $translation->translate('0'); + $translation->setPreviousContext('0'); + $translation->setPreviousOriginal('0'); + $translations->add($translation); + + $translation = Translation::create(null, 'One'); + $translation->setPlural('0'); + $translation->translate('0'); + $translation->translatePlural('0'); + $translations->add($translation); + + $expected = <<<'EOT' +msgid "" +msgstr "" + +#| msgctxt "0" +#| msgid "0" +msgctxt "0" +msgid "1" +msgstr "0" + +msgid "One" +msgid_plural "0" +msgstr[0] "0" +msgstr[1] "0" + +EOT; + + $this->assertSame($expected, (new PoGenerator())->generateString($translations)); + } } diff --git a/tests/TranslationTest.php b/tests/TranslationTest.php index 4a7cfaa8..21306885 100644 --- a/tests/TranslationTest.php +++ b/tests/TranslationTest.php @@ -5,6 +5,7 @@ use Gettext\Comments; use Gettext\Flags; +use Gettext\Merge; use Gettext\References; use Gettext\Translation; use PHPUnit\Framework\TestCase; @@ -99,4 +100,29 @@ public function testMergeTranslation(): void $this->assertNotSame($merged, $translation1); $this->assertNotSame($merged, $translation2); } + + public function testMergeKeepsZeroStrings(): void + { + $translation1 = Translation::create(null, '1'); + $translation1->setPlural('0'); + $translation1->translate('0'); + + $translation2 = Translation::create(null, '1'); + $translation2->setPlural('Plural'); + $translation2->translate('One'); + + $merged = $translation1->mergeWith($translation2); + + $this->assertSame('0', $merged->getTranslation()); + $this->assertSame('0', $merged->getPlural()); + + $merged = $translation1->mergeWith(Translation::create(null, '1'), Merge::TRANSLATIONS_OVERRIDE); + + $this->assertSame('0', $merged->getTranslation()); + + $merged = $translation2->mergeWith($translation1, Merge::TRANSLATIONS_OVERRIDE); + + $this->assertSame('0', $merged->getTranslation()); + $this->assertSame('0', $merged->getPlural()); + } } From 853d88765ff1f995050a1016d6b1d408906c226b Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Wed, 18 Nov 2026 12:58:57 +0000 Subject: [PATCH 2/2] Wrap line exceeding 120 characters --- src/Translation.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Translation.php b/src/Translation.php index e3243d62..f42ee2d1 100644 --- a/src/Translation.php +++ b/src/Translation.php @@ -273,7 +273,9 @@ public function mergeWith(Translation $translation, int $strategy = 0): Translat $merged->previousContext = $translation->previousContext; } - if (!self::hasValue($merged->previousOriginal) || (self::hasValue($translation->previousOriginal) && $override)) { + if (!self::hasValue($merged->previousOriginal) + || (self::hasValue($translation->previousOriginal) && $override) + ) { $merged->previousOriginal = $translation->previousOriginal; }