8000 feature #30909 [Translator] Add comments when dumping po files (deguif) · symfony/symfony@e05aaf9 · GitHub
[go: up one dir, main page]

Skip to content

Commit e05aaf9

Browse files
committed
feature #30909 [Translator] Add comments when dumping po files (deguif)
This PR was merged into the 4.3-dev branch. Discussion ---------- [Translator] Add comments when dumping po files | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | ? <!-- please add some, will be required by reviewers --> | Fixed tickets | #29962 | License | MIT | Doc PR | This code ```php $catalogue = new MessageCatalogue('fr'); $dumper = new PoFileDumper(); $catalogue->set('key.one', 'First key', 'custom'); $catalogue->setMetadata('key.one', ['sources' => 'src/file_1', 'comments' => 'Comment', 'flags' => 'fuzzy'], 'custom'); $catalogue->set('key.second', 'Second key', 'custom'); $catalogue->setMetadata('key.second', ['sources' => ['src/file_1', 'src/file_2'], 'comments' => ['Comment 1', 'Comment 2'], 'flags' => ['fuzzy', 'another']], 'custom'); $dumper->dump($catalogue, [ 'path' => 'xxx', ]); ``` Will produces this output: ``` msgid "" msgstr "" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" "Language: fr\n" # Comment #, fuzzy #: src/file_1 msgid "key.one" msgstr "First key" # Comment 1 # Comment 2 #, fuzzy,another #: src/file_1 src/file_2 msgid "key.second" msgstr "Second key" ``` Commits ------- 31b3a55 Add comments when dumping po files
2 parents 3de3e4e + 31b3a55 commit e05aaf9

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
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"'."\n", $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', 'bar' => 'foo']);
23+
$catalogue->add(['foo' => 'bar', 'bar' => 'foo', 'foo_bar' => 'foobar', 'bar_foo' => 'barfoo']);
24+
$catalogue->setMetadata('foo_bar', [
25+
'comments' => [
26+
'Comment 1',
27+
'Comment 2',
28+
],
29+
'flags' => [
30+
'fuzzy',
31+
'another',
32+
],
33+
'sources' => [
34+
'src/file_1',
35+
'src/file_2:50',
36+
],
37+
]);
38+
$catalogue->setMetadata('bar_foo', [
39+
'comments' => 'Comment',
40+
'flags' => 'fuzzy',
41+
'sources' => 'src/file_1',
42+
]);
2443

2544
$dumper = new PoFileDumper();
2645

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,16 @@ msgstr "bar"
99

1010
msgid "bar"
1111
msgstr "foo"
12+
13+
# Comment 1
14+
# Comment 2
15+
#, fuzzy,another
16+
#: src/file_1 src/file_2:50
17+
msgid "foo_bar"
18+
msgstr "foobar"
19+
20+
# Comment
21+
#, fuzzy
22+
#: src/file_1
23+
msgid "bar_foo"
24+
msgstr "barfoo"

0 commit comments

Comments
 (0)
0