LoggingTranslator.php 3.5 KB

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