Skip to content

Commit f58e630

Browse files
damienalexandrefabpot
authored andcommitted
[Translation] Add parameters to DataCollectorTranslator
1 parent 8231f7c commit f58e630

File tree

4 files changed

+59
-5
lines changed

4 files changed

+59
-5
lines changed

DataCollector/TranslationDataCollector.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,14 @@ private function sanitizeCollectedMessages($messages)
101101

102102
if (!isset($result[$messageId])) {
103103
$message['count'] = 1;
104+
$message['parameters'] = !empty($message['parameters']) ? array($message['parameters']) : array();
104105
$messages[$key]['translation'] = $this->sanitizeString($message['translation']);
105106
$result[$messageId] = $message;
106107
} else {
108+
if (!empty($message['parameters'])) {
109+
$result[$messageId]['parameters'][] = $message['parameters'];
110+
}
111+
107112
$result[$messageId]['count']++;
108113
}
109114

DataCollectorTranslator.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function __construct(TranslatorInterface $translator)
4848
public function trans($id, array $parameters = array(), $domain = null, $locale = null)
4949
{
5050
$trans = $this->translator->trans($id, $parameters, $domain, $locale);
51-
$this->collectMessage($locale, $domain, $id, $trans);
51+
$this->collectMessage($locale, $domain, $id, $trans, $parameters);
5252

5353
return $trans;
5454
}
@@ -59,7 +59,7 @@ public function trans($id, array $parameters = array(), $domain = null, $locale
5959
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
6060
{
6161
$trans = $this->translator->transChoice($id, $number, $parameters, $domain, $locale);
62-
$this->collectMessage($locale, $domain, $id, $trans);
62+
$this->collectMessage($locale, $domain, $id, $trans, $parameters, $number);
6363

6464
return $trans;
6565
}
@@ -112,9 +112,11 @@ public function getCollectedMessages()
112112
* @param string|null $locale
113113
* @param string|null $domain
114114
* @param string $id
115-
* @param string $trans
115+
* @param string $translation
116+
* @param array|null $parameters
117+
* @param int|null $number
116118
*/
117-
private function collectMessage($locale, $domain, $id, $translation)
119+
private function collectMessage($locale, $domain, $id, $translation, $parameters = array(), $number = null)
118120
{
119121
if (null === $domain) {
120122
$domain = 'messages';
@@ -146,6 +148,8 @@ private function collectMessage($locale, $domain, $id, $translation)
146148
'domain' => $domain,
147149
'id' => $id,
148150
'translation' => $translation,
151+
'parameters' => $parameters,
152+
'transChoiceNumber' => $number,
149153
'state' => $state,
150154
);
151155
}

Tests/DataCollector/TranslationDataCollectorTest.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,44 @@ public function testCollect()
4646
'locale' => 'en',
4747
'domain' => 'messages',
4848
'state' => DataCollectorTranslator::MESSAGE_DEFINED,
49+
'parameters' => array(),
50+
'transChoiceNumber' => null,
4951
),
5052
array(
5153
'id' => 'bar',
5254
'translation' => 'bar (fr)',
5355
'locale' => 'fr',
5456
'domain' => 'messages',
5557
'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
58+
'parameters' => array(),
59+
'transChoiceNumber' => null,
5660
),
5761
array(
5862
'id' => 'choice',
5963
'translation' => 'choice',
6064
'locale' => 'en',
6165
'domain' => 'messages',
6266
'state' => DataCollectorTranslator::MESSAGE_MISSING,
67+
'parameters' => array('%count%' => 3),
68+
'transChoiceNumber' => 3,
6369
),
6470
array(
6571
'id' => 'choice',
6672
'translation' => 'choice',
6773
'locale' => 'en',
6874
'domain' => 'messages',
6975
'state' => DataCollectorTranslator::MESSAGE_MISSING,
76+
'parameters' => array('%count%' => 3),
77+
'transChoiceNumber' => 3,
78+
),
79+
array(
80+
'id' => 'choice',
81+
'translation' => 'choice',
82+
'locale' => 'en',
83+
'domain' => 'messages',
84+
'state' => DataCollectorTranslator::MESSAGE_MISSING,
85+
'parameters' => array('%count%' => 4, '%foo%' => 'bar'),
86+
'transChoiceNumber' => 4,
7087
),
7188
);
7289
$expectedMessages = array(
@@ -77,6 +94,8 @@ public function testCollect()
7794
'domain' => 'messages',
7895
'state' => DataCollectorTranslator::MESSAGE_DEFINED,
7996
'count' => 1,
97+
'parameters' => array(),
98+
'transChoiceNumber' => null,
8099
),
81100
array(
82101
'id' => 'bar',
@@ -85,14 +104,22 @@ public function testCollect()
85104
'domain' => 'messages',
86105
'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
87106
'count' => 1,
107+
'parameters' => array(),
108+
'transChoiceNumber' => null,
88109
),
89110
array(
90111
'id' => 'choice',
91112
'translation' => 'choice',
92113
'locale' => 'en',
93114
'domain' => 'messages',
94115
'state' => DataCollectorTranslator::MESSAGE_MISSING,
95-
'count' => 2,
116+
'count' => 3,
117+
'parameters' => array(
118+
array('%count%' => 3),
119+
array('%count%' => 3),
120+
array('%count%' => 4, '%foo%' => 'bar'),
121+
),
122+
'transChoiceNumber' => 3,
96123
),
97124
);
98125

Tests/DataCollectorTranslatorTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function testCollectMessages()
3232
$collector->trans('bar');
3333
$collector->transChoice('choice', 0);
3434
$collector->trans('bar_ru');
35+
$collector->trans('bar_ru', array('foo' => 'bar'));
3536

3637
$expectedMessages = array();
3738
$expectedMessages[] = array(
@@ -40,27 +41,44 @@ public function testCollectMessages()
4041
'locale' => 'en',
4142
'domain' => 'messages',
4243
'state' => DataCollectorTranslator::MESSAGE_DEFINED,
44+
'parameters' => array(),
45+
'transChoiceNumber' => null,
4346
);
4447
$expectedMessages[] = array(
4548
'id' => 'bar',
4649
'translation' => 'bar (fr)',
4750
'locale' => 'fr',
4851
'domain' => 'messages',
4952
'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
53+
'parameters' => array(),
54+
'transChoiceNumber' => null,
5055
);
5156
$expectedMessages[] = array(
5257
'id' => 'choice',
5358
'translation' => 'choice',
5459
'locale' => 'en',
5560
'domain' => 'messages',
5661
'state' => DataCollectorTranslator::MESSAGE_MISSING,
62+
'parameters' => array(),
63+
'transChoiceNumber' => 0,
5764
);
5865
$expectedMessages[] = array(
5966
'id' => 'bar_ru',
6067
'translation' => 'bar (ru)',
6168
'locale' => 'ru',
6269
'domain' => 'messages',
6370
'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
71+
'parameters' => array(),
72+
'transChoiceNumber' => null,
73+
);
74+
$expectedMessages[] = array(
75+
'id' => 'bar_ru',
76+
'translation' => 'bar (ru)',
77+
'locale' => 'ru',
78+
'domain' => 'messages',
79+
'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
80+
'parameters' => array('foo' => 'bar'),
81+
'transChoiceNumber' => null,
6482
);
6583

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

0 commit comments

Comments
 (0)