Skip to content

Commit de3b1f7

Browse files
committed
Add choice_translation_domain handling
+ Fix test # Conflicts: # Tests/Translation/Extractor/File/Fixture/MyFormType.php # Tests/Translation/Extractor/FileExtractorTest.php # Translation/Extractor/File/FormExtractor.php
1 parent 0510564 commit de3b1f7

File tree

3 files changed

+48
-11
lines changed

3 files changed

+48
-11
lines changed

Tests/Translation/Extractor/File/Fixture/MyFormType.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,19 @@ public function buildForm(FormBuilder $builder, array $options)
7979
'empty_value' => array('year' => 'form.dueDate.empty.year', 'month' => 'form.dueDate.empty.month', 'day'=>'form.dueDate.empty.day')
8080
));
8181

82-
$builder->add('choices_with_translation_domain', 'choice', array(
83-
'choices' => array('form.choice_translation_domain.label' => 'form.choice_translation_domain.value'),
84-
'choice_translation_domain' => 'choice-domain'
85-
));
82+
$builder
83+
->add('choices_with_translation_domain', 'choice', array(
84+
'choices' => array('form.choices_with_translation_domain.label' => 'form.choices_with_translation_domain.value'),
85+
'choice_translation_domain' => 'choice-domain'
86+
))
87+
->add('choices_without_translation', 'choice', array(
88+
'choices' => array('form.choices_without_translation.label' => 'form.choices_without_translation.value'),
89+
'choice_translation_domain' => false,
90+
))
91+
->add('untranslatable_label', 'text', array(
92+
'label' => 'form.untranslatable_label.label',
93+
'translation_domain' => false,
94+
))
95+
;
8696
}
8797
}

Tests/Translation/Extractor/File/FormExtractorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ public function testExtract()
156156
$message->addSource($fileSourceFactory->create($fixtureSplInfo, 69));
157157
$expected->add($message);
158158

159-
$message = new Message('form.choice_translation_domain.label', 'choice-domain');
160-
$message->addSource($fileSourceFactory->create($fixtureSplInfo, 83));
159+
$message = new Message('form.choices_with_translation_domain.label', 'choice-domain');
160+
$message->addSource($fileSourceFactory->create($fixtureSplInfo, 84));
161161
$expected->add($message);
162162

163163
$this->assertEquals($expected, $this->extract('MyFormType.php'));

Translation/Extractor/File/FormExtractor.php

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class FormExtractor implements FileVisitorInterface, LoggerAwareInterface, NodeV
4141
* @var FileSourceFactory
4242
*/
4343
private $fileSourceFactory;
44-
44+
4545
/**
4646
* @var DocParser
4747
*/
@@ -116,6 +116,10 @@ public function enterNode(Node $node)
116116
if ($node instanceof Node\Expr\Array_) {
117117
// first check if a translation_domain is set for this field
118118
$domain = $this->getDomain($node);
119+
$choiceDomain = $this->getChoiceDomain($node);
120+
if ($choiceDomain === null) {
121+
$choiceDomain = $domain;
122+
}
119123

120124
// look for options containing a message
121125
foreach ($node->items as $item) {
@@ -138,7 +142,7 @@ public function enterNode(Node $node)
138142
$this->parseItem($item, $domain);
139143
break;
140144
case 'choices':
141-
if ($this->parseChoiceNode($item, $node, $domain)) {
145+
if ($this->parseChoiceNode($item, $node, $choiceDomain)) {
142146
continue 2;
143147
}
144148
$this->parseItem($item, $domain);
@@ -159,6 +163,20 @@ public function enterNode(Node $node)
159163
* @return null|string
160164
*/
161165
public function getDomain(Node $node)
166+
{
167+
return $this->getDomainValueForKey('translation_domain', $node);
168+
}
169+
170+
/**
171+
* @param Node $node
172+
* @return null|string
173+
*/
174+
public function getChoiceDomain(Node $node)
175+
{
176+
return $this->getDomainValueForKey('choice_translation_domain', $node);
177+
}
178+
179+
private function getDomainValueForKey($key, Node $node)
162180
{
163181
$domain = null;
164182

@@ -167,7 +185,11 @@ public function getDomain(Node $node)
167185
continue;
168186
}
169187

170-
if ('translation_domain' === $item->key->value) {
188+
if ($key === $item->key->value) {
189+
if ($item->value instanceof Node\Expr\ConstFetch && $item->value->name->parts[0] === 'false') {
190+
$domain = false;
191+
break;
192+
}
171193
if (!$item->value instanceof Node\Scalar\String_) {
172194
continue;
173195
}
@@ -210,7 +232,7 @@ protected function parseEmptyValueNode(Node $item, $domain)
210232
}
211233

212234
/**
213-
* This parses any Node of type choices.
235+
* This parses any Node of type choices.
214236
*
215237
* Returning true means either that regardless of whether
216238
* parsing has occurred or not, the enterNode function should move on to the next node item.
@@ -252,7 +274,7 @@ protected function parseChoiceNode(Node $item, Node $node, $domain)
252274
}
253275

254276
/**
255-
* This parses any Node of type attr
277+
* This parses any Node of type attr
256278
*
257279
* Returning true means either that regardless of whether
258280
* parsing has occurred or not, the enterNode function should move on to the next node item.
@@ -385,6 +407,11 @@ private function parseItem($item, $domain = null)
385407
throw new RuntimeException($message);
386408
}
387409

410+
if ($domain === false) {
411+
// Don't translate when domain is `false`
412+
return;
413+
}
414+
388415
$source = $this->fileSourceFactory->create($this->file, $item->value->getLine());
389416
$id = $item->value->value;
390417

0 commit comments

Comments
 (0)