TranslationDataCollector.php 4.6 KB

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