Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e631806

Browse filesBrowse files
committed
feature #31269 [Translator] Dump native plural formats to po files (Stadly)
This PR was submitted for the master branch but it was squashed and merged into the 4.4 branch instead (closes #31269). Discussion ---------- [Translator] Dump native plural formats to po files | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #29963, #10152 | License | MIT | Doc PR | Implementing support for dumping to the native po plural format. ``` 'foo|foos' => 'bar|bars' ``` Before, the entry above was dumped directly: ``` msgid "foo|foos" msgstr "bar|bars" ``` With this PR, it is dumped using the native po plural format: ``` msgid "foo" msgid_plural "foos" msgstr[0] "bar" msgstr[1] "bars" ``` Strings using explicit rules or contain more than 2 pluralization forms are still dumped directly, as the po format does not support such cases: ``` '{0} no foos|one foo|%count% foos' => '{0} no bars|one bar|%count% bars' ``` ``` msgid "{0} no foos|one foo|%count% foos" msgstr "{0} no bars|one bar|%count% bars" ``` This PR complements #31266, fixing loading of native po plural formats. Commits ------- dc31739 [Translator] Dump native plural formats to po files
2 parents 3c9ff1f + dc31739 commit e631806
Copy full SHA for e631806

File tree

Expand file treeCollapse file tree

4 files changed

+81
-3
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+81
-3
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Dumper/PoFileDumper.php
+55-2Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Tests/Dumper/PoFileDumperTest.php
+13Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Tests/Loader/PoFileLoaderTest.php
+5-1Lines 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
}
+8Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.