Skip to content

Commit feab919

Browse files
committed
bug #31267 [Translator] Load plurals from mo files properly (Stadly)
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
2 parents 9fc8d2e + 97d28b5 commit feab919

File tree

3 files changed

+8
-10
lines changed

3 files changed

+8
-10
lines changed

src/Symfony/Component/Translation/Loader/MoFileLoader.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,12 @@ protected function loadResource($resource)
111111
$ids = ['singular' => $singularId, 'plural' => $pluralId];
112112
$item = compact('ids', 'translated');
113113

114-
if (\is_array($item['translated'])) {
115-
$messages[$item['ids']['singular']] = stripcslashes($item['translated'][0]);
114+
if (!empty($item['ids']['singular'])) {
115+
$id = $item['ids']['singular'];
116116
if (isset($item['ids']['plural'])) {
117-
$plurals = [];
118-
foreach ($item['translated'] as $plural => $translated) {
119-
$plurals[] = sprintf('{%d} %s', $plural, $translated);
120-
}
121-
$messages[$item['ids']['plural']] = stripcslashes(implode('|', $plurals));
117+
$id .= '|'.$item['ids']['plural'];
122118
}
123-
} elseif (!empty($item['ids']['singular'])) {
124-
$messages[$item['ids']['singular']] = stripcslashes($item['translated']);
119+
$messages[$id] = stripcslashes(implode('|', (array) $item['translated']));
125120
}
126121
}
127122

src/Symfony/Component/Translation/Tests/Loader/MoFileLoaderTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ public function testLoadPlurals()
3434
$resource = __DIR__.'/../fixtures/plurals.mo';
3535
$catalogue = $loader->load($resource, 'en', 'domain1');
3636

37-
$this->assertEquals(['foo' => 'bar', 'foos' => '{0} bar|{1} bars'], $catalogue->all('domain1'));
37+
$this->assertEquals([
38+
'foo|foos' => 'bar|bars',
39+
'{0} no foos|one foo|%count% foos' => '{0} no bars|one bar|%count% bars',
40+
], $catalogue->all('domain1'));
3841
$this->assertEquals('en', $catalogue->getLocale());
3942
$this->assertEquals([new FileResource($resource)], $catalogue->getResources());
4043
}
Binary file not shown.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy