Skip to content

Commit 9fc8d2e

Browse files
committed
bug #31266 [Translator] Load plurals from po files properly (Stadly)
This PR was squashed before being merged into the 3.4 branch (closes #31266). Discussion ---------- [Translator] Load plurals from po files properly | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #10152 (comment) | License | MIT | Doc PR | Plurals were not handled correctly when loading po files. ``` msgid "foo" msgid_plural "foos" msgstr[0] "bar" msgstr[1] "bars" ``` Before, the po entry above was treated as two entries, which doesn't make sense: ``` 'foo' => 'bar' 'foos' => 'bar|bars' ``` With this PR, it is treated as one entry: ``` 'foo|foos' => 'bar|bars' ``` Commits ------- 6b69a99 [Translator] Load plurals from po files properly
2 parents 166502c + 6b69a99 commit 9fc8d2e

File tree

4 files changed

+40
-20
lines changed

4 files changed

+40
-20
lines changed

src/Symfony/Component/Translation/Loader/PoFileLoader.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -126,23 +126,24 @@ protected function loadResource($resource)
126126
*/
127127
private function addMessage(array &$messages, array $item)
128128
{
129-
if (\is_array($item['translated'])) {
130-
$messages[stripcslashes($item['ids']['singular'])] = stripcslashes($item['translated'][0]);
129+
if (!empty($item['ids']['singular'])) {
130+
$id = stripcslashes($item['ids']['singular']);
131131
if (isset($item['ids']['plural'])) {
132-
$plurals = $item['translated'];
133-
// PO are by definition indexed so sort by index.
134-
ksort($plurals);
135-
// Make sure every index is filled.
136-
end($plurals);
137-
$count = key($plurals);
138-
// Fill missing spots with '-'.
139-
$empties = array_fill(0, $count + 1, '-');
140-
$plurals += $empties;
141-
ksort($plurals);
142-
$messages[stripcslashes($item['ids']['plural'])] = stripcslashes(implode('|', $plurals));
132+
$id .= '|'.stripcslashes($item['ids']['plural']);
143133
}
144-
} elseif (!empty($item['ids']['singular'])) {
145-
$messages[stripcslashes($item['ids']['singular'])] = stripcslashes($item['translated']);
134+
135+
$translated = (array) $item['translated'];
136+
// PO are by definition indexed so sort by index.
137+
ksort($translated);
138+
// Make sure every index is filled.
139+
end($translated);
140+
$count = key($translated);
141+
// Fill missing spots with '-'.
142+
$empties = array_fill(0, $count + 1, '-');
143+
$translated += $empties;
144+
ksort($translated);
145+
146+
$messages[$id] = stripcslashes(implode('|', $translated));
146147
}
147148
}
148149
}

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ 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|foos' => 'bar|bars',
39+
'{0} no foos|one foo|%count% foos' => '{0} no bars|one bar|%count% bars',
40+
], $catalogue->all('domain1'));
3841
$this->assertEquals('en', $catalogue->getLocale());
3942
$this->assertEquals([new FileResource($resource)], $catalogue->getResources());
4043
}
@@ -89,10 +92,8 @@ public function testEscapedIdPlurals()
8992
$catalogue = $loader->load($resource, 'en', 'domain1');
9093

9194
$messages = $catalogue->all('domain1');
92-
$this->assertArrayHasKey('escaped "foo"', $messages);
93-
$this->assertArrayHasKey('escaped "foos"', $messages);
94-
$this->assertEquals('escaped "bar"', $messages['escaped "foo"']);
95-
$this->assertEquals('escaped "bar"|escaped "bars"', $messages['escaped "foos"']);
95+
$this->assertArrayHasKey('escaped "foo"|escaped "foos"', $messages);
96+
$this->assertEquals('escaped "bar"|escaped "bars"', $messages['escaped "foo"|escaped "foos"']);
9697
}
9798

9899
public function testSkipFuzzyTranslations()
@@ -106,4 +107,16 @@ public function testSkipFuzzyTranslations()
106107
$this->assertArrayNotHasKey('foo2', $messages);
107108
$this->assertArrayHasKey('foo3', $messages);
108109
}
110+
111+
public function testMissingPlurals()
112+
{
113+
$loader = new PoFileLoader();
114+
$resource = __DIR__.'/../fixtures/missing-plurals.po';
115+
$catalogue = $loader->load($resource, 'en', 'domain1');
116+
117+
$this->assertEquals([
118+
'foo|foos' => '-|bar|-|bars',
119+
], $catalogue->all('domain1'));
120+
$this->assertEquals('en', $catalogue->getLocale());
121+
}
109122
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
msgid "foo"
2+
msgid_plural "foos"
3+
msgstr[3] "bars"
4+
msgstr[1] "bar"

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ msgid_plural "foos"
33
msgstr[0] "bar"
44
msgstr[1] "bars"
55

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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy