8000 [2.2] [FrameworkBundle][Translation] Added logging capability to translator by meandmymonkey · Pull Request #3890 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[2.2] [FrameworkBundle][Translation] Added logging capability to translator #3890

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
[FrameworkBundle] added logging capabilities to translator
  • Loading branch information
Andreas Hucks committed Apr 11, 2012
commit cbd5f990afdccf6698f1277467fc3990bf3dd10c
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ private function addTranslatorSection(ArrayNodeDefinition $rootNode)
->treatTrueLike(array('enabled' => true))
->children()
->booleanNode('enabled')->defaultTrue()->end()
->booleanNode('enable_logging')->defaultFalse()->end()
Copy link
Member

Choose a reason for hiding this comment

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

it should simply be named logging to be consistent with DoctrineBundle

Copy link
Author

Choose a reason for hiding this comment

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

fast as always :)

done.

->scalarNode('fallback')->defaultValue('en')->end()
->end()
->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,10 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
$translator->addMethodCall('addResource', array($format, (string) $file, $locale, $domain));
}
}

if ($config['enable_logging']) {
$translator->addMethodCall('setLogger', array(new Reference('logger')));
}
}
}

Expand Down
25 changes: 25 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@

use Symfony\Component\Translation\Translator as BaseTranslator;
use Symfony\Component\Translation\MessageSelector;
use Symfony\Component\Translation\MessageCatalogueInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Config\ConfigCache;
use Symfony\Component\HttpKernel\Log\LoggerInterface;

/**
* Translator.
Expand All @@ -26,6 +28,7 @@ class Translator extends BaseTranslator
protected $container;
protected $options;
protected $loaderIds;
protected $logger;

/**
* Constructor.
Expand All @@ -44,6 +47,7 @@ public function __construct(ContainerInterface $container, MessageSelector $sele
{
$this->container = $container;
$this->loaderIds = $loaderIds;
$this->logger = null;

$this->options = array(
'cache_dir' => null,
Expand Down Expand Up @@ -72,6 +76,27 @@ public function getLocale()
return $this->locale;
}

public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}

/**
* {@inheritdoc}
*/
protected function validateCatalogueForId(MessageCatalogueInterface $catalogue, $id, $domain)
{
if (null !== $this->logger && !$catalogue->defines($id, $domain)) {
if ($catalogue->has($id, $domain)) {
$this->logger->notice('Translator: using fallback catalogue.', array('id' => $id, 'domain' => $domain));
Copy link

Choose a reason for hiding this comment

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

@meandmymonkey Thanks for making this PR. I know that a lot of time passed but could you also pass locale ($this->getLocale()) to the logger?

} else {
$this->logger->warn('Translator: translation not found.', array('id' => $id, 'domain' => $domain));
Copy link

Choose a reason for hiding this comment

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

Same here.

}
}

return parent::validateCatalogueForId($catalogue, $id, $domain);
}

/**
* {@inheritdoc}
*/
Expand Down
0