From 654b02bf034ecb70612954b444b559f67129c4ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mi=C5=A1o=20=C4=8Cerve=C5=88=C3=A1k?= Date: Tue, 3 Sep 2024 21:46:30 +0200 Subject: [PATCH 1/4] Lang map --- src/BaseComponent/BaseComponent.php | 4 +- src/Localization/GettextTranslator.php | 25 ++++++++++--- src/Localization/LangMap.php | 51 ++++++++++++++++++++++++++ src/Localization/LocalizedString.php | 41 ++++----------------- 4 files changed, 81 insertions(+), 40 deletions(-) create mode 100644 src/Localization/LangMap.php diff --git a/src/BaseComponent/BaseComponent.php b/src/BaseComponent/BaseComponent.php index c64d501..4f28d64 100644 --- a/src/BaseComponent/BaseComponent.php +++ b/src/BaseComponent/BaseComponent.php @@ -5,7 +5,8 @@ namespace Fykosak\Utils\BaseComponent; use Fykosak\Utils\Localization\GettextTranslator; -use Nette\Application\UI\{Control, Template}; +use Nette\Application\UI\Control; +use Nette\Application\UI\Template; use Nette\DI\Container; /** @@ -37,6 +38,7 @@ protected function createTemplate(): Template /** @var \Nette\Bridges\ApplicationLatte\Template $template */ $template = parent::createTemplate(); $template->setTranslator($this->translator); + $template->translator = $this->translator; return $template; } } diff --git a/src/Localization/GettextTranslator.php b/src/Localization/GettextTranslator.php index 61cc5df..9189780 100644 --- a/src/Localization/GettextTranslator.php +++ b/src/Localization/GettextTranslator.php @@ -6,15 +6,19 @@ use Nette\Localization\Translator; +/** + * @phpstan-template TLang of string + */ class GettextTranslator implements Translator { - /** @phpstan-var array */ + /** @phpstan-var array */ public array $locales; private string $localeDir; - public ?string $lang = null; + /** @phpstan-var TLang */ + public string $lang; /** - * @phpstan-param array $locales + * @phpstan-param array $locales */ public function __construct(array $locales, string $localeDir) { @@ -24,7 +28,7 @@ public function __construct(array $locales, string $localeDir) /** * - * @param string $lang ISO 639-1 + * @param TLang $lang ISO 639-1 * @throws UnsupportedLanguageException */ public function setLang(string $lang): void @@ -44,19 +48,30 @@ public function setLang(string $lang): void } /** - * @return string[] + * @return TLang[] */ public function getSupportedLanguages(): array { return array_keys($this->locales); } + /** + * @phpstan-template TValue + * @phpstan-param LangMap $map + * @phpstan-return TValue + */ + public function getVariant(LangMap $map) + { + return $map->get($this->lang); + } + /** * @param mixed|string $message * @param string|int $parameters */ public function translate($message, ...$parameters): string { + if ($message === '' || $message === null) { return ''; } diff --git a/src/Localization/LangMap.php b/src/Localization/LangMap.php new file mode 100644 index 0000000..c078507 --- /dev/null +++ b/src/Localization/LangMap.php @@ -0,0 +1,51 @@ + + */ + protected array $variants; + + /** + * @phpstan-param array $variants + */ + public function __construct(array $variants) + { + $this->variants = $variants; + } + + /** + * @phpstan-param TLang $lang + * @phpstan-return TValue + */ + public function __get(string $lang) + { + return $this->get($lang); + } + + /** + * @phpstan-param TLang $lang + * @phpstan-return TValue + */ + public function get(string $lang) + { + return $this->variants[$lang] ?? null; + } + + /** + * @phpstan-return array + */ + public function __serialize(): array + { + return $this->variants; + } +} diff --git a/src/Localization/LocalizedString.php b/src/Localization/LocalizedString.php index 18235b0..2a6552c 100644 --- a/src/Localization/LocalizedString.php +++ b/src/Localization/LocalizedString.php @@ -5,49 +5,22 @@ namespace Fykosak\Utils\Localization; /** - * @template L of string + * @template TLang of string + * @deprecated + * @phpstan-extends LangMap */ -class LocalizedString +class LocalizedString extends LangMap { /** - * @phpstan-var array - */ - private array $texts; - - /** - * @phpstan-param array $texts - */ - public function __construct(array $texts) - { - $this->texts = $texts; - } - - /** - * @phpstan-param L $lang - */ - public function __get(string $lang): ?string - { - return $this->getText($lang); - } - - /** - * @phpstan-param L $lang + * @phpstan-param TLang $lang */ public function getText(string $lang): ?string { - return $this->texts[$lang] ?? null; - } - - /** - * @phpstan-return array - */ - public function __serialize(): array - { - return $this->texts; + return $this->variants[$lang] ?? null; } public function __toString(): string { - return join('/', $this->texts); + return join('/', $this->variants); } } From 8296c40de49fba851a3b14cf2128e70c69d7bba1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mi=C5=A1o=20=C4=8Cerve=C5=88=C3=A1k?= Date: Tue, 10 Sep 2024 22:22:20 +0200 Subject: [PATCH 2/4] support for lang array --- src/Localization/GettextTranslator.php | 19 +++++++++++++++---- src/Localization/LocalizedString.php | 26 -------------------------- 2 files changed, 15 insertions(+), 30 deletions(-) delete mode 100644 src/Localization/LocalizedString.php diff --git a/src/Localization/GettextTranslator.php b/src/Localization/GettextTranslator.php index 9189780..b162f81 100644 --- a/src/Localization/GettextTranslator.php +++ b/src/Localization/GettextTranslator.php @@ -57,16 +57,21 @@ public function getSupportedLanguages(): array /** * @phpstan-template TValue - * @phpstan-param LangMap $map + * @phpstan-param array|LangMap $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 @@ -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 { diff --git a/src/Localization/LocalizedString.php b/src/Localization/LocalizedString.php deleted file mode 100644 index 2a6552c..0000000 --- a/src/Localization/LocalizedString.php +++ /dev/null @@ -1,26 +0,0 @@ - - */ -class LocalizedString extends LangMap -{ - /** - * @phpstan-param TLang $lang - */ - public function getText(string $lang): ?string - { - return $this->variants[$lang] ?? null; - } - - public function __toString(): string - { - return join('/', $this->variants); - } -} From a5d30d08378f1493289ebef1a262a871cbdd8323 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mi=C5=A1o=20=C4=8Cerve=C5=88=C3=A1k?= Date: Tue, 10 Sep 2024 22:25:01 +0200 Subject: [PATCH 3/4] update WF --- .github/workflows/php-psr.yml | 2 +- .github/workflows/php.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/php-psr.yml b/.github/workflows/php-psr.yml index c230aac..6f2de99 100644 --- a/.github/workflows/php-psr.yml +++ b/.github/workflows/php-psr.yml @@ -28,7 +28,7 @@ jobs: name: Test - if: failure() name: Failure output - uses: actions/upload-artifact@v2 + uses: actions/upload-artifact@v4 with: name: output path: tests/**/*.actual diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index a83b75c..a54cdc9 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -33,7 +33,7 @@ jobs: path: coverage.html - if: failure() name: Failure output - uses: actions/upload-artifact@v2 + uses: actions/upload-artifact@v4 with: name: output path: tests/**/*.actual From c5563c74ba08ab88a423ae56dd190ef185e1ffcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mi=C5=A1o=20=C4=8Cerve=C5=88=C3=A1k?= Date: Tue, 10 Sep 2024 22:26:16 +0200 Subject: [PATCH 4/4] fix no2 --- .github/workflows/php.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index a54cdc9..d20f815 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -27,7 +27,7 @@ jobs: - run: composer run-script testCoverage name: Test - name: Archive code coverage results - uses: actions/upload-artifact@v2 + uses: actions/upload-artifact@v4 with: name: code-coverage-report path: coverage.html