merged branch stealth35/fix_2630 (PR #3023)
Commits
-------
f4890c2
[Translation] Po/MoFileLoader parse plurization rules
Discussion
----------
[Translation] Po/MoFileLoader parse plurization rules
Bug fix: yes
Feature addition: no
Backwards compatibility break: no
Symfony2 tests pass: [![Build Status](https://secure.travis-ci.org/stealth35/symfony.png?branch=fix_2630)](http://travis-ci.org/stealth35/symfony)Fixes the following tickets: #2630
Todo: Not happy with the pluralize style
```
msgid "foo"
msgid_plural "foos"
msgstr[0] "bar"
msgstr[1] "bars"
```
to
```
array (
'foo' => 'bar',
'foos' => '{0} bar|{1} bars'
)
```
This commit is contained in:
commit
54a8b58785
@ -138,12 +138,16 @@ class MoFileLoader extends ArrayLoader implements LoaderInterface
|
||||
$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']);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -71,9 +71,13 @@ class PoFileLoader extends ArrayLoader implements LoaderInterface
|
||||
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;
|
||||
@ -93,7 +97,8 @@ class PoFileLoader extends ArrayLoader implements LoaderInterface
|
||||
} 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -27,6 +27,17 @@ class MoFileLoaderTest extends \PHPUnit_Framework_TestCase
|
||||
$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
|
||||
*/
|
||||
|
@ -27,6 +27,17 @@ class PoFileLoaderTest extends \PHPUnit_Framework_TestCase
|
||||
$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();
|
||||
|
BIN
tests/Symfony/Tests/Component/Translation/fixtures/plurals.mo
Normal file
BIN
tests/Symfony/Tests/Component/Translation/fixtures/plurals.mo
Normal file
Binary file not shown.
@ -0,0 +1,5 @@
|
||||
msgid "foo"
|
||||
msgid_plural "foos"
|
||||
msgstr[0] "bar"
|
||||
msgstr[1] "bars"
|
||||
|
Reference in New Issue
Block a user