diff --git a/Tests/Translation/Extractor/File/Fixture/MyFormType.php b/Tests/Translation/Extractor/File/Fixture/MyFormType.php index e3d08cf0..5542c2a8 100644 --- a/Tests/Translation/Extractor/File/Fixture/MyFormType.php +++ b/Tests/Translation/Extractor/File/Fixture/MyFormType.php @@ -79,9 +79,19 @@ public function buildForm(FormBuilder $builder, array $options) 'empty_value' => array('year' => 'form.dueDate.empty.year', 'month' => 'form.dueDate.empty.month', 'day'=>'form.dueDate.empty.day') )); - $builder->add('choices_with_translation_domain', 'choice', array( - 'choices' => array('form.choice_translation_domain.label' => 'form.choice_translation_domain.value'), - 'choice_translation_domain' => 'choice-domain' - )); + $builder + ->add('choices_with_translation_domain', 'choice', array( + 'choices' => array('form.choices_with_translation_domain.label' => 'form.choices_with_translation_domain.value'), + 'choice_translation_domain' => 'choice-domain' + )) + ->add('choices_without_translation', 'choice', array( + 'choices' => array('form.choices_without_translation.label' => 'form.choices_without_translation.value'), + 'choice_translation_domain' => false, + )) + ->add('untranslatable_label', 'text', array( + 'label' => 'form.untranslatable_label.label', + 'translation_domain' => false, + )) + ; } } diff --git a/Tests/Translation/Extractor/File/FormExtractorTest.php b/Tests/Translation/Extractor/File/FormExtractorTest.php index 4379cae3..f611839b 100644 --- a/Tests/Translation/Extractor/File/FormExtractorTest.php +++ b/Tests/Translation/Extractor/File/FormExtractorTest.php @@ -156,8 +156,8 @@ public function testExtract() $message->addSource($fileSourceFactory->create($fixtureSplInfo, 69)); $expected->add($message); - $message = new Message('form.choice_translation_domain.label', 'choice-domain'); - $message->addSource($fileSourceFactory->create($fixtureSplInfo, 83)); + $message = new Message('form.choices_with_translation_domain.label', 'choice-domain'); + $message->addSource($fileSourceFactory->create($fixtureSplInfo, 84)); $expected->add($message); $this->assertEquals($expected, $this->extract('MyFormType.php')); diff --git a/Translation/Extractor/File/FormExtractor.php b/Translation/Extractor/File/FormExtractor.php index d8a7df83..ee1693d4 100644 --- a/Translation/Extractor/File/FormExtractor.php +++ b/Translation/Extractor/File/FormExtractor.php @@ -41,7 +41,7 @@ class FormExtractor implements FileVisitorInterface, LoggerAwareInterface, NodeV * @var FileSourceFactory */ private $fileSourceFactory; - + /** * @var DocParser */ @@ -116,6 +116,10 @@ public function enterNode(Node $node) if ($node instanceof Node\Expr\Array_) { // first check if a translation_domain is set for this field $domain = $this->getDomain($node); + $choiceDomain = $this->getChoiceDomain($node); + if ($choiceDomain === null) { + $choiceDomain = $domain; + } // look for options containing a message foreach ($node->items as $item) { @@ -138,7 +142,7 @@ public function enterNode(Node $node) $this->parseItem($item, $domain); break; case 'choices': - if ($this->parseChoiceNode($item, $node, $domain)) { + if ($this->parseChoiceNode($item, $node, $choiceDomain)) { continue 2; } $this->parseItem($item, $domain); @@ -159,6 +163,20 @@ public function enterNode(Node $node) * @return null|string */ public function getDomain(Node $node) + { + return $this->getDomainValueForKey('translation_domain', $node); + } + + /** + * @param Node $node + * @return null|string + */ + public function getChoiceDomain(Node $node) + { + return $this->getDomainValueForKey('choice_translation_domain', $node); + } + + private function getDomainValueForKey($key, Node $node) { $domain = null; @@ -167,7 +185,11 @@ public function getDomain(Node $node) continue; } - if ('translation_domain' === $item->key->value) { + if ($key === $item->key->value) { + if ($item->value instanceof Node\Expr\ConstFetch && $item->value->name->parts[0] === 'false') { + $domain = false; + break; + } if (!$item->value instanceof Node\Scalar\String_) { continue; } @@ -210,7 +232,7 @@ protected function parseEmptyValueNode(Node $item, $domain) } /** - * This parses any Node of type choices. + * This parses any Node of type choices. * * Returning true means either that regardless of whether * 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) } /** - * This parses any Node of type attr + * This parses any Node of type attr * * Returning true means either that regardless of whether * 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) throw new RuntimeException($message); } + if ($domain === false) { + // Don't translate when domain is `false` + return; + } + $source = $this->fileSourceFactory->create($this->file, $item->value->getLine()); $id = $item->value->value;