-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Translation] added LoggingTranslator. #10887
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a532d24
c319cef
f543258
8fdd235
0f2294d
ecea954
38fb884
071880b
8af5171
c7ee300
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
|
||
/** | ||
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com> | ||
*/ | ||
class LoggingTranslatorPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if (!$container->hasAlias('logger')) { | ||
return; | ||
} | ||
|
||
if ($container->getParameter('translator.logging')) { | ||
$translatorAlias = $container->getAlias('translator'); | ||
$definition = $container->getDefinition((string) $translatorAlias); | ||
$class = $container->getParameterBag()->resolveValue($definition->getClass()); | ||
|
||
$refClass = new \ReflectionClass($class); | ||
if ($refClass->implementsInterface('Symfony\Component\Translation\TranslatorInterface') && $refClass->implementsInterface('Symfony\Component\Translation\TranslatorBagInterface')) { | ||
$container->getDefinition('translator.logging')->setDecoratedService('translator'); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,16 @@ | |
*/ | ||
class Configuration implements ConfigurationInterface | ||
{ | ||
private $debug; | ||
|
||
/** | ||
* @param bool $debug Whether debugging is enabled or not | ||
*/ | ||
public function __construct($debug) | ||
{ | ||
$this->debug = (bool) $debug; | ||
} | ||
|
||
/** | ||
* Generates the configuration tree builder. | ||
* | ||
|
@@ -441,6 +451,7 @@ private function addTranslatorSection(ArrayNodeDefinition $rootNode) | |
->canBeEnabled() | ||
->children() | ||
->scalarNode('fallback')->defaultValue('en')->end() | ||
->booleanNode('logging')->defaultValue($this->debug)->end() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not such a fan of dynamic defaults as it means you get different output from |
||
->end() | ||
->end() | ||
->end() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
|
||
<parameters> | ||
<parameter key="translator.class">Symfony\Bundle\FrameworkBundle\Translation\Translator</parameter> | ||
<parameter key="translator.logging.class">Symfony\Component\Translation\LoggingTranslator</parameter> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As of 2.6 this won't be done anymore. Can you move the class name to the service definition? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @iltar Agreed for an application-specific but not for a reusable bundles ;) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @aitboudad, this is not good for re-usability as you cannot change the constructor. You can use service decoration for this. I suggest you read upon the issue Fabien made: #11881 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @iltar 👍 |
||
<parameter key="translator.identity.class">Symfony\Component\Translation\IdentityTranslator</parameter> | ||
<parameter key="translator.selector.class">Symfony\Component\Translation\MessageSelector</parameter> | ||
<parameter key="translation.loader.php.class">Symfony\Component\Translation\Loader\PhpFileLoader</parameter> | ||
|
@@ -46,6 +47,12 @@ | |
</argument> | ||
</service> | ||
|
||
<service id="translator.logging" class="%translator.logging.class%" public="false"> | ||
<argument type="service" id="translator.logging.inner" /> | ||
<argument type="service" id="logger" /> | ||
<tag name="monolog.logger" channel="translation" /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A translator is not a loger, is it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
</service> | ||
|
||
<service id="translator" class="%translator.identity.class%"> | ||
<argument type="service" id="translator.selector" /> | ||
</service> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler; | ||
|
||
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\LoggingTranslatorPass; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
|
||
class LoggingTranslatorPassTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testProcess() | ||
{ | ||
$definition = $this->getMock('Symfony\Component\DependencyInjection\Definition'); | ||
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder'); | ||
$parameterBag = $this->getMock('Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface'); | ||
|
||
$container->expects($this->once()) | ||
->method('hasAlias') | ||
->will($this->returnValue(true)); | ||
|
||
$container->expects($this->once()) | ||
->method('getParameter') | ||
->will($this->returnValue(true)); | ||
|
||
$container->expects($this->once()) | ||
->method('getAlias') | ||
->will($this->returnValue('translation.default')); | ||
|
||
$container->expects($this->exactly(2)) | ||
->method('getDefinition') | ||
->will($this->returnValue($definition)); | ||
|
||
$definition->expects($this->once()) | ||
->method('getClass') | ||
->will($this->returnValue("%translator.class%")); | ||
|
||
$parameterBag->expects($this->once()) | ||
->method('resolveValue') | ||
->will($this->returnValue("Symfony\Bundle\FrameworkBundle\Translation\Translator")); | ||
|
||
$container->expects($this->once()) | ||
->method('getParameterBag') | ||
->will($this->returnValue($parameterBag)); | ||
|
||
$pass = new LoggingTranslatorPass(); | ||
$pass->process($container); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The container is just a data container, why don't you just test if the data you expect to be added in the container is there rather than writing tests to see if the container gets called the way you think it gets called? It will make the test easier to update and less fragile, as the inner workings may change over time while the input and output stay the same. |
||
} | ||
|
||
public function testThatCompilerPassIsIgnoredIfThereIsNotLoggerDefinition() | ||
{ | ||
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder'); | ||
$container->expects($this->once()) | ||
->method('hasAlias') | ||
->will($this->returnValue(false)); | ||
|
||
$pass = new LoggingTranslatorPass(); | ||
$pass->process($container); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Translation; | ||
|
||
use Psr\Log\LoggerInterface; | ||
|
||
/** | ||
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com> | ||
*/ | ||
class LoggingTranslator implements TranslatorInterface | ||
{ | ||
/** | ||
* @var TranslatorInterface | ||
*/ | ||
private $translator; | ||
|
||
/** | ||
* @var LoggerInterface | ||
*/ | ||
private $logger; | ||
|
||
/** | ||
* @param Translator $translator | ||
* @param LoggerInterface $logger | ||
*/ | ||
public function __construct($translator, LoggerInterface $logger) | ||
{ | ||
if (!($translator instanceof TranslatorInterface && $translator instanceof TranslatorBagInterface)) { | ||
throw new \InvalidArgumentException(sprintf('The Translator "%s" must implements TranslatorInterface and TranslatorBagInterface.', get_class($translator))); | ||
} | ||
|
||
$this->translator = $translator; | ||
$this->logger = $logger; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function trans($id, array $parameters = array(), $domain = null, $locale = null) | ||
{ | ||
$trans = $this->translator->trans($id, $parameters , $domain , $locale); | ||
$this->log($id, $domain, $locale); | ||
|
||
return $trans; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null) | ||
{ | ||
$trans = $this->translator->transChoice($id, $number, $parameters, $domain, $locale); | ||
$this->log($id, $domain, $locale); | ||
|
||
return $trans; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @api | ||
*/ | ||
public function setLocale($locale) | ||
{ | ||
$this->translator->setLocale($locale); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @api | ||
*/ | ||
public function getLocale() | ||
{ | ||
return $this->translator->getLocale(); | ||
} | ||
|
||
/** | ||
* Passes through all unknown calls onto the translator object. | ||
*/ | ||
public function __call($method, $args) | ||
{ | ||
return call_user_func_array(array($this->translator, $method), $args); | ||
} | ||
|
||
/** | ||
* Logs for missing translations. | ||
* | ||
* @param string $id | ||
* @param string|null $domain | ||
* @param string|null $locale | ||
*/ | ||
private function log($id, $domain, $locale) | ||
{ | ||
if (null === $locale) { | ||
$locale = $this->getLocale(); | ||
} | ||
|
||
if (null === $domain) { | ||
$domain = 'messages'; | ||
} | ||
|
||
$id = (string) $id; | ||
$catalogue = $this->translator->getCatalogue($locale); | ||
if ($catalogue->defines($id, $domain)) { | ||
return; | ||
} | ||
|
||
if ($catalogue->has($id, $domain)) { | ||
$this->logger->debug('Translation use fallback catalogue.', array('id' => $id, 'domain' => $domain, 'locale' => $catalogue->getLocale())); | ||
} else { | ||
$this->logger->warning('Translation not found.', array('id' => $id, 'domain' => $domain, 'locale' => $catalogue->getLocale())); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This broke my symfony-master
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php#L638
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I opened a PR: #12023