Skip to content

[Translation] Po/MoFileLoader parse plurization rules #3023

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

Merged
merged 1 commit into from
Jan 4, 2012
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Translation] Po/MoFileLoader parse plurization rules
  • Loading branch information
stealth35 committed Jan 3, 2012
commit f4890c24f9435ab1f3b51f4cad94fbe212290bad
12 changes: 8 additions & 4 deletions src/Symfony/Component/Translation/Loader/MoFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,16 @@ private function parse($resource)
$item = compact('ids', 'translated');

if (is_array($item['translated'])) {
$messages[$item['ids']['singular']] = stripslashes($item['translated'][0]);
$messages[$item['ids']['singular']] = stripcslashes($item['translated'][0]);
if (isset($item['ids']['plural'])) {
$messages[$item['ids']['plural']] = stripslashes(end($item['translated']));
$plurals = array();
foreach ($item['translated'] as $plural => $translated) {
$plurals[] = sprintf('{%d} %s', $plural, $translated);
}
$messages[$item['ids']['plural']] = stripcslashes(implode('|', $plurals));
}
} elseif($item['ids']['singular']) {
$messages[$item['ids']['singular']] = stripslashes($item['translated']);
} elseif(!empty($item['ids']['singular'])) {
$messages[$item['ids']['singular']] = stripcslashes($item['translated']);
}
}

Expand Down
11 changes: 8 additions & 3 deletions src/Symfony/Component/Translation/Loader/PoFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,13 @@ private function parse($resource)
if (is_array($item['translated'])) {
$messages[$item['ids']['singular']] = stripslashes($item['translated'][0]);
if (isset($item['ids']['plural'])) {
$messages[$item['ids']['plural']] = stripslashes(end($item['translated']));
$plurals = array();
foreach ($item['translated'] as $plural => $translated) {
$plurals[] = sprintf('{%d} %s', $plural, $translated);
}
$messages[$item['ids']['plural']] = stripcslashes(implode('|', $plurals));
}
} elseif($item['ids']['singular']) {
} elseif(!empty($item['ids']['singular'])) {
$messages[$item['ids']['singular']] = stripslashes($item['translated']);
}
$item = $defaults;
Expand All @@ -93,7 +97,8 @@ private function parse($resource)
} elseif (substr($line, 0, 14) === 'msgid_plural "') {
$item['ids']['plural'] = substr($line, 14, -1);
} elseif (substr($line, 0, 7) === 'msgstr[') {
$item['translated'][(integer) substr($line, 7, 1)] = substr($line, 11, -1);
$size = strpos($line, ']');
$item['translated'][(integer) substr($line, 7, 1)] = substr($line, $size + 3, -1);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ public function testLoad()
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
}

public function testLoadPlurals()
{
$loader = new MoFileLoader();
$resource = __DIR__.'/../fixtures/plurals.mo';
$catalogue = $loader->load($resource, 'en', 'domain1');

$this->assertEquals(array('foo' => 'bar', 'foos' => '{0} bar|{1} bars'), $catalogue->all('domain1'));
$this->assertEquals('en', $catalogue->getLocale());
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
}

/**
* @expectedException \InvalidArgumentException
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ public function testLoad()
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
}

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

$this->assertEquals(array('foo' => 'bar', 'foos' => '{0} bar|{1} bars'), $catalogue->all('domain1'));
$this->assertEquals('en', $catalogue->getLocale());
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
}

public function testLoadDoesNothingIfEmpty()
{
$loader = new PoFileLoader();
Expand Down
Binary file not shown.
5 changes: 5 additions & 0 deletions tests/Symfony/Tests/Component/Translation/fixtures/plurals.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
msgid "foo"
msgid_plural "foos"
msgstr[0] "bar"
msgstr[1] "bars"