菜谱项目

TranslationDataCollector.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. }
  42. /**
  43. * @return array
  44. */
  45. public function getMessages()
  46. {
  47. return isset($this->data['messages']) ? $this->data['messages'] : array();
  48. }
  49. /**
  50. * @return int
  51. */
  52. public function getCountMissings()
  53. {
  54. return isset($this->data[DataCollectorTranslator::MESSAGE_MISSING]) ? $this->data[DataCollectorTranslator::MESSAGE_MISSING] : 0;
  55. }
  56. /**
  57. * @return int
  58. */
  59. public function getCountFallbacks()
  60. {
  61. return isset($this->data[DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK]) ? $this->data[DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK] : 0;
  62. }
  63. /**
  64. * @return int
  65. */
  66. public function getCountDefines()
  67. {
  68. return isset($this->data[DataCollectorTranslator::MESSAGE_DEFINED]) ? $this->data[DataCollectorTranslator::MESSAGE_DEFINED] : 0;
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function getName()
  74. {
  75. return 'translation';
  76. }
  77. private function sanitizeCollectedMessages($messages)
  78. {
  79. $result = array();
  80. foreach ($messages as $key => $message) {
  81. $messageId = $message['locale'].$message['domain'].$message['id'];
  82. if (!isset($result[$messageId])) {
  83. $message['count'] = 1;
  84. $message['parameters'] = !empty($message['parameters']) ? array($message['parameters']) : array();
  85. $messages[$key]['translation'] = $this->sanitizeString($message['translation']);
  86. $result[$messageId] = $message;
  87. } else {
  88. if (!empty($message['parameters'])) {
  89. $result[$messageId]['parameters'][] = $message['parameters'];
  90. }
  91. ++$result[$messageId]['count'];
  92. }
  93. unset($messages[$key]);
  94. }
  95. return $result;
  96. }
  97. private function computeCount($messages)
  98. {
  99. $count = array(
  100. DataCollectorTranslator::MESSAGE_DEFINED => 0,
  101. DataCollectorTranslator::MESSAGE_MISSING => 0,
  102. DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK => 0,
  103. );
  104. foreach ($messages as $message) {
  105. ++$count[$message['state']];
  106. }
  107. return $count;
  108. }
  109. private function sanitizeString($string, $length = 80)
  110. {
  111. $string = trim(preg_replace('/\s+/', ' ', $string));
  112. if (false !== $encoding = mb_detect_encoding($string, null, true)) {
  113. if (mb_strlen($string, $encoding) > $length) {
  114. return mb_substr($string, 0, $length - 3, $encoding).'...';
  115. }
  116. } elseif (strlen($string) > $length) {
  117. return substr($string, 0, $length - 3).'...';
  118. }
  119. return $string;
  120. }
  121. }