diff --git a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/translation.html.twig b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/translation.html.twig
index 2a86e4648a..d446854717 100644
--- a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/translation.html.twig
+++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/translation.html.twig
@@ -57,12 +57,23 @@
{% endblock %}
{% block panelContent %}
-
Called Translations
-
- - Defined messages: {{ collector.countdefines }}
- - Fallback messages: {{ collector.countFallbacks }}
- - Missing messages: {{ collector.countMissings }}
-
+ Translation Stats
+
+
+
+ Defined messages |
+ {{ collector.countdefines }} |
+
+
+ Fallback messages |
+ {{ collector.countFallbacks }} |
+
+
+ Missing messages |
+ {{ collector.countMissings }} |
+
+
+
@@ -77,7 +88,10 @@
{{ translator.state(message) }} |
{{ message.locale }} |
{{ message.domain }} |
- {{ message.id }} |
+
+ {{ message.id }}
+ {% if message.count > 1 %} (used {{ message.count }} times){% endif %}
+ |
{{ message.translation }} |
{% endfor %}
diff --git a/src/Symfony/Component/Translation/DataCollector/TranslationDataCollector.php b/src/Symfony/Component/Translation/DataCollector/TranslationDataCollector.php
index 928e2fe8ac..4d9e3db193 100644
--- a/src/Symfony/Component/Translation/DataCollector/TranslationDataCollector.php
+++ b/src/Symfony/Component/Translation/DataCollector/TranslationDataCollector.php
@@ -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']];
}
diff --git a/src/Symfony/Component/Translation/Tests/DataCollector/TranslationDataCollectorTest.php b/src/Symfony/Component/Translation/Tests/DataCollector/TranslationDataCollectorTest.php
new file mode 100644
index 0000000000..4f186086e2
--- /dev/null
+++ b/src/Symfony/Component/Translation/Tests/DataCollector/TranslationDataCollectorTest.php
@@ -0,0 +1,99 @@
+
+ *
+ * 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()));
+ }
+}