Skip to content

Commit

Permalink
bug #31267 [Translator] Load plurals from mo files properly (Stadly)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.4 branch.

Discussion
----------

[Translator] Load plurals from mo 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 mo files.
```
msgid "foo"
msgid_plural "foos"
msgstr[0] "bar"
msgstr[1] "bars"
```

Before, the mo entry above was treated as two entries, which doesn't make sense:
```
'foo' => 'bar'
'foos' => '{0} bar|{1} bars'
```

With this PR, it is treated as one entry:
```
'foo|foos' => 'bar|bars'
```

This PR does the same as #31266, just for mo files instead of po files. Note, however, that the old behavior differed between po and mo files: `'foos' => 'bar|bars'` for po files and `'foos' => '{0} bar|{1} bars'` for mo files.

Commits
-------

97d28b5 Load plurals from mo files properly
  • Loading branch information
fabpot committed Jul 8, 2019
2 parents 9fc8d2e + 97d28b5 commit feab919
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
13 changes: 4 additions & 9 deletions src/Symfony/Component/Translation/Loader/MoFileLoader.php
Expand Up @@ -111,17 +111,12 @@ protected function loadResource($resource)
$ids = ['singular' => $singularId, 'plural' => $pluralId];
$item = compact('ids', 'translated');

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

Expand Down
Expand Up @@ -34,7 +34,10 @@ public function testLoadPlurals()
$resource = __DIR__.'/../fixtures/plurals.mo';
$catalogue = $loader->load($resource, 'en', 'domain1');

$this->assertEquals(['foo' => 'bar', 'foos' => '{0} bar|{1} 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
Binary file modified src/Symfony/Component/Translation/Tests/fixtures/plurals.mo
Binary file not shown.

0 comments on commit feab919

Please sign in to comment.