| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\Translation\DataCollector;
|
|
|
13 |
|
|
|
14 |
use Symfony\Component\HttpFoundation\Request;
|
|
|
15 |
use Symfony\Component\HttpFoundation\Response;
|
|
|
16 |
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
|
|
|
17 |
use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
|
|
|
18 |
use Symfony\Component\Translation\DataCollectorTranslator;
|
|
|
19 |
use Symfony\Component\VarDumper\Cloner\Data;
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
|
|
|
23 |
*
|
|
|
24 |
* @final
|
|
|
25 |
*/
|
|
|
26 |
class TranslationDataCollector extends DataCollector implements LateDataCollectorInterface
|
|
|
27 |
{
|
|
|
28 |
private DataCollectorTranslator $translator;
|
|
|
29 |
|
|
|
30 |
public function __construct(DataCollectorTranslator $translator)
|
|
|
31 |
{
|
|
|
32 |
$this->translator = $translator;
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
public function lateCollect()
|
|
|
36 |
{
|
|
|
37 |
$messages = $this->sanitizeCollectedMessages($this->translator->getCollectedMessages());
|
|
|
38 |
|
|
|
39 |
$this->data += $this->computeCount($messages);
|
|
|
40 |
$this->data['messages'] = $messages;
|
|
|
41 |
|
|
|
42 |
$this->data = $this->cloneVar($this->data);
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
public function collect(Request $request, Response $response, \Throwable $exception = null)
|
|
|
46 |
{
|
|
|
47 |
$this->data['locale'] = $this->translator->getLocale();
|
|
|
48 |
$this->data['fallback_locales'] = $this->translator->getFallbackLocales();
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
public function reset()
|
|
|
52 |
{
|
|
|
53 |
$this->data = [];
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
public function getMessages(): array|Data
|
|
|
57 |
{
|
|
|
58 |
return $this->data['messages'] ?? [];
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
public function getCountMissings(): int
|
|
|
62 |
{
|
|
|
63 |
return $this->data[DataCollectorTranslator::MESSAGE_MISSING] ?? 0;
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
public function getCountFallbacks(): int
|
|
|
67 |
{
|
|
|
68 |
return $this->data[DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK] ?? 0;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
public function getCountDefines(): int
|
|
|
72 |
{
|
|
|
73 |
return $this->data[DataCollectorTranslator::MESSAGE_DEFINED] ?? 0;
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
public function getLocale()
|
|
|
77 |
{
|
|
|
78 |
return !empty($this->data['locale']) ? $this->data['locale'] : null;
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
/**
|
|
|
82 |
* @internal
|
|
|
83 |
*/
|
|
|
84 |
public function getFallbackLocales()
|
|
|
85 |
{
|
|
|
86 |
return (isset($this->data['fallback_locales']) && \count($this->data['fallback_locales']) > 0) ? $this->data['fallback_locales'] : [];
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
public function getName(): string
|
|
|
90 |
{
|
|
|
91 |
return 'translation';
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
private function sanitizeCollectedMessages(array $messages)
|
|
|
95 |
{
|
|
|
96 |
$result = [];
|
|
|
97 |
foreach ($messages as $key => $message) {
|
|
|
98 |
$messageId = $message['locale'].$message['domain'].$message['id'];
|
|
|
99 |
|
|
|
100 |
if (!isset($result[$messageId])) {
|
|
|
101 |
$message['count'] = 1;
|
|
|
102 |
$message['parameters'] = !empty($message['parameters']) ? [$message['parameters']] : [];
|
|
|
103 |
$messages[$key]['translation'] = $this->sanitizeString($message['translation']);
|
|
|
104 |
$result[$messageId] = $message;
|
|
|
105 |
} else {
|
|
|
106 |
if (!empty($message['parameters'])) {
|
|
|
107 |
$result[$messageId]['parameters'][] = $message['parameters'];
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
++$result[$messageId]['count'];
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
unset($messages[$key]);
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
return $result;
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
private function computeCount(array $messages)
|
|
|
120 |
{
|
|
|
121 |
$count = [
|
|
|
122 |
DataCollectorTranslator::MESSAGE_DEFINED => 0,
|
|
|
123 |
DataCollectorTranslator::MESSAGE_MISSING => 0,
|
|
|
124 |
DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK => 0,
|
|
|
125 |
];
|
|
|
126 |
|
|
|
127 |
foreach ($messages as $message) {
|
|
|
128 |
++$count[$message['state']];
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
return $count;
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
private function sanitizeString(string $string, int $length = 80)
|
|
|
135 |
{
|
|
|
136 |
$string = trim(preg_replace('/\s+/', ' ', $string));
|
|
|
137 |
|
|
|
138 |
if (false !== $encoding = mb_detect_encoding($string, null, true)) {
|
|
|
139 |
if (mb_strlen($string, $encoding) > $length) {
|
|
|
140 |
return mb_substr($string, 0, $length - 3, $encoding).'...';
|
|
|
141 |
}
|
|
|
142 |
} elseif (\strlen($string) > $length) {
|
|
|
143 |
return substr($string, 0, $length - 3).'...';
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
return $string;
|
|
|
147 |
}
|
|
|
148 |
}
|