TranslationDataCollector.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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\DataCollector;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\DataCollector\DataCollector;
  14. use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
  15. use Symfony\Component\Translation\DataCollectorTranslator;
  16. use Symfony\Component\VarDumper\Cloner\Data;
  17. /**
  18. * @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
  19. *
  20. * @final
  21. */
  22. class TranslationDataCollector extends DataCollector implements LateDataCollectorInterface
  23. {
  24. public function __construct(
  25. private DataCollectorTranslator $translator,
  26. ) {
  27. }
  28. public function lateCollect(): void
  29. {
  30. $messages = $this->sanitizeCollectedMessages($this->translator->getCollectedMessages());
  31. $this->data += $this->computeCount($messages);
  32. $this->data['messages'] = $messages;
  33. $this->data = $this->cloneVar($this->data);
  34. }
  35. public function collect(Request $request, Response $response, ?\Throwable $exception = null): void
  36. {
  37. $this->data['locale'] = $this->translator->getLocale();
  38. $this->data['fallback_locales'] = $this->translator->getFallbackLocales();
  39. $this->data['global_parameters'] = $this->translator->getGlobalParameters();
  40. }
  41. public function reset(): void
  42. {
  43. $this->data = [];
  44. }
  45. public function getMessages(): array|Data
  46. {
  47. return $this->data['messages'] ?? [];
  48. }
  49. public function getCountMissings(): int
  50. {
  51. return $this->data[DataCollectorTranslator::MESSAGE_MISSING] ?? 0;
  52. }
  53. public function getCountFallbacks(): int
  54. {
  55. return $this->data[DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK] ?? 0;
  56. }
  57. public function getCountDefines(): int
  58. {
  59. return $this->data[DataCollectorTranslator::MESSAGE_DEFINED] ?? 0;
  60. }
  61. public function getLocale(): ?string
  62. {
  63. return !empty($this->data['locale']) ? $this->data['locale'] : null;
  64. }
  65. /**
  66. * @internal
  67. */
  68. public function getFallbackLocales(): Data|array
  69. {
  70. return (isset($this->data['fallback_locales']) && \count($this->data['fallback_locales']) > 0) ? $this->data['fallback_locales'] : [];
  71. }
  72. /**
  73. * @internal
  74. */
  75. public function getGlobalParameters(): Data|array
  76. {
  77. return $this->data['global_parameters'] ?? [];
  78. }
  79. public function getName(): string
  80. {
  81. return 'translation';
  82. }
  83. private function sanitizeCollectedMessages(array $messages): array
  84. {
  85. $result = [];
  86. foreach ($messages as $key => $message) {
  87. $messageId = $message['locale'].$message['domain'].$message['id'];
  88. if (!isset($result[$messageId])) {
  89. $message['count'] = 1;
  90. $message['parameters'] = !empty($message['parameters']) ? [$message['parameters']] : [];
  91. $messages[$key]['translation'] = $this->sanitizeString($message['translation']);
  92. $result[$messageId] = $message;
  93. } else {
  94. if (!empty($message['parameters'])) {
  95. $result[$messageId]['parameters'][] = $message['parameters'];
  96. }
  97. ++$result[$messageId]['count'];
  98. }
  99. unset($messages[$key]);
  100. }
  101. return $result;
  102. }
  103. private function computeCount(array $messages): array
  104. {
  105. $count = [
  106. DataCollectorTranslator::MESSAGE_DEFINED => 0,
  107. DataCollectorTranslator::MESSAGE_MISSING => 0,
  108. DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK => 0,
  109. ];
  110. foreach ($messages as $message) {
  111. ++$count[$message['state']];
  112. }
  113. return $count;
  114. }
  115. private function sanitizeString(string $string, int $length = 80): string
  116. {
  117. $string = trim(preg_replace('/\s+/', ' ', $string));
  118. if (false !== $encoding = mb_detect_encoding($string, null, true)) {
  119. if (mb_strlen($string, $encoding) > $length) {
  120. return mb_substr($string, 0, $length - 3, $encoding).'...';
  121. }
  122. } elseif (\strlen($string) > $length) {
  123. return substr($string, 0, $length - 3).'...';
  124. }
  125. return $string;
  126. }
  127. }