Skip to content

Commit afa4f86

Browse files
committed
choice_translation_domain extractor
1 parent 2c17672 commit afa4f86

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,20 @@ public function buildForm(FormBuilder $builder, array $options)
7878
$builder->add('dueDate', 'date', array(
7979
'empty_value' => array('year' => 'form.dueDate.empty.year', 'month' => 'form.dueDate.empty.month', 'day'=>'form.dueDate.empty.day')
8080
));
81+
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+
;
8196
}
8297
}

Tests/Translation/Extractor/File/FormExtractorTest.php

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

159+
$message = new Message('form.choices_with_translation_domain.label', 'choice-domain');
160+
$message->addSource($fileSourceFactory->create($fixtureSplInfo, 84));
161+
$expected->add($message);
162+
159163
$this->assertEquals($expected, $this->extract('MyFormType.php'));
160164
}
161165

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
*/
@@ -117,6 +117,10 @@ public function enterNode(Node $node)
117117
if ($node instanceof Node\Expr\Array_) {
118118
// first check if a translation_domain is set for this field
119119
$domain = $this->getDomain($node);
120+
$choiceDomain = $this->getChoiceDomain($node);
121+
if ($choiceDomain === null) {
122+
$choiceDomain = $domain;
123+
}
120124

121125
// look for options containing a message
122126
foreach ($node->items as $item) {
@@ -139,7 +143,7 @@ public function enterNode(Node $node)
139143
$this->parseItem($item, $domain);
140144
break;
141145
case 'choices':
142-
if ($this->parseChoiceNode($item, $node, $domain)) {
146+
if ($this->parseChoiceNode($item, $node, $choiceDomain)) {
143147
continue 2;
144148
}
145149
$this->parseItem($item, $domain);
@@ -160,6 +164,20 @@ public function enterNode(Node $node)
160164
* @return null|string
161165
*/
162166
public function getDomain(Node $node)
167+
{
168+
return $this->getDomainValueForKey('translation_domain', $node);
169+
}
170+
171+
/**
172+
* @param Node $node
173+
* @return null|string
174+
*/
175+
public function getChoiceDomain(Node $node)
176+
{
177+
return $this->getDomainValueForKey('choice_translation_domain', $node);
178+
}
179+
180+
private function getDomainValueForKey($key, Node $node)
163181
{
164182
$domain = null;
165183

@@ -168,7 +186,11 @@ public function getDomain(Node $node)
168186
continue;
169187
}
170188

171-
if ('translation_domain' === $item->key->value) {
189+
if ($key === $item->key->value) {
190+
if ($item->value instanceof Node\Expr\ConstFetch && $item->value->name->parts[0] === 'false') {
191+
$domain = false;
192+
break;
193+
}
172194
if (!$item->value instanceof Node\Scalar\String_) {
173195
continue;
174196
}
@@ -211,7 +233,7 @@ protected function parseEmptyValueNode(Node $item, $domain)
211233
}
212234

213235
/**
214-
* This parses any Node of type choices.
236+
* This parses any Node of type choices.
215237
*
216238
* Returning true means either that regardless of whether
217239
* parsing has occurred or not, the enterNode function should move on to the next node item.
@@ -253,7 +275,7 @@ protected function parseChoiceNode(Node $item, Node $node, $domain)
253275
}
254276

255277
/**
256-
* This parses any Node of type attr
278+
* This parses any Node of type attr
257279
*
258280
* Returning true means either that regardless of whether
259281
* parsing has occurred or not, the enterNode function should move on to the next node item.
@@ -395,6 +417,11 @@ private function parseItem($item, $domain = null)
395417
throw new RuntimeException($message);
396418
}
397419

420+
if ($domain === false) {
421+
// Don't translate when domain is `false`
422+
return;
423+
}
424+
398425
$source = $this->fileSourceFactory->create($this->file, $item->value->getLine());
399426
$id = $item->value->value;
400427

0 commit comments

Comments
 (0)