DataCollectorTranslator.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 TranslatorInterface $translator;
  24. private array $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. public function trans(?string $id, array $parameters = [], ?string $domain = null, ?string $locale = null): string
  36. {
  37. $trans = $this->translator->trans($id = (string) $id, $parameters, $domain, $locale);
  38. $this->collectMessage($locale, $domain, $id, $trans, $parameters);
  39. return $trans;
  40. }
  41. /**
  42. * @return void
  43. */
  44. public function setLocale(string $locale)
  45. {
  46. $this->translator->setLocale($locale);
  47. }
  48. public function getLocale(): string
  49. {
  50. return $this->translator->getLocale();
  51. }
  52. public function getCatalogue(?string $locale = null): MessageCatalogueInterface
  53. {
  54. return $this->translator->getCatalogue($locale);
  55. }
  56. public function getCatalogues(): array
  57. {
  58. return $this->translator->getCatalogues();
  59. }
  60. public function warmUp(string $cacheDir, ?string $buildDir = null): array
  61. {
  62. if ($this->translator instanceof WarmableInterface) {
  63. return (array) $this->translator->warmUp($cacheDir, $buildDir);
  64. }
  65. return [];
  66. }
  67. /**
  68. * Gets the fallback locales.
  69. */
  70. public function getFallbackLocales(): array
  71. {
  72. if ($this->translator instanceof Translator || method_exists($this->translator, 'getFallbackLocales')) {
  73. return $this->translator->getFallbackLocales();
  74. }
  75. return [];
  76. }
  77. /**
  78. * @return mixed
  79. */
  80. public function __call(string $method, array $args)
  81. {
  82. return $this->translator->{$method}(...$args);
  83. }
  84. public function getCollectedMessages(): array
  85. {
  86. return $this->messages;
  87. }
  88. private function collectMessage(?string $locale, ?string $domain, string $id, string $translation, ?array $parameters = []): void
  89. {
  90. $domain ??= 'messages';
  91. $catalogue = $this->translator->getCatalogue($locale);
  92. $locale = $catalogue->getLocale();
  93. $fallbackLocale = null;
  94. if ($catalogue->defines($id, $domain)) {
  95. $state = self::MESSAGE_DEFINED;
  96. } elseif ($catalogue->has($id, $domain)) {
  97. $state = self::MESSAGE_EQUALS_FALLBACK;
  98. $fallbackCatalogue = $catalogue->getFallbackCatalogue();
  99. while ($fallbackCatalogue) {
  100. if ($fallbackCatalogue->defines($id, $domain)) {
  101. $fallbackLocale = $fallbackCatalogue->getLocale();
  102. break;
  103. }
  104. $fallbackCatalogue = $fallbackCatalogue->getFallbackCatalogue();
  105. }
  106. } else {
  107. $state = self::MESSAGE_MISSING;
  108. }
  109. $this->messages[] = [
  110. 'locale' => $locale,
  111. 'fallbackLocale' => $fallbackLocale,
  112. 'domain' => $domain,
  113. 'id' => $id,
  114. 'translation' => $translation,
  115. 'parameters' => $parameters,
  116. 'state' => $state,
  117. 'transChoiceNumber' => isset($parameters['%count%']) && is_numeric($parameters['%count%']) ? $parameters['%count%'] : null,
  118. ];
  119. }
  120. }