LoggingTranslator.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Translation;
  11. use Psr\Log\LoggerInterface;
  12. /**
  13. * @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
  14. */
  15. class LoggingTranslator implements TranslatorInterface, TranslatorBagInterface
  16. {
  17. /**
  18. * @var TranslatorInterface|TranslatorBagInterface
  19. */
  20. private $translator;
  21. /**
  22. * @var LoggerInterface
  23. */
  24. private $logger;
  25. /**
  26. * @param TranslatorInterface $translator The translator must implement TranslatorBagInterface
  27. * @param LoggerInterface $logger
  28. */
  29. public function __construct(TranslatorInterface $translator, LoggerInterface $logger)
  30. {
  31. if (!$translator instanceof TranslatorBagInterface) {
  32. throw new \InvalidArgumentException(sprintf('The Translator "%s" must implement TranslatorInterface and TranslatorBagInterface.', get_class($translator)));
  33. }
  34. $this->translator = $translator;
  35. $this->logger = $logger;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function trans($id, array $parameters = array(), $domain = null, $locale = null)
  41. {
  42. $trans = $this->translator->trans($id, $parameters, $domain, $locale);
  43. $this->log($id, $domain, $locale);
  44. return $trans;
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
  50. {
  51. $trans = $this->translator->transChoice($id, $number, $parameters, $domain, $locale);
  52. $this->log($id, $domain, $locale);
  53. return $trans;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function setLocale($locale)
  59. {
  60. $this->translator->setLocale($locale);
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function getLocale()
  66. {
  67. return $this->translator->getLocale();
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function getCatalogue($locale = null)
  73. {
  74. return $this->translator->getCatalogue($locale);
  75. }
  76. /**
  77. * Passes through all unknown calls onto the translator object.
  78. */
  79. public function __call($method, $args)
  80. {
  81. return call_user_func_array(array($this->translator, $method), $args);
  82. }
  83. /**
  84. * Logs for missing translations.
  85. *
  86. * @param string $id
  87. * @param string|null $domain
  88. * @param string|null $locale
  89. */
  90. private function log($id, $domain, $locale)
  91. {
  92. if (null === $domain) {
  93. $domain = 'messages';
  94. }
  95. $id = (string) $id;
  96. $catalogue = $this->translator->getCatalogue($locale);
  97. if ($catalogue->defines($id, $domain)) {
  98. return;
  99. }
  100. if ($catalogue->has($id, $domain)) {
  101. $this->logger->debug('Translation use fallback catalogue.', array('id' => $id, 'domain' => $domain, 'locale' => $catalogue->getLocale()));
  102. } else {
  103. $this->logger->warning('Translation not found.', array('id' => $id, 'domain' => $domain, 'locale' => $catalogue->getLocale()));
  104. }
  105. }
  106. }