Drupal investigation

LoggingTranslator.php 3.5KB

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