diff --git a/src/Translator.php b/src/Translator.php index 9e0879a..ebdf30d 100644 --- a/src/Translator.php +++ b/src/Translator.php @@ -133,10 +133,18 @@ public function gettext(string $msgid): string /** * Check if a string is translated. * - * @param string $msgid String to be checked + * A string with plural forms is stored under its singular and plural msgids + * together, so pass the plural msgid as well to check it, as for ngettext(). + * + * @param string $msgid String to be checked + * @param string|null $msgidPlural Plural form of the string, if it has one */ - public function exists(string $msgid): bool + public function exists(string $msgid, string|null $msgidPlural = null): bool { + if ($msgidPlural !== null) { + $msgid .= "\u{0}" . $msgidPlural; + } + return $this->cache->has($msgid); } diff --git a/tests/MoFilesTest.php b/tests/MoFilesTest.php index 0795645..c06637c 100644 --- a/tests/MoFilesTest.php +++ b/tests/MoFilesTest.php @@ -112,6 +112,15 @@ public function testExists(string $file): void self::assertFalse($parser->exists('Column parser')); } + #[DataProvider('provideMoFiles')] + public function testExistsWithPlural(string $file): void + { + $parser = $this->getTranslator($file); + self::assertTrue($parser->exists('%d second', '%d seconds')); + self::assertFalse($parser->exists('%d second', '%d minutes')); + self::assertFalse($parser->exists('%d second')); + } + /** * @param string $pattern path names pattern to match *