Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ class Translator
*/
public const ERROR_READING = 3;

/**
* Error on translating because key not found.
*/
public const ERROR_KEY_DOES_NOT_EXIST = 4;

/**
* Big endian mo file magic bytes.
*/
Expand All @@ -87,7 +92,7 @@ class Translator
public const MAGIC_LE = "\xde\x12\x04\x95";

/**
* Parse error code (0 if no error).
* Error code (0 if no error).
*/
public int $error = self::ERROR_NONE;

Expand Down Expand Up @@ -127,6 +132,13 @@ public function __construct(CacheInterface|string|null $cache)
*/
public function gettext(string $msgid): string
{
// reset error
$this->error = self::ERROR_NONE;

if (! $this->exists($msgid)) {
$this->error = self::ERROR_KEY_DOES_NOT_EXIST;
}

return $this->cache->get($msgid);
}

Expand Down Expand Up @@ -274,9 +286,14 @@ private function selectString(int $n): int
*/
public function ngettext(string $msgid, string $msgidPlural, int $number): string
{
// reset error
$this->error = self::ERROR_NONE;

// this should contains all strings separated by NULLs
$key = $msgid . "\u{0}" . $msgidPlural;
if (! $this->cache->has($key)) {
if (! $this->exists($key)) {
$this->error = self::ERROR_KEY_DOES_NOT_EXIST;

return $number !== 1 ? $msgidPlural : $msgid;
}

Expand Down
50 changes: 50 additions & 0 deletions tests/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,56 @@ public function testGettext(): void
self::assertSame('Test', $translator->gettext('Test'));
}

/**
* Test on gettext with a missing translation
*/
public function testGettextSetsErrorOnMissingTranslation(): void
{
$translator = $this->getTranslator('');
self::assertSame('Missing', $translator->gettext('Missing'));
self::assertSame(Translator::ERROR_KEY_DOES_NOT_EXIST, $translator->error);
}

/**
* Test the error is reset by gettext
*/
public function testGettextResetsError(): void
{
$translator = $this->getTranslator('');
$translator->setTranslation('Test', 'Translation');

$translator->gettext('Missing');
self::assertSame(Translator::ERROR_KEY_DOES_NOT_EXIST, $translator->error);

self::assertSame('Translation', $translator->gettext('Test'));
self::assertSame(Translator::ERROR_NONE, $translator->error);
}

/**
* Test on ngettext with a missing translation
*/
public function testNgettextSetsErrorOnMissingTranslation(): void
{
$translator = $this->getTranslator('');
self::assertSame('Missing', $translator->ngettext('Missing', 'Missings', 1));
self::assertSame(Translator::ERROR_KEY_DOES_NOT_EXIST, $translator->error);
}

/**
* Test the error is reset by ngettext
*/
public function testNgettextResetsError(): void
{
$translator = $this->getTranslator('');
$translator->setTranslation("Test\u{0}Tests", "Translation\u{0}Translations");

$translator->ngettext('Missing', 'Missings', 1);
self::assertSame(Translator::ERROR_KEY_DOES_NOT_EXIST, $translator->error);

self::assertSame('Translations', $translator->ngettext('Test', 'Tests', 2));
self::assertSame(Translator::ERROR_NONE, $translator->error);
}

/**
* Test set a translation
*/
Expand Down
Loading