DataCollectorTranslator.php 4.6 KB

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