feature #14185 [Translation][Profiler]added the number of times a translation has been used. (aitboudad)
This PR was merged into the 2.7 branch.
Discussion
----------
[Translation][Profiler]added the number of times a translation has been used.
| Q | A
| ------------- | ---
| Bug fix? | no
| New feature? | yes
| BC breaks? | no
| Deprecations? | no
| Fixed tickets | #14183
| Tests pass? | yes
| License | MIT
Commits
-------
b470a5b
[Translation][Profiler] added the number of times a translation has been used.
This commit is contained in:
commit
04ad2a73b2
@ -57,12 +57,23 @@
|
||||
{% endblock %}
|
||||
|
||||
{% block panelContent %}
|
||||
<h2>Called Translations</h2>
|
||||
<ul>
|
||||
<li><strong>Defined messages: {{ collector.countdefines }}</strong></li>
|
||||
<li><strong>Fallback messages: {{ collector.countFallbacks }}</strong></li>
|
||||
<li><strong>Missing messages: {{ collector.countMissings }}</strong></li>
|
||||
</ul>
|
||||
<h2>Translation Stats</h2>
|
||||
<table>
|
||||
<tbody>
|
||||
<tr>
|
||||
<th>Defined messages</th>
|
||||
<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>
|
||||
<tr>
|
||||
@ -77,7 +88,10 @@
|
||||
<td><code>{{ translator.state(message) }}</code></td>
|
||||
<td><code>{{ message.locale }}</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>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
|
@ -40,8 +40,10 @@ class TranslationDataCollector extends DataCollector implements LateDataCollecto
|
||||
*/
|
||||
public function lateCollect()
|
||||
{
|
||||
$this->data = $this->computeCount();
|
||||
$this->data['messages'] = $this->sanitizeCollectedMessages($this->translator->getCollectedMessages());
|
||||
$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)
|
||||
{
|
||||
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(
|
||||
DataCollectorTranslator::MESSAGE_DEFINED => 0,
|
||||
@ -108,7 +120,7 @@ class TranslationDataCollector extends DataCollector implements LateDataCollecto
|
||||
DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK => 0,
|
||||
);
|
||||
|
||||
foreach ($this->translator->getCollectedMessages() as $message) {
|
||||
foreach ($messages as $message) {
|
||||
++$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