8000 Add comments when dumping po files · symfony/symfony@82d08d4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 82d08d4

Browse files
committed
Add comments when dumping po files
1 parent f527acf commit 82d08d4

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

src/Symfony/Component/Translation/Dumper/PoFileDumper.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ public function formatCatalogue(MessageCatalogue $messages, $domain, array $opti
3939
} else {
4040
$newLine = true;
4141
}
42+
$metadata = $messages->getMetadata($source, $domain);
43+
44+
if (isset($metadata['comments'])) {
45+
$output .= $this->formatComments($metadata['comments']);
46+
}
47+
if (isset($metadata['flags'])) {
48+
$output .= $this->formatComments(implode(',', (array) $metadata['flags']), ',');
49+
}
50+
if (isset($metadata['sources'])) {
51+
$output .= $this->formatComments(implode(' ', (array) $metadata['sources']), ':');
52+
}
53+
4254
$output .= sprintf('msgid "%s"'."\n", $this->escape($source));
4355
$output .= sprintf('msgstr "%s"', $this->escape($target));
4456
}
@@ -58,4 +70,15 @@ private function escape($str)
5870
{
5971
return addcslashes($str, "\0..\37\42\134");
6072
}
73+
74+
private function formatComments($comments, string $prefix = ''): ?string
75+
{
76+
$output = null;
77+
78+
foreach ((array) $comments as $comment) {
79+
$output .= sprintf('#%s %s'."\n", $prefix, $comment);
80+
}
81+
82+
return $output;
83+
}
6184
}

src/Symfony/Component/Translation/Tests/Dumper/PoFileDumperTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,26 @@ class PoFileDumperTest extends TestCase
2020
public function testFormatCatalogue()
2121
{
2222
$catalogue = new MessageCatalogue('en');
23-
$catalogue->add(['foo' => 'bar']);
23+
$catalogue->add(['foo' => 'bar', 'bar' => 'foo', 'foo_bar' => 'foo bar']);
24+
$catalogue->setMetadata('bar', [
25+
'comments' => 'Comment',
26+
'flags' => 'fuzzy',
27+
'sources' => 'src/file_1',
28+
]);
29+
$catalogue->setMetadata('foo_bar', [
30+
'comments' => [
31+
'Comment 1',
32+
'Comment 2',
33+
],
34+
'flags' => [
35+
'fuzzy',
36+
'another',
37+
],
38+
'sources' => [
39+
'src/file_1',
40+
'src/file_2:50',
41+
],
42+
]);
2443

2544
$dumper = new PoFileDumper();
2645

src/Symfony/Component/Translation/Tests/fixtures/resources.po

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,15 @@ msgstr ""
55
"Language: en\n"
66

77
msgid "foo"
8-
msgstr "bar"
8+
msgstr "bar"
9+
# Comment
10+
#, fuzzy
11+
#: src/file_1
12+
msgid "bar"
13+
msgstr "foo"
14+
# Comment 1
15+
# Comment 2
16+
#, fuzzy,another
17+
#: src/file_1 src/file_2:50
18+
msgid "foo_bar"
19+
msgstr "foo bar"

0 commit comments

Comments
 (0)
0