Skip to content

Commit

Permalink
Fixed handling of corrupted formulas in mo files
Browse files Browse the repository at this point in the history
Fixes #17

Signed-off-by: Michal Čihař <[email protected]>
  • Loading branch information
nijel committed May 23, 2017
1 parent b801c2e commit 83b74d8
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
10 changes: 7 additions & 3 deletions src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,13 @@ private function selectString($n)
if (is_null($this->pluralexpression)) {
$this->pluralexpression = new ExpressionLanguage();
}
$plural = $this->pluralexpression->evaluate(
$this->getPluralForms(), array('n' => $n)
);
try {
$plural = $this->pluralexpression->evaluate(
$this->getPluralForms(), array('n' => $n)
);
} catch (\Exception $e) {
$plural = 0;
}

if ($plural >= $this->pluralcount) {
$plural = $this->pluralcount - 1;
Expand Down
16 changes: 10 additions & 6 deletions tests/MoFilesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,17 @@ public function testMoFileTranslate($filename)
public function testMoFilePlurals($filename)
{
$parser = new PhpMyAdmin\MoTranslator\Translator($filename);
$expected_2 = '%d sekundy';
if (strpos($filename, 'plurals.mo') !== false) {
$expected = '%d sekundy';
$expected_0 = '%d sekundy';
} elseif (strpos($filename, 'invalid-formula.mo') !== false) {
$expected_0 = '%d sekunda';
$expected_2 = '%d sekunda';
} else {
$expected = '%d sekund';
$expected_0 = '%d sekund';
}
$this->assertEquals(
$expected,
$expected_0,
$parser->ngettext(
'%d second',
'%d seconds',
Expand All @@ -55,23 +59,23 @@ public function testMoFilePlurals($filename)
)
);
$this->assertEquals(
'%d sekundy',
$expected_2,
$parser->ngettext(
'%d second',
'%d seconds',
2
)
);
$this->assertEquals(
$expected,
$expected_0,
$parser->ngettext(
'%d second',
'%d seconds',
5
)
);
$this->assertEquals(
$expected,
$expected_0,
$parser->ngettext(
'%d second',
'%d seconds',
Expand Down
Binary file added tests/data/invalid-formula.mo
Binary file not shown.
Binary file added tests/data/not-translated/invalid-equation.mo
Binary file not shown.

0 comments on commit 83b74d8

Please sign in to comment.