feature #15139 [Translation] Add parameters to DataCollectorTranslator (damienalexandre)
This PR was submitted for the 2.7 branch but it was merged into the 2.8 branch instead (closes #15139).
Discussion
----------
[Translation] Add parameters to DataCollectorTranslator
| Q | A
| ------------- | ---
| Bug fix? | no
| New feature? | yes
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | -
| License | MIT
| Doc PR | -
The new translation web profiler panel miss a important piece of information: the parameters that can be used for placeholders.
This PR add parameters in the collected translations and display them in the WebProfiler translation list.
![image](https://cloud.githubusercontent.com/assets/225704/8426851/31234cdc-1f13-11e5-8e9c-87420bb43bd5.png)
Original idea by my team-mate @joelwurtz 🤘
Commits
-------
85ae760
[Translation] Add parameters to DataCollectorTranslator
This commit is contained in:
commit
02347d58f0
@ -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