diff --git a/src/Loader/PoLoader.php b/src/Loader/PoLoader.php index 67968cb..baf0d78 100644 --- a/src/Loader/PoLoader.php +++ b/src/Loader/PoLoader.php @@ -24,7 +24,13 @@ public function loadString(string $string, ?Translations $translations = null): $nextLine = next($lines); // Treat empty comments as empty lines https://github.com/php-gettext/Gettext/pull/296 + // but only after an entry: before it, `#` is part of its comments (the header + // description is followed by one) if ($line === '#') { + if (self::isEmpty($translation)) { + $line = $nextLine; + continue; + } $line = ''; } diff --git a/tests/PoLoaderTest.php b/tests/PoLoaderTest.php index 399f893..c7fce02 100644 --- a/tests/PoLoaderTest.php +++ b/tests/PoLoaderTest.php @@ -22,4 +22,35 @@ public function testStringDecode($source, $decoded): void { $this->assertSame($decoded, PoLoader::decode($source)); } + + public function testCommentsBeforeEmptyComment(): void + { + $po = <<<'EOT' +# German translation for myapp. +# Copyright (C) 2024 Example Inc. +# +#, fuzzy +msgid "" +msgstr "" +"Language: de\n" + +msgid "Hello" +msgstr "Hallo" +# +# Reviewed by Max +# +#: src/home.php:12 +msgid "Bye" +msgstr "Tschüss" +EOT; + $translations = $this->createPoLoader()->loadString($po); + + $this->assertSame( + "German translation for myapp.\nCopyright (C) 2024 Example Inc.", + $translations->getDescription() + ); + $this->assertTrue($translations->getFlags()->has('fuzzy')); + $this->assertSame('Hallo', $translations->find(null, 'Hello')->getTranslation()); + $this->assertSame(['Reviewed by Max'], $translations->find(null, 'Bye')->getComments()->toArray()); + } }