No Description

LoggingTranslator.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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
  16. {
  17. /**
  18. * @var TranslatorInterface
  19. */
  20. private $translator;
  21. /**
  22. * @var LoggerInterface
  23. */
  24. private $logger;
  25. /**
  26. * @param Translator $translator
  27. * @param LoggerInterface $logger
  28. */
  29. public function __construct($translator, LoggerInterface $logger)
  30. {
  31. if (!($translator instanceof TranslatorInterface && $translator instanceof TranslatorBagInterface)) {
  32. throw new \InvalidArgumentException(sprintf('The Translator "%s" must implements 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. * @api
  59. */
  60. public function setLocale($locale)
  61. {
  62. $this->translator->setLocale($locale);
  63. }
  64. /**
  65. * {@inheritdoc}
  66. *
  67. * @api
  68. */
  69. public function getLocale()
  70. {
  71. return $this->translator->getLocale();
  72. }
  73. /**
  74. * Passes through all unknown calls onto the translator object.
  75. */
  76. public function __call($method, $args)
  77. {
  78. return call_user_func_array(array($this->translator, $method), $args);
  79. }
  80. /**
  81. * Logs for missing translations.
  82. *
  83. * @param string $id
  84. * @param string|null $domain
  85. * @param string|null $locale
  86. */
  87. private function log($id, $domain, $locale)
  88. {
  89. if (null === $locale) {
  90. $locale = $this->getLocale();
  91. }
  92. if (null === $domain) {
  93. $domain = 'messages';
  94. }
  95. $id = (string) $id;
  96. $catalogue = $this->translator->getCatalogue($locale);
  97. if ($catalogue->defines($id, $domain)) {
  98. return;
  99. }
  100. if ($catalogue->has($id, $domain)) {
  101. $this->logger->debug('Translation use fallback catalogue.', array('id' => $id, 'domain' => $domain, 'locale' => $catalogue->getLocale()));
  102. } else {
  103. $this->logger->warning('Translation not found.', array('id' => $id, 'domain' => $domain, 'locale' => $catalogue->getLocale()));
  104. }
  105. }
  106. }