Skip to content

Commit

Permalink
Translator split: Translator -> LoggerTranslator -> DebuggerTranslator
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleš authored Jun 5, 2019
2 parents 2b03f9e + 5de4072 commit f24d6c8
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 72 deletions.
12 changes: 11 additions & 1 deletion src/DI/TranslationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
52 changes: 52 additions & 0 deletions src/DebuggerTranslator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php declare(strict_types = 1);

/**
* This file is part of the Contributte/Translation
*/

namespace Contributte\Translation;

use Contributte;
use Nette;

/**
* @property Contributte\Translation\Tracy\Panel|null $tracyPanel
*/
class DebuggerTranslator extends LoggerTranslator
{

use Nette\SmartObject;

/** @var Contributte\Translation\Tracy\Panel|null */
private $tracyPanel;

public function getTracyPanel(): ?Tracy\Panel
{
return $this->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);
}

}
56 changes: 56 additions & 0 deletions src/LoggerTranslator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php declare(strict_types = 1);

/**
* This file is part of the Contributte/Translation
*/

namespace Contributte\Translation;

use Nette;
use Psr;

/**
* @property Psr\Log\LoggerInterface|null $psrLogger
*/
class LoggerTranslator extends Translator
{

use Nette\SmartObject;

/** @var Psr\Log\LoggerInterface|null */
private $psrLogger;

public function getPsrLogger(): ?Psr\Log\LoggerInterface
{
return $this->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);
}

}
4 changes: 2 additions & 2 deletions src/Tracy/Panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)[][] */
Expand All @@ -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);
Expand Down
59 changes: 0 additions & 59 deletions src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

use Contributte;
use Nette;
use Psr;
use Symfony;

/**
Expand All @@ -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
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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}
*/
Expand Down
32 changes: 32 additions & 0 deletions tests/Tests/DebuggerTranslator.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php declare(strict_types = 1);

/**
* This file is part of the Contributte/Translation
*/

namespace Tests;

use Contributte;
use Tester;
use Tests;

$container = require __DIR__ . '/../bootstrap.php';

class DebuggerTranslator extends Tests\TestAbstract
{

public function test01(): void
{
$debuggerTranslator = new Contributte\Translation\DebuggerTranslator(new Contributte\Translation\LocaleResolver(), new Contributte\Translation\FallbackResolver(), 'en', __DIR__ . '/cacheDir', true);

Tester\Assert::null($debuggerTranslator->tracyPanel);

new Contributte\Translation\Tracy\Panel($debuggerTranslator);

Tester\Assert::true($debuggerTranslator->tracyPanel instanceof Contributte\Translation\Tracy\Panel);
}

}


(new DebuggerTranslator($container))->run();
32 changes: 32 additions & 0 deletions tests/Tests/LoggerTranslator.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php declare(strict_types = 1);

/**
* This file is part of the Contributte/Translation
*/

namespace Tests;

use Contributte;
use Tester;
use Tests;

$container = require __DIR__ . '/../bootstrap.php';

class LoggerTranslator extends Tests\TestAbstract
{

public function test01(): void
{
$loggerTranslator = new Contributte\Translation\LoggerTranslator(new Contributte\Translation\LocaleResolver(), new Contributte\Translation\FallbackResolver(), 'en', __DIR__ . '/cacheDir', true);

Tester\Assert::null($loggerTranslator->psrLogger);

$loggerTranslator->setPsrLogger(new PsrLoggerMock());

Tester\Assert::true($loggerTranslator->psrLogger instanceof PsrLoggerMock);
}

}


(new LoggerTranslator($container))->run();
10 changes: 0 additions & 10 deletions tests/Tests/Translator.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit f24d6c8

Please sign in to comment.