Skip to content

Commit

Permalink
support for lang array
Browse files Browse the repository at this point in the history
  • Loading branch information
cevro committed Sep 10, 2024
1 parent 2f84dd9 commit 8296c40
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 30 deletions.
19 changes: 15 additions & 4 deletions src/Localization/GettextTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,21 @@ public function getSupportedLanguages(): array

/**
* @phpstan-template TValue
* @phpstan-param LangMap<TLang,TValue> $map
* @phpstan-param array<TLang,TValue>|LangMap<TLang,TValue> $map
* @phpstan-return TValue
*/
public function getVariant(LangMap $map)
public function getVariant($map)
{
return $map->get($this->lang);
if ($map instanceof LangMap) {
return $map->get($this->lang);
} elseif (is_array($map)) {
return $map[$this->lang];
}
throw new \InvalidArgumentException();//@phpstan-ignore-line
}

/**
* @param mixed|string $message
* @param array|LangMap|string|null $message
* @param string|int $parameters
*/
public function translate($message, ...$parameters): string
Expand All @@ -75,6 +80,12 @@ public function translate($message, ...$parameters): string
if ($message === '' || $message === null) {
return '';
}
if (is_array($message)) {
return (string)$this->getVariant($message);
}
if ($message instanceof LangMap) {
return (string)$message->get($this->lang);
}
if (isset($parameters[0])) {
return ngettext($message, $message, (int)$parameters[0]);
} else {
Expand Down
26 changes: 0 additions & 26 deletions src/Localization/LocalizedString.php

This file was deleted.

0 comments on commit 8296c40

Please sign in to comment.