Skip to content
Open
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
6 changes: 6 additions & 0 deletions src/Loader/PoLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
}

Expand Down
31 changes: 31 additions & 0 deletions tests/PoLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}