From 25f1e4d7fbd086c4e005350bf7671d57cc4dae52 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] 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 9e0879a..9285c6f 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; }