DataCollectorTranslator.php 4.7 KB

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