LoggingTranslator.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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\Contracts\Translation\LocaleAwareInterface;
  13. use Symfony\Contracts\Translation\TranslatorInterface;
  14. /**
  15. * @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
  16. */
  17. class LoggingTranslator implements TranslatorInterface, TranslatorBagInterface, LocaleAwareInterface
  18. {
  19. public function __construct(
  20. private TranslatorInterface&TranslatorBagInterface&LocaleAwareInterface $translator,
  21. private LoggerInterface $logger,
  22. ) {
  23. }
  24. public function trans(?string $id, array $parameters = [], ?string $domain = null, ?string $locale = null): string
  25. {
  26. $trans = $this->translator->trans($id = (string) $id, $parameters, $domain, $locale);
  27. $this->log($id, $domain, $locale);
  28. return $trans;
  29. }
  30. public function setLocale(string $locale): void
  31. {
  32. $prev = $this->translator->getLocale();
  33. $this->translator->setLocale($locale);
  34. if ($prev === $locale) {
  35. return;
  36. }
  37. $this->logger->debug(\sprintf('The locale of the translator has changed from "%s" to "%s".', $prev, $locale));
  38. }
  39. public function getLocale(): string
  40. {
  41. return $this->translator->getLocale();
  42. }
  43. public function getCatalogue(?string $locale = null): MessageCatalogueInterface
  44. {
  45. return $this->translator->getCatalogue($locale);
  46. }
  47. public function getCatalogues(): array
  48. {
  49. return $this->translator->getCatalogues();
  50. }
  51. /**
  52. * Gets the fallback locales.
  53. */
  54. public function getFallbackLocales(): array
  55. {
  56. if ($this->translator instanceof Translator || method_exists($this->translator, 'getFallbackLocales')) {
  57. return $this->translator->getFallbackLocales();
  58. }
  59. return [];
  60. }
  61. public function __call(string $method, array $args): mixed
  62. {
  63. return $this->translator->{$method}(...$args);
  64. }
  65. /**
  66. * Logs for missing translations.
  67. */
  68. private function log(string $id, ?string $domain, ?string $locale): void
  69. {
  70. $domain ??= 'messages';
  71. $catalogue = $this->translator->getCatalogue($locale);
  72. if ($catalogue->defines($id, $domain)) {
  73. return;
  74. }
  75. if ($catalogue->has($id, $domain)) {
  76. $this->logger->debug('Translation use fallback catalogue.', ['id' => $id, 'domain' => $domain, 'locale' => $catalogue->getLocale()]);
  77. } else {
  78. $this->logger->warning('Translation not found.', ['id' => $id, 'domain' => $domain, 'locale' => $catalogue->getLocale()]);
  79. }
  80. }
  81. }