From 97d28b5e4e23cca3dbe090e29d427275f4d98bf6 Mon Sep 17 00:00:00 2001 From: Stadly Date: Fri, 26 Apr 2019 12:01:40 +0200 Subject: [PATCH] Load plurals from mo files properly --- .../Translation/Loader/MoFileLoader.php | 13 ++++--------- .../Tests/Loader/MoFileLoaderTest.php | 5 ++++- .../Translation/Tests/fixtures/plurals.mo | Bin 74 -> 448 bytes 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/Symfony/Component/Translation/Loader/MoFileLoader.php b/src/Symfony/Component/Translation/Loader/MoFileLoader.php index 74d2b2f4411d..4b249df024ec 100644 --- a/src/Symfony/Component/Translation/Loader/MoFileLoader.php +++ b/src/Symfony/Component/Translation/Loader/MoFileLoader.php @@ -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'])); } } diff --git a/src/Symfony/Component/Translation/Tests/Loader/MoFileLoaderTest.php b/src/Symfony/Component/Translation/Tests/Loader/MoFileLoaderTest.php index 63de5cebaa4d..d6adecb1736f 100644 --- a/src/Symfony/Component/Translation/Tests/Loader/MoFileLoaderTest.php +++ b/src/Symfony/Component/Translation/Tests/Loader/MoFileLoaderTest.php @@ -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()); } diff --git a/src/Symfony/Component/Translation/Tests/fixtures/plurals.mo b/src/Symfony/Component/Translation/Tests/fixtures/plurals.mo index 6445e77beab595289cd154ea253c4e49dfd6af47..3945ad95beae6c9ce059cb8d5209c53653c47440 100644 GIT binary patch literal 448 zcmYL_&rSkC493@giP@uP5A{Hzhe047$dWxF7!rsZvl@?^6|RIMVJP@`5{?BFK$r{hUKD*w_WrkMko+TAzA#5p zh<$%fwf5NPz3~C8!RxhV@?u>p^om_Am{C@(3|6YI&B7+Y%TU_1)q#yn&l%2AXkK*U z%;-54P7NmNfs8FRClF`1x#}81C#AYZN5NBf^iukce`|==soWsj3Y|96G(?`Qa7HR8 zTu40{jC#Ad&3Ys5YIel(+uKs6I(l`N%L+^GK=F*ml1uONzH0CK{P-yu)#E}>N}eUq r9+z%=Qv$^Cqq-p`vw1|OX;M09)m!yu9F5UGz5fS3b_Eugd`kOrxNfwcU51|TkGNJ=aM;bH~=*B}U7