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