[Translation] Add parameters to DataCollectorTranslator
This commit is contained in:
parent
f5fefeff49
commit
85ae760d05
@ -114,12 +114,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) %}
|
||||
|
@ -101,9 +101,14 @@ class TranslationDataCollector extends DataCollector implements LateDataCollecto
|
||||
|
||||
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']++;
|
||||
}
|
||||
|
||||
|
@ -48,7 +48,7 @@ class DataCollectorTranslator implements TranslatorInterface, TranslatorBagInter
|
||||
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;
|
||||
}
|
||||
@ -59,7 +59,7 @@ class DataCollectorTranslator implements TranslatorInterface, TranslatorBagInter
|
||||
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;
|
||||
}
|
||||
@ -112,9 +112,11 @@ class DataCollectorTranslator implements TranslatorInterface, TranslatorBagInter
|
||||
* @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';
|
||||
@ -146,6 +148,8 @@ class DataCollectorTranslator implements TranslatorInterface, TranslatorBagInter
|
||||
'domain' => $domain,
|
||||
'id' => $id,
|
||||
'translation' => $translation,
|
||||
'parameters' => $parameters,
|
||||
'transChoiceNumber' => $number,
|
||||
'state' => $state,
|
||||
);
|
||||
}
|
||||
|
@ -46,6 +46,8 @@ class TranslationDataCollectorTest extends \PHPUnit_Framework_TestCase
|
||||
'locale' => 'en',
|
||||
'domain' => 'messages',
|
||||
'state' => DataCollectorTranslator::MESSAGE_DEFINED,
|
||||
'parameters' => array(),
|
||||
'transChoiceNumber' => null,
|
||||
),
|
||||
array(
|
||||
'id' => 'bar',
|
||||
@ -53,6 +55,8 @@ class TranslationDataCollectorTest extends \PHPUnit_Framework_TestCase
|
||||
'locale' => 'fr',
|
||||
'domain' => 'messages',
|
||||
'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
|
||||
'parameters' => array(),
|
||||
'transChoiceNumber' => null,
|
||||
),
|
||||
array(
|
||||
'id' => 'choice',
|
||||
@ -60,6 +64,8 @@ class TranslationDataCollectorTest extends \PHPUnit_Framework_TestCase
|
||||
'locale' => 'en',
|
||||
'domain' => 'messages',
|
||||
'state' => DataCollectorTranslator::MESSAGE_MISSING,
|
||||
'parameters' => array('%count%' => 3),
|
||||
'transChoiceNumber' => 3,
|
||||
),
|
||||
array(
|
||||
'id' => 'choice',
|
||||
@ -67,6 +73,17 @@ class TranslationDataCollectorTest extends \PHPUnit_Framework_TestCase
|
||||
'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(
|
||||
@ -77,6 +94,8 @@ class TranslationDataCollectorTest extends \PHPUnit_Framework_TestCase
|
||||
'domain' => 'messages',
|
||||
'state' => DataCollectorTranslator::MESSAGE_DEFINED,
|
||||
'count' => 1,
|
||||
'parameters' => array(),
|
||||
'transChoiceNumber' => null,
|
||||
),
|
||||
array(
|
||||
'id' => 'bar',
|
||||
@ -85,6 +104,8 @@ class TranslationDataCollectorTest extends \PHPUnit_Framework_TestCase
|
||||
'domain' => 'messages',
|
||||
'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
|
||||
'count' => 1,
|
||||
'parameters' => array(),
|
||||
'transChoiceNumber' => null,
|
||||
),
|
||||
array(
|
||||
'id' => 'choice',
|
||||
@ -92,7 +113,13 @@ class TranslationDataCollectorTest extends \PHPUnit_Framework_TestCase
|
||||
'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,
|
||||
),
|
||||
);
|
||||
|
||||
|
@ -32,6 +32,7 @@ class DataCollectorTranslatorTest extends \PHPUnit_Framework_TestCase
|
||||
$collector->trans('bar');
|
||||
$collector->transChoice('choice', 0);
|
||||
$collector->trans('bar_ru');
|
||||
$collector->trans('bar_ru', array('foo' => 'bar'));
|
||||
|
||||
$expectedMessages = array();
|
||||
$expectedMessages[] = array(
|
||||
@ -40,6 +41,8 @@ class DataCollectorTranslatorTest extends \PHPUnit_Framework_TestCase
|
||||
'locale' => 'en',
|
||||
'domain' => 'messages',
|
||||
'state' => DataCollectorTranslator::MESSAGE_DEFINED,
|
||||
'parameters' => array(),
|
||||
'transChoiceNumber' => null,
|
||||
);
|
||||
$expectedMessages[] = array(
|
||||
'id' => 'bar',
|
||||
@ -47,6 +50,8 @@ class DataCollectorTranslatorTest extends \PHPUnit_Framework_TestCase
|
||||
'locale' => 'fr',
|
||||
'domain' => 'messages',
|
||||
'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
|
||||
'parameters' => array(),
|
||||
'transChoiceNumber' => null,
|
||||
);
|
||||
$expectedMessages[] = array(
|
||||
'id' => 'choice',
|
||||
@ -54,6 +59,8 @@ class DataCollectorTranslatorTest extends \PHPUnit_Framework_TestCase
|
||||
'locale' => 'en',
|
||||
'domain' => 'messages',
|
||||
'state' => DataCollectorTranslator::MESSAGE_MISSING,
|
||||
'parameters' => array(),
|
||||
'transChoiceNumber' => 0,
|
||||
);
|
||||
$expectedMessages[] = array(
|
||||
'id' => 'bar_ru',
|
||||
@ -61,6 +68,17 @@ class DataCollectorTranslatorTest extends \PHPUnit_Framework_TestCase
|
||||
'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());
|
||||
|
Reference in New Issue
Block a user