DataCollectorTranslator.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 Symfony\Component\Translation\Exception\InvalidArgumentException;
  12. use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
  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 DataCollectorTranslator implements LegacyTranslatorInterface, TranslatorInterface, TranslatorBagInterface
  19. {
  20. const MESSAGE_DEFINED = 0;
  21. const MESSAGE_MISSING = 1;
  22. const MESSAGE_EQUALS_FALLBACK = 2;
  23. /**
  24. * @var TranslatorInterface|TranslatorBagInterface
  25. */
  26. private $translator;
  27. private $messages = [];
  28. /**
  29. * @param TranslatorInterface $translator The translator must implement TranslatorBagInterface
  30. */
  31. public function __construct($translator)
  32. {
  33. if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
  34. throw new \TypeError(sprintf('Argument 1 passed to %s() must be an instance of %s, %s given.', __METHOD__, TranslatorInterface::class, \is_object($translator) ? \get_class($translator) : \gettype($translator)));
  35. }
  36. if (!$translator instanceof TranslatorBagInterface || !$translator instanceof LocaleAwareInterface) {
  37. throw new InvalidArgumentException(sprintf('The Translator "%s" must implement TranslatorInterface, TranslatorBagInterface and LocaleAwareInterface.', \get_class($translator)));
  38. }
  39. $this->translator = $translator;
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function trans($id, array $parameters = [], $domain = null, $locale = null)
  45. {
  46. $trans = $this->translator->trans($id, $parameters, $domain, $locale);
  47. $this->collectMessage($locale, $domain, $id, $trans, $parameters);
  48. return $trans;
  49. }
  50. /**
  51. * {@inheritdoc}
  52. *
  53. * @deprecated since Symfony 4.2, use the trans() method instead with a %count% parameter
  54. */
  55. public function transChoice($id, $number, array $parameters = [], $domain = null, $locale = null)
  56. {
  57. if ($this->translator instanceof TranslatorInterface) {
  58. $trans = $this->translator->trans($id, ['%count%' => $number] + $parameters, $domain, $locale);
  59. }
  60. $trans = $this->translator->transChoice($id, $number, $parameters, $domain, $locale);
  61. $this->collectMessage($locale, $domain, $id, $trans, ['%count%' => $number] + $parameters);
  62. return $trans;
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function setLocale($locale)
  68. {
  69. $this->translator->setLocale($locale);
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function getLocale()
  75. {
  76. return $this->translator->getLocale();
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function getCatalogue($locale = null)
  82. {
  83. return $this->translator->getCatalogue($locale);
  84. }
  85. /**
  86. * Gets the fallback locales.
  87. *
  88. * @return array The fallback locales
  89. */
  90. public function getFallbackLocales()
  91. {
  92. if ($this->translator instanceof Translator || method_exists($this->translator, 'getFallbackLocales')) {
  93. return $this->translator->getFallbackLocales();
  94. }
  95. return [];
  96. }
  97. /**
  98. * Passes through all unknown calls onto the translator object.
  99. */
  100. public function __call($method, $args)
  101. {
  102. return $this->translator->{$method}(...$args);
  103. }
  104. /**
  105. * @return array
  106. */
  107. public function getCollectedMessages()
  108. {
  109. return $this->messages;
  110. }
  111. /**
  112. * @param string|null $locale
  113. * @param string|null $domain
  114. * @param string $id
  115. * @param string $translation
  116. * @param array|null $parameters
  117. */
  118. private function collectMessage($locale, $domain, $id, $translation, $parameters = [])
  119. {
  120. if (null === $domain) {
  121. $domain = 'messages';
  122. }
  123. $id = (string) $id;
  124. $catalogue = $this->translator->getCatalogue($locale);
  125. $locale = $catalogue->getLocale();
  126. if ($catalogue->defines($id, $domain)) {
  127. $state = self::MESSAGE_DEFINED;
  128. } elseif ($catalogue->has($id, $domain)) {
  129. $state = self::MESSAGE_EQUALS_FALLBACK;
  130. $fallbackCatalogue = $catalogue->getFallbackCatalogue();
  131. while ($fallbackCatalogue) {
  132. if ($fallbackCatalogue->defines($id, $domain)) {
  133. $locale = $fallbackCatalogue->getLocale();
  134. break;
  135. }
  136. $fallbackCatalogue = $fallbackCatalogue->getFallbackCatalogue();
  137. }
  138. } else {
  139. $state = self::MESSAGE_MISSING;
  140. }
  141. $this->messages[] = [
  142. 'locale' => $locale,
  143. 'domain' => $domain,
  144. 'id' => $id,
  145. 'translation' => $translation,
  146. 'parameters' => $parameters,
  147. 'state' => $state,
  148. 'transChoiceNumber' => isset($parameters['%count%']) && is_numeric($parameters['%count%']) ? $parameters['%count%'] : null,
  149. ];
  150. }
  151. }