DataCollectorTranslator.php 5.8 KB

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