Skip to content
Closed
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,43 @@
<td><code>{{ message.domain }}</code></td>
<td>
<code>{{ message.id }}</code>

{% if message.count > 1 %}<br><small style="color: gray;">(used {{ message.count }} times)</small>{% endif %}
{% if message.transChoiceNumber is not null %}<br><small style="color: gray;">(use pluralization)</small>{% endif %}

{% if message.parameters|length > 0 %}
<div>
[<a href="#" onclick="return openParameters(this);" style="text-decoration: none;"
title="Toggle parameters display" data-target-id="parameters-{{ loop.index }}" >
<img alt="+" src="data:image/gif;base64,R0lGODlhEgASAMQTANft99/v+Ga44bHb8ITG52S44dXs9+z1+uPx+YvK6WC24G+944/M6W28443L6dnu+Ge54v/+/l614P///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAABMALAAAAAASABIAQAVS4DQBTiOd6LkwgJgeUSzHSDoNaZ4PU6FLgYBA5/vFID/DbylRGiNIZu74I0h1hNsVxbNuUV4d9SsZM2EzWe1qThVzwWFOAFCQFa1RQq6DJB4iIQA7" style="display: inline; width: 12px; height: 12px;" />
<img alt="-" src="data:image/gif;base64,R0lGODlhEgASAMQSANft94TG57Hb8GS44ez1+mC24IvK6ePx+Wa44dXs92+942e54o3L6W2844/M6dnu+P/+/l614P///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAABIALAAAAAASABIAQAVCoCQBTBOd6Kk4gJhGBCTPxysJb44K0qD/ER/wlxjmisZkMqBEBW5NHrMZmVKvv9hMVsO+hE0EoNAstEYGxG9heIhCADs=" style="display: none; width: 12px; height: 12px;" />
<span style="vertical-align:top">Parameters</span>
</a>]

<div id="parameters-{{ loop.index }}" style="display: none;">
{% for parameters in message.parameters %}
{{ profiler_dump(parameters) }}
{% if not loop.last %}<br />{% endif %}
{% endfor %}
</div>
</div>
{% endif %}
</td>
<td><code>{{ message.translation }}</code></td>
</tr>
{% endfor %}
</table>

<script type="text/javascript">
function openParameters(link) {
"use strict";

var imgs = link.children,
target = link.getAttribute('data-target-id');

Sfjs.toggle(target, imgs[0], imgs[1]);
}
</script>
{% endblock %}

{% macro state(translation) %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,14 @@ private function sanitizeCollectedMessages($messages)

if (!isset($result[$messageId])) {
$message['count'] = 1;
$message['parameters'] = !empty($message['parameters']) ? array($message['parameters']) : array();
$messages[$key]['translation'] = $this->sanitizeString($message['translation']);
$result[$messageId] = $message;
} else {
if (!empty($message['parameters'])) {
$result[$messageId]['parameters'][] = $message['parameters'];
}

$result[$messageId]['count']++;
}

Expand Down
12 changes: 8 additions & 4 deletions src/Symfony/Component/Translation/DataCollectorTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function __construct(TranslatorInterface $translator)
public function trans($id, array $parameters = array(), $domain = null, $locale = null)
{
$trans = $this->translator->trans($id, $parameters, $domain, $locale);
$this->collectMessage($locale, $domain, $id, $trans);
$this->collectMessage($locale, $domain, $id, $trans, $parameters);

return $trans;
}
Expand All @@ -59,7 +59,7 @@ public function trans($id, array $parameters = array(), $domain = null, $locale
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
{
$trans = $this->translator->transChoice($id, $number, $parameters, $domain, $locale);
$this->collectMessage($locale, $domain, $id, $trans);
$this->collectMessage($locale, $domain, $id, $trans, $parameters, $number);

return $trans;
}
Expand Down Expand Up @@ -112,9 +112,11 @@ public function getCollectedMessages()
* @param string|null $locale
* @param string|null $domain
* @param string $id
* @param string $trans
* @param string $translation
* @param array|null $parameters
* @param int|null $number
*/
private function collectMessage($locale, $domain, $id, $translation)
private function collectMessage($locale, $domain, $id, $translation, $parameters = array(), $number = null)
{
if (null === $domain) {
$domain = 'messages';
Expand Down Expand Up @@ -146,6 +148,8 @@ private function collectMessage($locale, $domain, $id, $translation)
'domain' => $domain,
'id' => $id,
'translation' => $translation,
'parameters' => $parameters,
'transChoiceNumber' => $number,
'state' => $state,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,44 @@ public function testCollect()
'locale' => 'en',
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_DEFINED,
'parameters' => array(),
'transChoiceNumber' => null,
),
array(
'id' => 'bar',
'translation' => 'bar (fr)',
'locale' => 'fr',
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
'parameters' => array(),
'transChoiceNumber' => null,
),
array(
'id' => 'choice',
'translation' => 'choice',
'locale' => 'en',
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_MISSING,
'parameters' => array('%count%' => 3),
'transChoiceNumber' => 3,
),
array(
'id' => 'choice',
'translation' => 'choice',
'locale' => 'en',
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_MISSING,
'parameters' => array('%count%' => 3),
'transChoiceNumber' => 3,
),
array(
'id' => 'choice',
'translation' => 'choice',
'locale' => 'en',
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_MISSING,
'parameters' => array('%count%' => 4, '%foo%' => 'bar'),
'transChoiceNumber' => 4,
),
);
$expectedMessages = array(
Expand All @@ -77,6 +94,8 @@ public function testCollect()
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_DEFINED,
'count' => 1,
'parameters' => array(),
'transChoiceNumber' => null,
),
array(
'id' => 'bar',
Expand All @@ -85,14 +104,22 @@ public function testCollect()
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
'count' => 1,
'parameters' => array(),
'transChoiceNumber' => null,
),
array(
'id' => 'choice',
'translation' => 'choice',
'locale' => 'en',
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_MISSING,
'count' => 2,
'count' => 3,
'parameters' => array(
array('%count%' => 3),
array('%count%' => 3),
array('%count%' => 4, '%foo%' => 'bar'),
),
'transChoiceNumber' => 3,
),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function testCollectMessages()
$collector->trans('bar');
$collector->transChoice('choice', 0);
$collector->trans('bar_ru');
$collector->trans('bar_ru', array('foo' => 'bar'));

$expectedMessages = array();
$expectedMessages[] = array(
Expand All @@ -40,27 +41,44 @@ public function testCollectMessages()
'locale' => 'en',
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_DEFINED,
'parameters' => array(),
'transChoiceNumber' => null,
);
$expectedMessages[] = array(
'id' => 'bar',
'translation' => 'bar (fr)',
'locale' => 'fr',
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
'parameters' => array(),
'transChoiceNumber' => null,
);
$expectedMessages[] = array(
'id' => 'choice',
'translation' => 'choice',
'locale' => 'en',
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_MISSING,
'parameters' => array(),
'transChoiceNumber' => 0,
);
$expectedMessages[] = array(
'id' => 'bar_ru',
'translation' => 'bar (ru)',
'locale' => 'ru',
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
'parameters' => array(),
'transChoiceNumber' => null,
);
$expectedMessages[] = array(
'id' => 'bar_ru',
'translation' => 'bar (ru)',
'locale' => 'ru',
'domain' => 'messages',
'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
'parameters' => array('foo' => 'bar'),
'transChoiceNumber' => null,
);

$this->assertEquals($expectedMessages, $collector->getCollectedMessages());
Expand Down