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