LoggingTranslator.php 3.6 KB

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