From 690e70bc15a084de4e7b57a5fc4e3bc05268ee48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Tue, 23 May 2017 08:52:39 +0200 Subject: [PATCH] Correctly handle when mo file contains less plurals than formula says MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #18 Signed-off-by: Michal Čihař --- src/Translator.php | 3 +++ tests/MoFilesTest.php | 6 +++--- tests/data/lessplurals.mo | Bin 0 -> 169 bytes 3 files changed, 6 insertions(+), 3 deletions(-) create mode 100644 tests/data/lessplurals.mo diff --git a/src/Translator.php b/src/Translator.php index 6440b29..d042da6 100644 --- a/src/Translator.php +++ b/src/Translator.php @@ -301,6 +301,9 @@ public function ngettext($msgid, $msgidPlural, $number) $result = $this->cache_translations[$key]; $list = explode(chr(0), $result); + if (! isset($list[$select])) { + return $list[0]; + } return $list[$select]; } diff --git a/tests/MoFilesTest.php b/tests/MoFilesTest.php index cd92519..f2b7785 100644 --- a/tests/MoFilesTest.php +++ b/tests/MoFilesTest.php @@ -34,11 +34,11 @@ public function testMoFilePlurals($filename) { $parser = new PhpMyAdmin\MoTranslator\Translator($filename); $expected_2 = '%d sekundy'; - if (strpos($filename, 'plurals.mo') !== false || strpos($filename, 'noheader.mo') !== false) { - $expected_0 = '%d sekundy'; - } elseif (strpos($filename, 'invalid-formula.mo') !== false) { + if (strpos($filename, 'invalid-formula.mo') !== false || strpos($filename, 'lessplurals.mo') !== false) { $expected_0 = '%d sekunda'; $expected_2 = '%d sekunda'; + } elseif (strpos($filename, 'plurals.mo') !== false || strpos($filename, 'noheader.mo') !== false) { + $expected_0 = '%d sekundy'; } else { $expected_0 = '%d sekund'; } diff --git a/tests/data/lessplurals.mo b/tests/data/lessplurals.mo new file mode 100644 index 0000000000000000000000000000000000000000..5288d9ec1d7ab3c64eb82eec7e89cf8b81a082ca GIT binary patch literal 169 zcmca7#4?ou2$+Fb28d07m=%b9fLH{G6M&cvh)aPO#IFHjE+C!=#4JEO2Z-5$cp(sj z%m4u