4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Translation;
14 use Psr\Log\LoggerInterface;
15 use Symfony\Component\Translation\Exception\InvalidArgumentException;
18 * @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
20 class LoggingTranslator implements TranslatorInterface, TranslatorBagInterface
23 * @var TranslatorInterface|TranslatorBagInterface
30 * @param TranslatorInterface $translator The translator must implement TranslatorBagInterface
31 * @param LoggerInterface $logger
33 public function __construct(TranslatorInterface $translator, LoggerInterface $logger)
35 if (!$translator instanceof TranslatorBagInterface) {
36 throw new InvalidArgumentException(sprintf('The Translator "%s" must implement TranslatorInterface and TranslatorBagInterface.', get_class($translator)));
39 $this->translator = $translator;
40 $this->logger = $logger;
46 public function trans($id, array $parameters = array(), $domain = null, $locale = null)
48 $trans = $this->translator->trans($id, $parameters, $domain, $locale);
49 $this->log($id, $domain, $locale);
57 public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
59 $trans = $this->translator->transChoice($id, $number, $parameters, $domain, $locale);
60 $this->log($id, $domain, $locale);
68 public function setLocale($locale)
70 $this->translator->setLocale($locale);
76 public function getLocale()
78 return $this->translator->getLocale();
84 public function getCatalogue($locale = null)
86 return $this->translator->getCatalogue($locale);
90 * Gets the fallback locales.
92 * @return array $locales The fallback locales
94 public function getFallbackLocales()
96 if ($this->translator instanceof Translator || method_exists($this->translator, 'getFallbackLocales')) {
97 return $this->translator->getFallbackLocales();
104 * Passes through all unknown calls onto the translator object.
106 public function __call($method, $args)
108 return call_user_func_array(array($this->translator, $method), $args);
112 * Logs for missing translations.
115 * @param string|null $domain
116 * @param string|null $locale
118 private function log($id, $domain, $locale)
120 if (null === $domain) {
121 $domain = 'messages';
125 $catalogue = $this->translator->getCatalogue($locale);
126 if ($catalogue->defines($id, $domain)) {
130 if ($catalogue->has($id, $domain)) {
131 $this->logger->debug('Translation use fallback catalogue.', array('id' => $id, 'domain' => $domain, 'locale' => $catalogue->getLocale()));
133 $this->logger->warning('Translation not found.', array('id' => $id, 'domain' => $domain, 'locale' => $catalogue->getLocale()));