8000 [Translator] Dump native plural formats to po files · symfony/symfony@dc31739 · GitHub
[go: up one dir, main page]

Skip to content

Commit dc31739

Browse files
Stadlyfabpot
authored andcommitted
[Translator] Dump native plural formats to po files
1 parent 3c9ff1f commit dc31739

File tree

4 files changed

+81
-3
lines changed

4 files changed

+81
-3
lines changed

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

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,66 @@ public function formatCatalogue(MessageCatalogue $messages, $domain, array $opti
5151
$output .= $this->formatComments(implode(' ', (array) $metadata['sources']), ':');
5252
}
5353

54-
$output .= sprintf('msgid "%s"'."\n", $this->escape($source));
55-
$output .= sprintf('msgstr "%s"'."\n", $this->escape($target));
54+
$sourceRules = $this->getStandardRules($source);
55+
$targetRules = $this->getStandardRules($target);
56+
if (2 == \count($sourceRules) && $targetRules !== []) {
57+
$output .= sprintf('msgid "%s"'."\n", $this->escape($sourceRules[0]));
58+
$output .= sprintf('msgid_plural "%s"'."\n", $this->escape($sourceRules[1]));
59+
foreach ($targetRules as $i => $targetRule) {
60+
$output .= sprintf('msgstr[%d] "%s"'."\n", $i, $this->escape($targetRule));
61+
}
62+
} else {
63+
$output .= sprintf('msgid "%s"'."\n", $this->escape($source));
64+
$output .= sprintf('msgstr "%s"'."\n", $this->escape($target));
65+
}
5666
}
5767

5868
return $output;
5969
}
6070

71+
private function getStandardRules(string $id)
72+
{
73+
// Partly copied from TranslatorTrait::trans.
74+
$parts = [];
75+
if (preg_match('/^\|++$/', $id)) {
76+
$parts = explode('|', $id);
77+
} elseif (preg_match_all('/(?:\|\||[^\|])++/', $id, $matches)) {
78+
$parts = $matches[0];
79+
}
80+
81+
$intervalRegexp = <<<'EOF'
82+
/^(?P<interval>
83+
({\s*
84+
(\-?\d+(\.\d+)?[\s*,\s*\-?\d+(\.\d+)?]*)
85+
\s*})
86+
87+
|
88+
89+
(?P<left_delimiter>[\[\]])
90+
\s*
91+
(?P<left>-Inf|\-?\d+(\.\d+)?)
92+
\s*,\s*
93+
(?P<right>\+?Inf|\-?\d+(\.\d+)?)
94+
\s*
95+
(?P<right_delimiter>[\[\]])
96+
)\s*(?P<message>.*?)$/xs
97+
EOF;
98+
99+
$standardRules = [];
100+
foreach ($parts as $part) {
101+
$part = trim(str_replace('||', '|', $part));
102+
103+
if (preg_match($intervalRegexp, $part)) {
104+
// Explicit rule is not a standard rule.
105+
return [];
106+
} else {
107+
$standardRules[] = $part;
108+
}
109+
}
110+
111+
return $standardRules;
112+
}
113+
61114
/**
62115
* {@inheritdoc}
63116
*/

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,17 @@ public function testFormatCatalogue()
4545

4646
$this->assertStringEqualsFile(__DIR__.'/../fixtures/resources.po', $dumper->formatCatalogue($catalogue, 'messages'));
4747
}
48+
49+
public function testDumpPlurals()
50+
{
51+
$catalogue = new MessageCatalogue('en');
52+
$catalogue->add([
53+
'foo|foos' => 'bar|bars',
54+
'{0} no foos|one foo|%count% foos' => '{0} no bars|one bar|%count% bars',
55+
]);
56+
57+
$dumper = new PoFileDumper();
58+
59+
$this->assertStringEqualsFile(__DIR__.'/../fixtures/plurals.po', $dumper->formatCatalogue($catalogue, 'messages'));
60+
}
4861
}

src/Symfony/Component/Translation/Tests/Loader/PoFileLoaderTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ public function testLoadPlurals()
3434
$resource = __DIR__.'/../fixtures/plurals.po';
3535
$catalogue = $loader->load($resource, 'en', 'domain1');
3636

37-
$this->assertEquals(['foo' => 'bar', 'foos' => 'bar|bars'], $catalogue->all('domain1'));
37+
$this->assertEquals([
38+
'foo' => 'bar',
39+
'foos' => 'bar|bars',
40+
'{0} no foos|one foo|%count% foos' => '{0} no bars|one bar|%count% bars',
41+
], $catalogue->all('domain1'));
3842
$this->assertEquals('en', $catalogue->getLocale());
3943
$this->assertEquals([new FileResource($resource)], $catalogue->getResources());
4044
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
msgid ""
2+
msgstr ""
3+
"Content-Type: text/plain; charset=UTF-8\n"
4+
"Content-Transfer-Encoding: 8bit\n"
5+
"Language: en\n"
6+
17
msgid "foo"
28
msgid_plural "foos"
39
msgstr[0] "bar"
410
msgstr[1] "bars"
511

12+
msgid "{0} no foos|one foo|%count% foos"
13+
msgstr "{0} no bars|one bar|%count% bars"

0 commit comments

Comments
 (0)
0