Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Translator] Load plurals from po files properly #31266

Merged
merged 1 commit into from Jul 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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, '-');
Stadly marked this conversation as resolved.
Show resolved Hide resolved
$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"