Skip to content

Commit

Permalink
Merge branch 'dev-context-translator' into dev-datetime-period
Browse files Browse the repository at this point in the history
  • Loading branch information
cevro committed Oct 19, 2024
2 parents 9daae22 + c5563c7 commit 61bf8db
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/php-psr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ 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
- if: failure()
name: Failure output
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: output
path: tests/**/*.actual
4 changes: 3 additions & 1 deletion src/Components/DIComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
namespace Fykosak\Utils\Components;

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;

/**
Expand Down Expand Up @@ -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;
}
}
29 changes: 28 additions & 1 deletion src/Localization/GettextTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@

use Nette\Localization\Translator;

/**
* @phpstan-template TLang of string
*/
class GettextTranslator implements Translator
{
/** @phpstan-var \BackedEnum&LangEnum<TLang> */
public LangEnum & \BackedEnum $lang;


/** @phpstan-var array<TLang,string> */
public array $locales;

/**
* @phpstan-param class-string<LangEnum>|string $langEnumClass
*/
Expand All @@ -19,6 +27,8 @@ public function __construct(
}

/**
*
* @param TLang $lang ISO 639-1
* @throws UnsupportedLanguageException
*/
public function setLang(LangEnum & \BackedEnum $lang): void
Expand All @@ -38,14 +48,31 @@ public function setLang(LangEnum & \BackedEnum $lang): void
}

/**
* @param mixed|string $message
* @phpstan-template TValue
* @phpstan-param array<TLang,TValue>|LangMap<TLang,TValue> $map
* @phpstan-return TValue
*/
public function getVariant(array|LangMap $map)
{
$this->lang->getVariant($map);
}

/**
* @param array|LangMap|string|null $message
* @param string|int $parameters
*/
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)$this->getVariant($message);
}
if (isset($parameters[0])) {
return ngettext($message, $message, (int)$parameters[0]);
} else {
Expand Down
6 changes: 6 additions & 0 deletions src/Localization/LangEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@

namespace Fykosak\Utils\Localization;

/**
* @phpstan-template TLang of string
*/
interface LangEnum
{
/**
* @phpstan-return array<TLang,string>
*/
public static function getLocales(): array;

public function getLocaleDir(): string;
Expand Down
42 changes: 42 additions & 0 deletions src/Localization/LangMap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Fykosak\Utils\Localization;

/**
* @phpstan-template TLang of string
* @phpstan-template TValue
*/
class LangMap
{
/**
* @phpstan-var array<TLang,TValue>
*/
protected array $variants;

/**
* @phpstan-param array<TLang,TValue> $variants
*/
public function __construct(array $variants)
{
$this->variants = $variants;
}

/**
* @phpstan-param TLang $key
* @phpstan-return TValue
*/
public function get(string $key): mixed
{
return $this->variants[$key] ?? null;
}

/**
* @phpstan-return array<TLang,TValue>
*/
public function __serialize(): array
{
return $this->variants;
}
}
25 changes: 25 additions & 0 deletions src/Localization/LangTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Fykosak\Utils\Localization;

/**
* @phpstan-template TLang of string
*/
trait LangTrait
{
/**
* @phpstan-template TValue
* @phpstan-param array<TLang,TValue>|LangMap<TLang,TValue> $map
* @phpstan-return TValue
*/
final public function getVariant(array|LangMap $map): mixed
{
if ($map instanceof LangMap) {
return $map->get($this->value);
} else {
return $map[$this->value];
}
}
}
37 changes: 0 additions & 37 deletions src/Localization/LocalizedString.php

This file was deleted.

0 comments on commit 61bf8db

Please sign in to comment.