SymfonyComponentTranslationDataCollectorTranslationDataCollector

The Simple Calendar Symfony Component Translation DataCollector TranslationDataCollector class.

Defined (1)

The class is defined in the following location(s).

/vendor/symfony/translation/DataCollector/TranslationDataCollector.php  
  1. class TranslationDataCollector extends DataCollector implements LateDataCollectorInterface 
  2. /** 
  3. * @var DataCollectorTranslator 
  4. */ 
  5. private $translator; 
  6.  
  7. /** 
  8. * @param DataCollectorTranslator $translator 
  9. */ 
  10. public function __construct(DataCollectorTranslator $translator) 
  11. $this->translator = $translator; 
  12.  
  13. /** 
  14. * {@inheritdoc} 
  15. */ 
  16. public function lateCollect() 
  17. $messages = $this->sanitizeCollectedMessages($this->translator->getCollectedMessages()); 
  18.  
  19. $this->data = $this->computeCount($messages); 
  20. $this->data['messages'] = $messages; 
  21.  
  22. /** 
  23. * {@inheritdoc} 
  24. */ 
  25. public function collect(Request $request, Response $response, \Exception $exception = null) 
  26.  
  27. /** 
  28. * @return array 
  29. */ 
  30. public function getMessages() 
  31. return isset($this->data['messages']) ? $this->data['messages'] : array(); 
  32.  
  33. /** 
  34. * @return int 
  35. */ 
  36. public function getCountMissings() 
  37. return isset($this->data[DataCollectorTranslator::MESSAGE_MISSING]) ? $this->data[DataCollectorTranslator::MESSAGE_MISSING] : 0; 
  38.  
  39. /** 
  40. * @return int 
  41. */ 
  42. public function getCountFallbacks() 
  43. return isset($this->data[DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK]) ? $this->data[DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK] : 0; 
  44.  
  45. /** 
  46. * @return int 
  47. */ 
  48. public function getCountDefines() 
  49. return isset($this->data[DataCollectorTranslator::MESSAGE_DEFINED]) ? $this->data[DataCollectorTranslator::MESSAGE_DEFINED] : 0; 
  50.  
  51. /** 
  52. * {@inheritdoc} 
  53. */ 
  54. public function getName() 
  55. return 'translation'; 
  56.  
  57. private function sanitizeCollectedMessages($messages) 
  58. $result = array(); 
  59. foreach ($messages as $key => $message) { 
  60. $messageId = $message['locale'].$message['domain'].$message['id']; 
  61.  
  62. if (!isset($result[$messageId])) { 
  63. $message['count'] = 1; 
  64. $message['parameters'] = !empty($message['parameters']) ? array($message['parameters']) : array(); 
  65. $messages[$key]['translation'] = $this->sanitizeString($message['translation']); 
  66. $result[$messageId] = $message; 
  67. } else { 
  68. if (!empty($message['parameters'])) { 
  69. $result[$messageId]['parameters'][] = $message['parameters']; 
  70.  
  71. ++$result[$messageId]['count']; 
  72.  
  73. unset($messages[$key]); 
  74.  
  75. return $result; 
  76.  
  77. private function computeCount($messages) 
  78. $count = array( 
  79. DataCollectorTranslator::MESSAGE_DEFINED => 0,  
  80. DataCollectorTranslator::MESSAGE_MISSING => 0,  
  81. DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK => 0,  
  82. ); 
  83.  
  84. foreach ($messages as $message) { 
  85. ++$count[$message['state']]; 
  86.  
  87. return $count; 
  88.  
  89. private function sanitizeString($string, $length = 80) 
  90. $string = trim(preg_replace('/\s+/', ' ', $string)); 
  91.  
  92. if (false !== $encoding = mb_detect_encoding($string, null, true)) { 
  93. if (mb_strlen($string, $encoding) > $length) { 
  94. return mb_substr($string, 0, $length - 3, $encoding).'...'; 
  95. } elseif (strlen($string) > $length) { 
  96. return substr($string, 0, $length - 3).'...'; 
  97.  
  98. return $string;