Skip to content

Commit

Permalink
choice_translation_domain extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
artursvonda committed Oct 2, 2018
1 parent 942d501 commit c2e3854
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
15 changes: 15 additions & 0 deletions Tests/Translation/Extractor/File/Fixture/MyFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,20 @@ public function buildForm(FormBuilder $builder, array $options)
$builder->add('dueDate', 'date', array(
'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.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,
))
;
}
}
4 changes: 4 additions & 0 deletions Tests/Translation/Extractor/File/FormExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ public function testExtract()
$message->addSource($fileSourceFactory->create($fixtureSplInfo, 69));
$expected->add($message);

$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'));
}

Expand Down
37 changes: 32 additions & 5 deletions Translation/Extractor/File/FormExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class FormExtractor implements FileVisitorInterface, LoggerAwareInterface, NodeV
* @var FileSourceFactory
*/
private $fileSourceFactory;

/**
* @var DocParser
*/
Expand Down Expand Up @@ -117,6 +117,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) {
Expand All @@ -139,7 +143,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);
Expand All @@ -160,6 +164,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;

Expand All @@ -168,7 +186,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;
}
Expand Down Expand Up @@ -211,7 +233,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.
Expand Down Expand Up @@ -253,7 +275,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.
Expand Down Expand Up @@ -395,6 +417,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;

Expand Down

0 comments on commit c2e3854

Please sign in to comment.