Skip to content

Commit

Permalink
[Translator] Load plurals from po files properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Stadly authored and fabpot committed Jul 8, 2019
1 parent 15e9eec commit 6b69a99
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 20 deletions.
31 changes: 16 additions & 15 deletions src/Symfony/Component/Translation/Loader/PoFileLoader.php
Expand Up @@ -126,23 +126,24 @@ protected function loadResource($resource)
*/
private function addMessage(array &$messages, array $item)
{
if (\is_array($item['translated'])) {
$messages[stripcslashes($item['ids']['singular'])] = stripcslashes($item['translated'][0]);
if (!empty($item['ids']['singular'])) {
$id = stripcslashes($item['ids']['singular']);
if (isset($item['ids']['plural'])) {
$plurals = $item['translated'];
// PO are by definition indexed so sort by index.
ksort($plurals);
// Make sure every index is filled.
end($plurals);
$count = key($plurals);
// Fill missing spots with '-'.
$empties = array_fill(0, $count + 1, '-');
$plurals += $empties;
ksort($plurals);
$messages[stripcslashes($item['ids']['plural'])] = stripcslashes(implode('|', $plurals));
$id .= '|'.stripcslashes($item['ids']['plural']);
}
} elseif (!empty($item['ids']['singular'])) {
$messages[stripcslashes($item['ids']['singular'])] = stripcslashes($item['translated']);

$translated = (array) $item['translated'];
// PO are by definition indexed so sort by index.
ksort($translated);
// Make sure every index is filled.
end($translated);
$count = key($translated);
// Fill missing spots with '-'.
$empties = array_fill(0, $count + 1, '-');
$translated += $empties;
ksort($translated);

$messages[$id] = stripcslashes(implode('|', $translated));
}
}
}
Expand Up @@ -34,7 +34,10 @@ public function testLoadPlurals()
$resource = __DIR__.'/../fixtures/plurals.po';
$catalogue = $loader->load($resource, 'en', 'domain1');

$this->assertEquals(['foo' => 'bar', 'foos' => 'bar|bars'], $catalogue->all('domain1'));
$this->assertEquals([
'foo|foos' => 'bar|bars',
'{0} no foos|one foo|%count% foos' => '{0} no bars|one bar|%count% bars',
], $catalogue->all('domain1'));
$this->assertEquals('en', $catalogue->getLocale());
$this->assertEquals([new FileResource($resource)], $catalogue->getResources());
}
Expand Down Expand Up @@ -89,10 +92,8 @@ public function testEscapedIdPlurals()
$catalogue = $loader->load($resource, 'en', 'domain1');

$messages = $catalogue->all('domain1');
$this->assertArrayHasKey('escaped "foo"', $messages);
$this->assertArrayHasKey('escaped "foos"', $messages);
$this->assertEquals('escaped "bar"', $messages['escaped "foo"']);
$this->assertEquals('escaped "bar"|escaped "bars"', $messages['escaped "foos"']);
$this->assertArrayHasKey('escaped "foo"|escaped "foos"', $messages);
$this->assertEquals('escaped "bar"|escaped "bars"', $messages['escaped "foo"|escaped "foos"']);
}

public function testSkipFuzzyTranslations()
Expand All @@ -106,4 +107,16 @@ public function testSkipFuzzyTranslations()
$this->assertArrayNotHasKey('foo2', $messages);
$this->assertArrayHasKey('foo3', $messages);
}

public function testMissingPlurals()
{
$loader = new PoFileLoader();
$resource = __DIR__.'/../fixtures/missing-plurals.po';
$catalogue = $loader->load($resource, 'en', 'domain1');

$this->assertEquals([
'foo|foos' => '-|bar|-|bars',
], $catalogue->all('domain1'));
$this->assertEquals('en', $catalogue->getLocale());
}
}
@@ -0,0 +1,4 @@
msgid "foo"
msgid_plural "foos"
msgstr[3] "bars"
msgstr[1] "bar"
2 changes: 2 additions & 0 deletions src/Symfony/Component/Translation/Tests/fixtures/plurals.po
Expand Up @@ -3,3 +3,5 @@ msgid_plural "foos"
msgstr[0] "bar"
msgstr[1] "bars"

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

0 comments on commit 6b69a99

Please sign in to comment.