Skip to content

added error on translation fail #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
17 changes: 16 additions & 1 deletion 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 Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
}

Expand Down
Loading