Skip to content
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

[Translation] Added logging capability to translator #10014

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,15 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
}
$translator->addMethodCall('setFallbackLocales', array($config['fallback']));

// Inject logger into translation
if ($container->getParameter('kernel.debug')) {
$logger = new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE);

$translator
->addTag('monolog.logger', array('channel' => 'translation'))
->addMethodCall('setLogger', array($logger));
}

// Discover translation directories
$dirs = array();
if (class_exists('Symfony\Component\Validator\Validator')) {
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Translation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
CHANGELOG
=========

2.5.0
-----
* added logging capabilities to translator.

2.3.0
-----

Expand Down
37 changes: 37 additions & 0 deletions src/Symfony/Component/Translation/Tests/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,43 @@ public function testTransChoiceFallbackWithNoTranslation()
// unchanged if it can't be found
$this->assertEquals('some_message2', $translator->transChoice('some_message2', 10, array('%count%' => 10)));
}

public function testTransWithNoTranslationIsLogged()
{
if (!interface_exists('Psr\Log\LoggerInterface')) {
$this->markTestSkipped('The "LoggerInterface" is not available');
}

$logger = $this->getMock('Psr\Log\LoggerInterface');
$logger->expects($this->once())
->method('warning')
->with('Translation not found.')
;

$translator = new Translator('ar');
$translator->setLogger($logger);
$translator->trans('bar');
}

public function testTransChoiceFallbackIsLogged()
{
if (!interface_exists('Psr\Log\LoggerInterface')) {
$this->markTestSkipped('The "LoggerInterface" is not available');
}

$logger = $this->getMock('Psr\Log\LoggerInterface');
$logger->expects($this->once())
->method('debug')
->with('Translation use fallback catalogue.')
;

$translator = new Translator('ar');
$translator->setLogger($logger);
$translator->setFallbackLocales(array('en'));
$translator->addLoader('array', new ArrayLoader());
$translator->addResource('array', array('some_message2' => 'one thing|%count% things'), 'en');
$translator->transChoice('some_message2', 10, array('%count%' => 10));
}
}

class String
Expand Down
41 changes: 41 additions & 0 deletions src/Symfony/Component/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Translation\Loader\LoaderInterface;
use Symfony\Component\Translation\Exception\NotFoundResourceException;
use Psr\Log\LoggerInterface;

/**
* Translator.
Expand Down Expand Up @@ -53,6 +54,11 @@ class Translator implements TranslatorInterface
*/
private $selector;

/**
* @var LoggerInterface|null
*/
private $logger;

/**
* Constructor.
*
Expand Down Expand Up @@ -185,6 +191,8 @@ public function trans($id, array $parameters = array(), $domain = null, $locale
$this->loadCatalogue($locale);
}

$this->log($this->catalogues[$locale], $id, $domain);

return strtr($this->catalogues[$locale]->get((string) $id, $domain), $parameters);
}

Expand All @@ -210,6 +218,9 @@ public function transChoice($id, $number, array $parameters = array(), $domain =
$id = (string) $id;

$catalogue = $this->catalogues[$locale];

$this->log($catalogue, $id, $domain);

while (!$catalogue->defines($id, $domain)) {
if ($cat = $catalogue->getFallbackCatalogue()) {
$catalogue = $cat;
Expand All @@ -222,6 +233,16 @@ public function transChoice($id, $number, array $parameters = array(), $domain =
return strtr($this->selector->choose($catalogue->get($id, $domain), (int) $number, $locale), $parameters);
}

/**
* Sets the logger.
*
* @param LoggerInterface $logger A logger instance
*/
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}

/**
* Gets the loaders.
*
Expand All @@ -244,6 +265,26 @@ protected function loadCatalogue($locale)
$this->loadFallbackCatalogues($locale);
}

/**
* Logs for missing catalogue.
*
* @param MessageCatalogueInterface $catalogue
* @param string $id
* @param string $domain
*/
private function log(MessageCatalogueInterface $catalogue, $id, $domain)
{
if (null === $this->logger || $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()));
}
}

private function doLoadCatalogue($locale)
{
$this->catalogues[$locale] = new MessageCatalogue($locale);
Expand Down
6 changes: 4 additions & 2 deletions src/Symfony/Component/Translation/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
},
"require-dev": {
"symfony/config": "~2.0",
"symfony/yaml": "~2.2"
"symfony/yaml": "~2.2",
"psr/log": "~1.0"
},
"suggest": {
"symfony/config": "",
"symfony/yaml": ""
"symfony/yaml": "",
"psr/log": "For using logging capability in translator"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aitboudad
psr/log contains interfaces only. You need to require some real implementation (most likely monolog/monolog) to have working logger

},
"autoload": {
"psr-0": { "Symfony\\Component\\Translation\\": "" }
Expand Down