Skip to content

Commit ca67814

Browse files
committed
Added a fallback to plurals if no translations are loaded #35
1 parent 18f499e commit ca67814

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

src/Translator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,9 @@ public function dnpgettext($domain, $context, $original, $plural, $value)
244244
*/
245245
public function isPlural($domain, $n)
246246
{
247+
//Not loaded domain, use a fallback
247248
if (!isset($this->plurals[$domain])) {
248-
throw new \InvalidArgumentException("The gettext domain '$domain' is not loaded");
249+
return $n == 1 ? 1 : 2;
249250
}
250251

251252
if (!isset($this->plurals[$domain]['function'])) {

tests/TranslatorTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,14 @@ public function testPlural()
6060
*/
6161
$this->assertEquals('more', $t->ngettext("single", "more", 3), "non-plural fallback failed");
6262
}
63+
64+
public function testNonLoadedTranslations()
65+
{
66+
$t = new \Gettext\Translator();
67+
68+
$this->assertEquals('hello', $t->gettext('hello'));
69+
$this->assertEquals('worlds', $t->ngettext('world', 'worlds', 0));
70+
$this->assertEquals('world', $t->ngettext('world', 'worlds', 1));
71+
$this->assertEquals('worlds', $t->ngettext('world', 'worlds', 2));
72+
}
6373
}

0 commit comments

Comments
 (0)