LoggingTranslator.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. use Symfony\Component\Translation\Exception\InvalidArgumentException;
  13. /**
  14. * @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
  15. */
  16. class LoggingTranslator implements TranslatorInterface, TranslatorBagInterface
  17. {
  18. /**
  19. * @var TranslatorInterface|TranslatorBagInterface
  20. */
  21. private $translator;
  22. /**
  23. * @var LoggerInterface
  24. */
  25. private $logger;
  26. /**
  27. * @param TranslatorInterface $translator The translator must implement TranslatorBagInterface
  28. * @param LoggerInterface $logger
  29. */
  30. public function __construct(TranslatorInterface $translator, LoggerInterface $logger)
  31. {
  32. if (!$translator instanceof TranslatorBagInterface) {
  33. throw new InvalidArgumentException(sprintf('The Translator "%s" must implement TranslatorInterface and TranslatorBagInterface.', get_class($translator)));
  34. }
  35. $this->translator = $translator;
  36. $this->logger = $logger;
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function trans($id, array $parameters = array(), $domain = null, $locale = null)
  42. {
  43. $trans = $this->translator->trans($id, $parameters, $domain, $locale);
  44. $this->log($id, $domain, $locale);
  45. return $trans;
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
  51. {
  52. $trans = $this->translator->transChoice($id, $number, $parameters, $domain, $locale);
  53. $this->log($id, $domain, $locale);
  54. return $trans;
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function setLocale($locale)
  60. {
  61. $this->translator->setLocale($locale);
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function getLocale()
  67. {
  68. return $this->translator->getLocale();
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function getCatalogue($locale = null)
  74. {
  75. return $this->translator->getCatalogue($locale);
  76. }
  77. /**
  78. * Gets the fallback locales.
  79. *
  80. * @return array $locales The fallback locales
  81. */
  82. public function getFallbackLocales()
  83. {
  84. if ($this->translator instanceof Translator) {
  85. return $this->translator->getFallbackLocales();
  86. }
  87. return array();
  88. }
  89. /**
  90. * Passes through all unknown calls onto the translator object.
  91. */
  92. public function __call($method, $args)
  93. {
  94. return call_user_func_array(array($this->translator, $method), $args);
  95. }
  96. /**
  97. * Logs for missing translations.
  98. *
  99. * @param string $id
  100. * @param string|null $domain
  101. * @param string|null $locale
  102. */
  103. private function log($id, $domain, $locale)
  104. {
  105. if (null === $domain) {
  106. $domain = 'messages';
  107. }
  108. $id = (string) $id;
  109. $catalogue = $this->translator->getCatalogue($locale);
  110. if ($catalogue->defines($id, $domain)) {
  111. return;
  112. }
  113. if ($catalogue->has($id, $domain)) {
  114. $this->logger->debug('Translation use fallback catalogue.', array('id' => $id, 'domain' => $domain, 'locale' => $catalogue->getLocale()));
  115. } else {
  116. $this->logger->warning('Translation not found.', array('id' => $id, 'domain' => $domain, 'locale' => $catalogue->getLocale()));
  117. }
  118. }
  119. }