Skip to content

Commit

Permalink
Add choice_translation_domain handling
Browse files Browse the repository at this point in the history
+ Fix test

# Conflicts:
#	Tests/Translation/Extractor/File/Fixture/MyFormType.php
#	Tests/Translation/Extractor/FileExtractorTest.php
#	Translation/Extractor/File/FormExtractor.php
  • Loading branch information
artursvonda committed Dec 5, 2016
1 parent 0510564 commit de3b1f7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
18 changes: 14 additions & 4 deletions Tests/Translation/Extractor/File/Fixture/MyFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
))
;
}
}
4 changes: 2 additions & 2 deletions Tests/Translation/Extractor/File/FormExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
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 @@ -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) {
Expand All @@ -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);
Expand All @@ -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;

Expand All @@ -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;
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit de3b1f7

Please sign in to comment.