DataCollectorTranslator.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. 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(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_class($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. public function warmUp(string $cacheDir)
  72. {
  73. if ($this->translator instanceof WarmableInterface) {
  74. $this->translator->warmUp($cacheDir);
  75. }
  76. }
  77. /**
  78. * Gets the fallback locales.
  79. *
  80. * @return array The fallback locales
  81. */
  82. public function getFallbackLocales()
  83. {
  84. if ($this->translator instanceof Translator || method_exists($this->translator, 'getFallbackLocales')) {
  85. return $this->translator->getFallbackLocales();
  86. }
  87. return [];
  88. }
  89. /**
  90. * Passes through all unknown calls onto the translator object.
  91. */
  92. public function __call(string $method, array $args)
  93. {
  94. return $this->translator->{$method}(...$args);
  95. }
  96. /**
  97. * @return array
  98. */
  99. public function getCollectedMessages()
  100. {
  101. return $this->messages;
  102. }
  103. private function collectMessage(?string $locale, ?string $domain, string $id, string $translation, ?array $parameters = [])
  104. {
  105. if (null === $domain) {
  106. $domain = 'messages';
  107. }
  108. $catalogue = $this->translator->getCatalogue($locale);
  109. $locale = $catalogue->getLocale();
  110. $fallbackLocale = null;
  111. if ($catalogue->defines($id, $domain)) {
  112. $state = self::MESSAGE_DEFINED;
  113. } elseif ($catalogue->has($id, $domain)) {
  114. $state = self::MESSAGE_EQUALS_FALLBACK;
  115. $fallbackCatalogue = $catalogue->getFallbackCatalogue();
  116. while ($fallbackCatalogue) {
  117. if ($fallbackCatalogue->defines($id, $domain)) {
  118. $fallbackLocale = $fallbackCatalogue->getLocale();
  119. break;
  120. }
  121. $fallbackCatalogue = $fallbackCatalogue->getFallbackCatalogue();
  122. }
  123. } else {
  124. $state = self::MESSAGE_MISSING;
  125. }
  126. $this->messages[] = [
  127. 'locale' => $locale,
  128. 'fallbackLocale' => $fallbackLocale,
  129. 'domain' => $domain,
  130. 'id' => $id,
  131. 'translation' => $translation,
  132. 'parameters' => $parameters,
  133. 'state' => $state,
  134. 'transChoiceNumber' => isset($parameters['%count%']) && is_numeric($parameters['%count%']) ? $parameters['%count%'] : null,
  135. ];
  136. }
  137. }