From d9f124360853877c8d9d6362ac32150322fa4baf Mon Sep 17 00:00:00 2001 From: br4nnigan <10244835+br4nnigan@users.noreply.github.com> Date: Tue, 8 Jul 2025 11:34:46 +0200 Subject: [PATCH 1/2] added error on translation fail --- src/Translator.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Translator.php b/src/Translator.php index 657fa13..4a44989 100644 --- a/src/Translator.php +++ b/src/Translator.php @@ -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. */ @@ -127,6 +132,12 @@ 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); } @@ -274,9 +285,13 @@ 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; } From cae0ec209ee09a1f2d2b6fa85b7075169d0f895c Mon Sep 17 00:00:00 2001 From: William Desportes Date: Fri, 23 Oct 2026 11:34:49 +0200 Subject: [PATCH 2/2] Fix coding standard and add tests for translation errors Apply the phpcs fixes for the new error handling in gettext() and ngettext(), document the $error property as no longer parse-only and cover the new ERROR_KEY_DOES_NOT_EXIST cases. Co-Authored-By: DeepSeek --- src/Translator.php | 4 +++- tests/TranslatorTest.php | 50 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/Translator.php b/src/Translator.php index 4a44989..5e07473 100644 --- a/src/Translator.php +++ b/src/Translator.php @@ -92,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; @@ -138,6 +138,7 @@ public function gettext(string $msgid): string if (! $this->exists($msgid)) { $this->error = self::ERROR_KEY_DOES_NOT_EXIST; } + return $this->cache->get($msgid); } @@ -292,6 +293,7 @@ public function ngettext(string $msgid, string $msgidPlural, int $number): strin $key = $msgid . "\u{0}" . $msgidPlural; if (! $this->exists($key)) { $this->error = self::ERROR_KEY_DOES_NOT_EXIST; + return $number !== 1 ? $msgidPlural : $msgid; } diff --git a/tests/TranslatorTest.php b/tests/TranslatorTest.php index bd25df6..a18b744 100644 --- a/tests/TranslatorTest.php +++ b/tests/TranslatorTest.php @@ -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 */