You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
4.8 KiB

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