DataCollectorTranslator.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. * @final since Symfony 7.1
  19. */
  20. class DataCollectorTranslator implements TranslatorInterface, TranslatorBagInterface, LocaleAwareInterface, WarmableInterface
  21. {
  22. public const MESSAGE_DEFINED = 0;
  23. public const MESSAGE_MISSING = 1;
  24. public const MESSAGE_EQUALS_FALLBACK = 2;
  25. private TranslatorInterface $translator;
  26. private array $messages = [];
  27. /**
  28. * @param TranslatorInterface&TranslatorBagInterface&LocaleAwareInterface $translator
  29. */
  30. public function __construct(TranslatorInterface $translator)
  31. {
  32. if (!$translator instanceof TranslatorBagInterface || !$translator instanceof LocaleAwareInterface) {
  33. throw new InvalidArgumentException(sprintf('The Translator "%s" must implement TranslatorInterface, TranslatorBagInterface and LocaleAwareInterface.', get_debug_type($translator)));
  34. }
  35. $this->translator = $translator;
  36. }
  37. public function trans(?string $id, array $parameters = [], ?string $domain = null, ?string $locale = null): string
  38. {
  39. $trans = $this->translator->trans($id = (string) $id, $parameters, $domain, $locale);
  40. $this->collectMessage($locale, $domain, $id, $trans, $parameters);
  41. return $trans;
  42. }
  43. public function setLocale(string $locale): void
  44. {
  45. $this->translator->setLocale($locale);
  46. }
  47. public function getLocale(): string
  48. {
  49. return $this->translator->getLocale();
  50. }
  51. public function getCatalogue(?string $locale = null): MessageCatalogueInterface
  52. {
  53. return $this->translator->getCatalogue($locale);
  54. }
  55. public function getCatalogues(): array
  56. {
  57. return $this->translator->getCatalogues();
  58. }
  59. public function warmUp(string $cacheDir, ?string $buildDir = null): array
  60. {
  61. if ($this->translator instanceof WarmableInterface) {
  62. return (array) $this->translator->warmUp($cacheDir, $buildDir);
  63. }
  64. return [];
  65. }
  66. /**
  67. * Gets the fallback locales.
  68. */
  69. public function getFallbackLocales(): array
  70. {
  71. if ($this->translator instanceof Translator || method_exists($this->translator, 'getFallbackLocales')) {
  72. return $this->translator->getFallbackLocales();
  73. }
  74. return [];
  75. }
  76. public function __call(string $method, array $args): mixed
  77. {
  78. return $this->translator->{$method}(...$args);
  79. }
  80. public function getCollectedMessages(): array
  81. {
  82. return $this->messages;
  83. }
  84. private function collectMessage(?string $locale, ?string $domain, string $id, string $translation, ?array $parameters = []): void
  85. {
  86. $domain ??= 'messages';
  87. $catalogue = $this->translator->getCatalogue($locale);
  88. $locale = $catalogue->getLocale();
  89. $fallbackLocale = null;
  90. if ($catalogue->defines($id, $domain)) {
  91. $state = self::MESSAGE_DEFINED;
  92. } elseif ($catalogue->has($id, $domain)) {
  93. $state = self::MESSAGE_EQUALS_FALLBACK;
  94. $fallbackCatalogue = $catalogue->getFallbackCatalogue();
  95. while ($fallbackCatalogue) {
  96. if ($fallbackCatalogue->defines($id, $domain)) {
  97. $fallbackLocale = $fallbackCatalogue->getLocale();
  98. break;
  99. }
  100. $fallbackCatalogue = $fallbackCatalogue->getFallbackCatalogue();
  101. }
  102. } else {
  103. $state = self::MESSAGE_MISSING;
  104. }
  105. $this->messages[] = [
  106. 'locale' => $locale,
  107. 'fallbackLocale' => $fallbackLocale,
  108. 'domain' => $domain,
  109. 'id' => $id,
  110. 'translation' => $translation,
  111. 'parameters' => $parameters,
  112. 'state' => $state,
  113. 'transChoiceNumber' => isset($parameters['%count%']) && is_numeric($parameters['%count%']) ? $parameters['%count%'] : null,
  114. ];
  115. }
  116. }