diff --git a/src/DI/TranslationExtension.php b/src/DI/TranslationExtension.php index 0cee210..b9f918d 100644 --- a/src/DI/TranslationExtension.php +++ b/src/DI/TranslationExtension.php @@ -106,9 +106,19 @@ public function loadConfiguration(): void throw new Contributte\Translation\Exceptions\InvalidArgument('Default locale must be set.'); } + if ($this->config->debug && $this->config->debugger) { + $factory = Contributte\Translation\DebuggerTranslator::class; + + } elseif ($this->config->logger) { + $factory = Contributte\Translation\LoggerTranslator::class; + + } else { + $factory = Contributte\Translation\Translator::class; + } + $translator = $builder->addDefinition($this->prefix('translator')) ->setType(Nette\Localization\ITranslator::class) - ->setFactory(Contributte\Translation\Translator::class, ['defaultLocale' => $this->config->locales->default, 'cacheDir' => $this->config->cache->dir, 'debug' => $this->config->debug]) + ->setFactory($factory, ['defaultLocale' => $this->config->locales->default, 'cacheDir' => $this->config->cache->dir, 'debug' => $this->config->debug]) ->addSetup('setLocalesWhitelist', [$this->config->locales->whitelist]) ->addSetup('setConfigCacheFactory', [$configCacheFactory]) ->addSetup('setFallbackLocales', [$this->config->locales->fallback]); diff --git a/src/DebuggerTranslator.php b/src/DebuggerTranslator.php new file mode 100644 index 0000000..5da6d02 --- /dev/null +++ b/src/DebuggerTranslator.php @@ -0,0 +1,52 @@ +tracyPanel; + } + + public function setTracyPanel(?Tracy\Panel $tracyPanel): self + { + $this->tracyPanel = $tracyPanel; + return $this; + } + + /** + * {@inheritdoc} + */ + public function trans($id, array $parameters = [], $domain = null, $locale = null) + { + if ($this->tracyPanel !== null) { + if ($domain === null) { + $domain = 'messages'; + } + + if (!$this->getCatalogue()->has($id, $domain)) { + $this->tracyPanel->addMissingTranslation($id, $domain); + } + } + + return parent::trans($id, $parameters, $domain, $locale); + } + +} diff --git a/src/LoggerTranslator.php b/src/LoggerTranslator.php new file mode 100644 index 0000000..5ba3766 --- /dev/null +++ b/src/LoggerTranslator.php @@ -0,0 +1,56 @@ +psrLogger; + } + + public function setPsrLogger(?Psr\Log\LoggerInterface $psrLogger): self + { + $this->psrLogger = $psrLogger; + return $this; + } + + /** + * {@inheritdoc} + */ + public function trans($id, array $parameters = [], $domain = null, $locale = null) + { + if ($this->psrLogger !== null) { + if ($domain === null) { + $domain = 'messages'; + } + + if (!$this->getCatalogue()->has($id, $domain)) { + $this->psrLogger->notice('Missing translation', [ + 'id' => $id, + 'domain' => $domain, + 'locale' => $locale ?? $this->getLocale(), + ]); + } + } + + return parent::trans($id, $parameters, $domain, $locale); + } + +} diff --git a/src/Tracy/Panel.php b/src/Tracy/Panel.php index ced520b..2a17c2b 100644 --- a/src/Tracy/Panel.php +++ b/src/Tracy/Panel.php @@ -17,7 +17,7 @@ class Panel implements Tracy\IBarPanel use Nette\SmartObject; - /** @var Contributte\Translation\Translator */ + /** @var Contributte\Translation\DebuggerTranslator */ private $translator; /** @var (string|int)[][] */ @@ -41,7 +41,7 @@ class Panel implements Tracy\IBarPanel /** @var int */ private $ignoredResourcesCount = 0; - public function __construct(Contributte\Translation\Translator $translator) + public function __construct(Contributte\Translation\DebuggerTranslator $translator) { $this->translator = $translator; $translator->setTracyPanel($this); diff --git a/src/Translator.php b/src/Translator.php index b9a2d4a..88cc408 100644 --- a/src/Translator.php +++ b/src/Translator.php @@ -8,7 +8,6 @@ use Contributte; use Nette; -use Psr; use Symfony; /** @@ -17,8 +16,6 @@ * @property-read string $defaultLocale * @property-read string|null $cacheDir * @property-read bool $debug - * @property-read Contributte\Translation\Tracy\Panel|null $tracyPanel - * @property-read Psr\Log\LoggerInterface|null $psrLogger * @property string[]|null $localesWhitelist * @property string[] $prefix * @property-read string[][] $prefixTemp @@ -47,12 +44,6 @@ class Translator extends Symfony\Component\Translation\Translator implements Net /** @var bool */ private $debug; - /** @var Contributte\Translation\Tracy\Panel|null */ - private $tracyPanel; - - /** @var Psr\Log\LoggerInterface|null */ - private $psrLogger; - /** @var string[]|null */ private $localesWhitelist; @@ -103,28 +94,6 @@ public function getDebug(): bool return $this->debug; } - public function getTracyPanel(): ?Tracy\Panel - { - return $this->tracyPanel; - } - - public function setTracyPanel(?Tracy\Panel $tracyPanel): self - { - $this->tracyPanel = $tracyPanel; - return $this; - } - - public function getPsrLogger(): ?Psr\Log\LoggerInterface - { - return $this->psrLogger; - } - - public function setPsrLogger(?Psr\Log\LoggerInterface $psrLogger): self - { - $this->psrLogger = $psrLogger; - return $this; - } - /** * @return string[]|null */ @@ -320,34 +289,6 @@ public function translate($message, ...$parameters): string return $this->trans($message, $params, $domain, $locale); } - /** - * {@inheritdoc} - */ - public function trans($id, array $parameters = [], $domain = null, $locale = null) - { - if ($this->tracyPanel !== null || $this->psrLogger !== null) { - if ($domain === null) { - $domain = 'messages'; - } - - if (!$this->getCatalogue()->has($id, $domain)) { - if ($this->tracyPanel !== null) { - $this->tracyPanel->addMissingTranslation($id, $domain); - } - - if ($this->psrLogger !== null) { - $this->psrLogger->notice('Missing translation', [ - 'id' => $id, - 'domain' => $domain, - 'locale' => $locale ?? $this->getLocale(), - ]); - } - } - } - - return parent::trans($id, $parameters, $domain, $locale); - } - /** * {@inheritdoc} */ diff --git a/tests/Tests/DebuggerTranslator.phpt b/tests/Tests/DebuggerTranslator.phpt new file mode 100644 index 0000000..fcbe757 --- /dev/null +++ b/tests/Tests/DebuggerTranslator.phpt @@ -0,0 +1,32 @@ +tracyPanel); + + new Contributte\Translation\Tracy\Panel($debuggerTranslator); + + Tester\Assert::true($debuggerTranslator->tracyPanel instanceof Contributte\Translation\Tracy\Panel); + } + +} + + +(new DebuggerTranslator($container))->run(); diff --git a/tests/Tests/LoggerTranslator.phpt b/tests/Tests/LoggerTranslator.phpt new file mode 100644 index 0000000..754e796 --- /dev/null +++ b/tests/Tests/LoggerTranslator.phpt @@ -0,0 +1,32 @@ +psrLogger); + + $loggerTranslator->setPsrLogger(new PsrLoggerMock()); + + Tester\Assert::true($loggerTranslator->psrLogger instanceof PsrLoggerMock); + } + +} + + +(new LoggerTranslator($container))->run(); diff --git a/tests/Tests/Translator.phpt b/tests/Tests/Translator.phpt index 560e237..cce96be 100644 --- a/tests/Tests/Translator.phpt +++ b/tests/Tests/Translator.phpt @@ -28,22 +28,12 @@ class Translator extends Tests\TestAbstract Tester\Assert::same('en', $translator->defaultLocale); Tester\Assert::same(__DIR__ . '/cacheDir', $translator->cacheDir); Tester\Assert::true($translator->debug); - Tester\Assert::null($translator->tracyPanel); - Tester\Assert::null($translator->psrLogger); Tester\Assert::null($translator->localesWhitelist); Tester\Assert::same([], $translator->prefix); Tester\Assert::same('', $translator->formattedPrefix); Tester\Assert::same([], $translator->availableLocales); Tester\Assert::same('en', $translator->locale); - new Contributte\Translation\Tracy\Panel($translator); - - Tester\Assert::true($translator->tracyPanel instanceof Contributte\Translation\Tracy\Panel); - - $translator->setPsrLogger(new PsrLoggerMock()); - - Tester\Assert::true($translator->psrLogger instanceof PsrLoggerMock); - $translator->setLocalesWhitelist(['en', 'cs']); Tester\Assert::same(['en', 'cs'], $translator->localesWhitelist);