LoggingTranslator.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
  14. use Symfony\Contracts\Translation\LocaleAwareInterface;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. /**
  17. * @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
  18. */
  19. class LoggingTranslator implements TranslatorInterface, LegacyTranslatorInterface, TranslatorBagInterface
  20. {
  21. /**
  22. * @var TranslatorInterface|TranslatorBagInterface
  23. */
  24. private $translator;
  25. private $logger;
  26. /**
  27. * @param TranslatorInterface $translator The translator must implement TranslatorBagInterface
  28. * @param LoggerInterface $logger
  29. */
  30. public function __construct($translator, LoggerInterface $logger)
  31. {
  32. if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
  33. throw new \TypeError(sprintf('Argument 1 passed to %s() must be an instance of %s, %s given.', __METHOD__, TranslatorInterface::class, \is_object($translator) ? \get_class($translator) : \gettype($translator)));
  34. }
  35. if (!$translator instanceof TranslatorBagInterface || !$translator instanceof LocaleAwareInterface) {
  36. throw new InvalidArgumentException(sprintf('The Translator "%s" must implement TranslatorInterface, TranslatorBagInterface and LocaleAwareInterface.', \get_class($translator)));
  37. }
  38. $this->translator = $translator;
  39. $this->logger = $logger;
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function trans($id, array $parameters = [], $domain = null, $locale = null)
  45. {
  46. $trans = $this->translator->trans($id, $parameters, $domain, $locale);
  47. $this->log($id, $domain, $locale);
  48. return $trans;
  49. }
  50. /**
  51. * {@inheritdoc}
  52. *
  53. * @deprecated since Symfony 4.2, use the trans() method instead with a %count% parameter
  54. */
  55. public function transChoice($id, $number, array $parameters = [], $domain = null, $locale = null)
  56. {
  57. @trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2, use the trans() one instead with a "%%count%%" parameter.', __METHOD__), E_USER_DEPRECATED);
  58. if ($this->translator instanceof TranslatorInterface) {
  59. $trans = $this->translator->trans($id, ['%count%' => $number] + $parameters, $domain, $locale);
  60. } else {
  61. $trans = $this->translator->transChoice($id, $number, $parameters, $domain, $locale);
  62. }
  63. $this->log($id, $domain, $locale);
  64. return $trans;
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function setLocale($locale)
  70. {
  71. $prev = $this->translator->getLocale();
  72. $this->translator->setLocale($locale);
  73. if ($prev === $locale) {
  74. return;
  75. }
  76. $this->logger->debug(sprintf('The locale of the translator has changed from "%s" to "%s".', $prev, $locale));
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function getLocale()
  82. {
  83. return $this->translator->getLocale();
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function getCatalogue($locale = null)
  89. {
  90. return $this->translator->getCatalogue($locale);
  91. }
  92. /**
  93. * Gets the fallback locales.
  94. *
  95. * @return array The fallback locales
  96. */
  97. public function getFallbackLocales()
  98. {
  99. if ($this->translator instanceof Translator || method_exists($this->translator, 'getFallbackLocales')) {
  100. return $this->translator->getFallbackLocales();
  101. }
  102. return [];
  103. }
  104. /**
  105. * Passes through all unknown calls onto the translator object.
  106. */
  107. public function __call($method, $args)
  108. {
  109. return $this->translator->{$method}(...$args);
  110. }
  111. /**
  112. * Logs for missing translations.
  113. *
  114. * @param string $id
  115. * @param string|null $domain
  116. * @param string|null $locale
  117. */
  118. private function log($id, $domain, $locale)
  119. {
  120. if (null === $domain) {
  121. $domain = 'messages';
  122. }
  123. $id = (string) $id;
  124. $catalogue = $this->translator->getCatalogue($locale);
  125. if ($catalogue->defines($id, $domain)) {
  126. return;
  127. }
  128. if ($catalogue->has($id, $domain)) {
  129. $this->logger->debug('Translation use fallback catalogue.', ['id' => $id, 'domain' => $domain, 'locale' => $catalogue->getLocale()]);
  130. } else {
  131. $this->logger->warning('Translation not found.', ['id' => $id, 'domain' => $domain, 'locale' => $catalogue->getLocale()]);
  132. }
  133. }
  134. }