-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Translator split: Translator -> LoggerTranslator -> DebuggerTranslator
- Loading branch information
Showing
8 changed files
with
185 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters