TranslationDataCollector.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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['locale'] = $this->translator->getLocale();
  35. $this->data['fallback_locales'] = $this->translator->getFallbackLocales();
  36. $this->data = $this->cloneVar($this->data);
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function collect(Request $request, Response $response, \Exception $exception = null)
  42. {
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function reset()
  48. {
  49. $this->data = array();
  50. }
  51. /**
  52. * @return array
  53. */
  54. public function getMessages()
  55. {
  56. return isset($this->data['messages']) ? $this->data['messages'] : array();
  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. public function getFallbackLocales()
  84. {
  85. return (isset($this->data['fallback_locales']) && count($this->data['fallback_locales']) > 0) ? $this->data['fallback_locales'] : array();
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function getName()
  91. {
  92. return 'translation';
  93. }
  94. private function sanitizeCollectedMessages($messages)
  95. {
  96. $result = array();
  97. foreach ($messages as $key => $message) {
  98. $messageId = $message['locale'].$message['domain'].$message['id'];
  99. if (!isset($result[$messageId])) {
  100. $message['count'] = 1;
  101. $message['parameters'] = !empty($message['parameters']) ? array($message['parameters']) : array();
  102. $messages[$key]['translation'] = $this->sanitizeString($message['translation']);
  103. $result[$messageId] = $message;
  104. } else {
  105. if (!empty($message['parameters'])) {
  106. $result[$messageId]['parameters'][] = $message['parameters'];
  107. }
  108. ++$result[$messageId]['count'];
  109. }
  110. unset($messages[$key]);
  111. }
  112. return $result;
  113. }
  114. private function computeCount($messages)
  115. {
  116. $count = array(
  117. DataCollectorTranslator::MESSAGE_DEFINED => 0,
  118. DataCollectorTranslator::MESSAGE_MISSING => 0,
  119. DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK => 0,
  120. );
  121. foreach ($messages as $message) {
  122. ++$count[$message['state']];
  123. }
  124. return $count;
  125. }
  126. private function sanitizeString($string, $length = 80)
  127. {
  128. $string = trim(preg_replace('/\s+/', ' ', $string));
  129. if (false !== $encoding = mb_detect_encoding($string, null, true)) {
  130. if (mb_strlen($string, $encoding) > $length) {
  131. return mb_substr($string, 0, $length - 3, $encoding).'...';
  132. }
  133. } elseif (strlen($string) > $length) {
  134. return substr($string, 0, $length - 3).'...';
  135. }
  136. return $string;
  137. }
  138. }