Skip to content

Commit 25f1e4d

Browse files
committed
added error on translation fail
1 parent 28b0378 commit 25f1e4d

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/Translator.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ class Translator
7676
*/
7777
public const ERROR_READING = 3;
7878

79+
/**
80+
* Error on translating because key not found.
81+
*/
82+
public const ERROR_KEY_DOES_NOT_EXIST = 4;
83+
7984
/**
8085
* Big endian mo file magic bytes.
8186
*/
@@ -127,6 +132,12 @@ public function __construct(CacheInterface|string|null $cache)
127132
*/
128133
public function gettext(string $msgid): string
129134
{
135+
// reset error
136+
$this->error = self::ERROR_NONE;
137+
138+
if (! $this->exists($msgid)) {
139+
$this->error = self::ERROR_KEY_DOES_NOT_EXIST;
140+
}
130141
return $this->cache->get($msgid);
131142
}
132143

@@ -274,9 +285,13 @@ private function selectString(int $n): int
274285
*/
275286
public function ngettext(string $msgid, string $msgidPlural, int $number): string
276287
{
288+
// reset error
289+
$this->error = self::ERROR_NONE;
290+
277291
// this should contains all strings separated by NULLs
278292
$key = $msgid . "\u{0}" . $msgidPlural;
279-
if (! $this->cache->has($key)) {
293+
if (! $this->exists($key)) {
294+
$this->error = self::ERROR_KEY_DOES_NOT_EXIST;
280295
return $number !== 1 ? $msgidPlural : $msgid;
281296
}
282297

0 commit comments

Comments
 (0)