You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
3.7 KiB

3 years ago
  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. * {@inheritdoc}
  73. */
  74. public function getCatalogues(): array
  75. {
  76. return $this->translator->getCatalogues();
  77. }
  78. /**
  79. * Gets the fallback locales.
  80. *
  81. * @return array The fallback locales
  82. */
  83. public function getFallbackLocales()
  84. {
  85. if ($this->translator instanceof Translator || method_exists($this->translator, 'getFallbackLocales')) {
  86. return $this->translator->getFallbackLocales();
  87. }
  88. return [];
  89. }
  90. /**
  91. * Passes through all unknown calls onto the translator object.
  92. */
  93. public function __call(string $method, array $args)
  94. {
  95. return $this->translator->{$method}(...$args);
  96. }
  97. /**
  98. * Logs for missing translations.
  99. */
  100. private function log(string $id, ?string $domain, ?string $locale)
  101. {
  102. if (null === $domain) {
  103. $domain = 'messages';
  104. }
  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.', ['id' => $id, 'domain' => $domain, 'locale' => $catalogue->getLocale()]);
  111. } else {
  112. $this->logger->warning('Translation not found.', ['id' => $id, 'domain' => $domain, 'locale' => $catalogue->getLocale()]);
  113. }
  114. }
  115. }