[Translation][Profiler] added the number of times a translation has been used.
This commit is contained in:
parent
be0c98efaf
commit
b470a5bfb6
@ -57,12 +57,23 @@
|
|||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block panelContent %}
|
{% block panelContent %}
|
||||||
<h2>Called Translations</h2>
|
<h2>Translation Stats</h2>
|
||||||
<ul>
|
<table>
|
||||||
<li><strong>Defined messages: {{ collector.countdefines }}</strong></li>
|
<tbody>
|
||||||
<li><strong>Fallback messages: {{ collector.countFallbacks }}</strong></li>
|
<tr>
|
||||||
<li><strong>Missing messages: {{ collector.countMissings }}</strong></li>
|
<th>Defined messages</th>
|
||||||
</ul>
|
<td><pre>{{ collector.countdefines }}</pre></td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<th scope="col" style="width: 30%">Fallback messages</th>
|
||||||
|
<td scope="col" style="width: 60%"><pre>{{ collector.countFallbacks }}</pre></td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<th>Missing messages</th>
|
||||||
|
<td><pre>{{ collector.countMissings }}</pre></td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
|
||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
@ -77,7 +88,10 @@
|
|||||||
<td><code>{{ translator.state(message) }}</code></td>
|
<td><code>{{ translator.state(message) }}</code></td>
|
||||||
<td><code>{{ message.locale }}</code></td>
|
<td><code>{{ message.locale }}</code></td>
|
||||||
<td><code>{{ message.domain }}</code></td>
|
<td><code>{{ message.domain }}</code></td>
|
||||||
<td><code>{{ message.id }}</code></td>
|
<td>
|
||||||
|
<code>{{ message.id }}</code>
|
||||||
|
{% if message.count > 1 %}<br><small style="color: gray;">(used {{ message.count }} times)</small>{% endif %}
|
||||||
|
</td>
|
||||||
<td><code>{{ message.translation }}</code></td>
|
<td><code>{{ message.translation }}</code></td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
@ -40,8 +40,10 @@ class TranslationDataCollector extends DataCollector implements LateDataCollecto
|
|||||||
*/
|
*/
|
||||||
public function lateCollect()
|
public function lateCollect()
|
||||||
{
|
{
|
||||||
$this->data = $this->computeCount();
|
$messages = $this->sanitizeCollectedMessages($this->translator->getCollectedMessages());
|
||||||
$this->data['messages'] = $this->sanitizeCollectedMessages($this->translator->getCollectedMessages());
|
|
||||||
|
$this->data = $this->computeCount($messages);
|
||||||
|
$this->data['messages'] = $messages;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -94,13 +96,23 @@ class TranslationDataCollector extends DataCollector implements LateDataCollecto
|
|||||||
private function sanitizeCollectedMessages($messages)
|
private function sanitizeCollectedMessages($messages)
|
||||||
{
|
{
|
||||||
foreach ($messages as $key => $message) {
|
foreach ($messages as $key => $message) {
|
||||||
$messages[$key]['translation'] = $this->sanitizeString($messages[$key]['translation']);
|
$messages[$key]['translation'] = $this->sanitizeString($message['translation']);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $messages;
|
return array_reduce($messages, function ($result, $message) {
|
||||||
|
$messageId = $message['locale'].$message['domain'].$message['id'];
|
||||||
|
if (!isset($result[$messageId])) {
|
||||||
|
$message['count'] = 1;
|
||||||
|
$result[$messageId] = $message;
|
||||||
|
} else {
|
||||||
|
$result[$messageId]['count']++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private function computeCount()
|
private function computeCount($messages)
|
||||||
{
|
{
|
||||||
$count = array(
|
$count = array(
|
||||||
DataCollectorTranslator::MESSAGE_DEFINED => 0,
|
DataCollectorTranslator::MESSAGE_DEFINED => 0,
|
||||||
@ -108,7 +120,7 @@ class TranslationDataCollector extends DataCollector implements LateDataCollecto
|
|||||||
DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK => 0,
|
DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK => 0,
|
||||||
);
|
);
|
||||||
|
|
||||||
foreach ($this->translator->getCollectedMessages() as $message) {
|
foreach ($messages as $message) {
|
||||||
++$count[$message['state']];
|
++$count[$message['state']];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,99 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Translation\Tests\DataCollector;
|
||||||
|
|
||||||
|
use Symfony\Component\Translation\DataCollectorTranslator;
|
||||||
|
use Symfony\Component\Translation\DataCollector\TranslationDataCollector;
|
||||||
|
|
||||||
|
class TranslationDataCollectorTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
if (!class_exists('Symfony\Component\HttpKernel\DataCollector\DataCollector')) {
|
||||||
|
$this->markTestSkipped('The "DataCollector" is not available');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public function testCollect()
|
||||||
|
{
|
||||||
|
$collectedMessages = array(
|
||||||
|
array(
|
||||||
|
'id' => 'foo',
|
||||||
|
'translation' => 'foo (en)',
|
||||||
|
'locale' => 'en',
|
||||||
|
'domain' => 'messages',
|
||||||
|
'state' => DataCollectorTranslator::MESSAGE_DEFINED,
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'id' => 'bar',
|
||||||
|
'translation' => 'bar (fr)',
|
||||||
|
'locale' => 'fr',
|
||||||
|
'domain' => 'messages',
|
||||||
|
'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'id' => 'choice',
|
||||||
|
'translation' => 'choice',
|
||||||
|
'locale' => 'en',
|
||||||
|
'domain' => 'messages',
|
||||||
|
'state' => DataCollectorTranslator::MESSAGE_MISSING,
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'id' => 'choice',
|
||||||
|
'translation' => 'choice',
|
||||||
|
'locale' => 'en',
|
||||||
|
'domain' => 'messages',
|
||||||
|
'state' => DataCollectorTranslator::MESSAGE_MISSING,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
$expectedMessages = array(
|
||||||
|
array(
|
||||||
|
'id' => 'foo',
|
||||||
|
'translation' => 'foo (en)',
|
||||||
|
'locale' => 'en',
|
||||||
|
'domain' => 'messages',
|
||||||
|
'state' => DataCollectorTranslator::MESSAGE_DEFINED,
|
||||||
|
'count' => 1,
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'id' => 'bar',
|
||||||
|
'translation' => 'bar (fr)',
|
||||||
|
'locale' => 'fr',
|
||||||
|
'domain' => 'messages',
|
||||||
|
'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
|
||||||
|
'count' => 1,
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'id' => 'choice',
|
||||||
|
'translation' => 'choice',
|
||||||
|
'locale' => 'en',
|
||||||
|
'domain' => 'messages',
|
||||||
|
'state' => DataCollectorTranslator::MESSAGE_MISSING,
|
||||||
|
'count' => 2,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
$translator = $this
|
||||||
|
->getMockBuilder('Symfony\Component\Translation\DataCollectorTranslator')
|
||||||
|
->disableOriginalConstructor()
|
||||||
|
->getMock()
|
||||||
|
;
|
||||||
|
$translator->expects($this->any())->method('getCollectedMessages')->will($this->returnValue($collectedMessages));
|
||||||
|
|
||||||
|
$dataCollector = new TranslationDataCollector($translator);
|
||||||
|
$dataCollector->lateCollect();
|
||||||
|
|
||||||
|
$this->assertEquals(1, $dataCollector->getCountMissings());
|
||||||
|
$this->assertEquals(1, $dataCollector->getCountFallbacks());
|
||||||
|
$this->assertEquals(1, $dataCollector->getCountDefines());
|
||||||
|
$this->assertEquals($expectedMessages, array_values($dataCollector->getMessages()));
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user