Skip to content

Commit 88aa521

Browse files
authored
Make placeholder respect translation_domain (#99)
* Make placeholder respect translation_domain This will fix #96 * cs
1 parent fe9fab9 commit 88aa521

File tree

3 files changed

+90
-20
lines changed

3 files changed

+90
-20
lines changed

src/Visitor/Php/Symfony/FormTypePlaceholder.php

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,53 +13,74 @@
1313

1414
use PhpParser\Node;
1515
use PhpParser\NodeVisitor;
16-
use Translation\Extractor\Visitor\Php\BasePHPVisitor;
1716

1817
/**
1918
* @author Tobias Nyholm <[email protected]>
2019
*/
21-
final class FormTypePlaceholder extends BasePHPVisitor implements NodeVisitor
20+
final class FormTypePlaceholder extends AbstractFormType implements NodeVisitor
2221
{
2322
use FormTrait;
2423

24+
private $arrayNodeVisited = [];
25+
2526
public function enterNode(Node $node)
2627
{
2728
if (!$this->isFormType($node)) {
2829
return;
2930
}
31+
parent::enterNode($node);
3032

3133
if (!$node instanceof Node\Expr\Array_) {
3234
return;
3335
}
3436

37+
$placeholderNode = null;
38+
$domain = null;
3539
foreach ($node->items as $item) {
3640
if (!$item->key instanceof Node\Scalar\String_) {
3741
continue;
3842
}
39-
40-
if ('placeholder' === $item->key->value) {
43+
if ('translation_domain' === $item->key->value) {
44+
// Try to find translation domain
4145
if ($item->value instanceof Node\Scalar\String_) {
42-
$line = $item->value->getAttribute('startLine');
43-
$this->addLocation($item->value->value, $line, $item);
46+
$domain = $item->value->value;
4447
} elseif ($item->value instanceof Node\Expr\ConstFetch && 'false' === $item->value->name->toString()) {
45-
// 'placeholder' => false,
46-
// Do noting
47-
} else {
48-
$this->addError($item, 'Form placeholder is not a scalar string');
48+
$domain = false;
49+
}
50+
} elseif ('placeholder' === $item->key->value) {
51+
$placeholderNode = $item;
52+
} elseif ('attr' === $item->key->value) {
53+
foreach ($item->value->items as $attrValue) {
54+
if ('placeholder' === $attrValue->key->value) {
55+
$placeholderNode = $attrValue;
56+
57+
break;
58+
}
4959
}
5060
}
5161
}
52-
}
53-
54-
public function leaveNode(Node $node)
55-
{
56-
}
5762

58-
public function beforeTraverse(array $nodes)
59-
{
60-
}
63+
if (null !== $placeholderNode) {
64+
/**
65+
* Make sure we do not visit the same placeholder node twice.
66+
*/
67+
$hash = spl_object_hash($placeholderNode);
68+
if (isset($this->arrayNodeVisited[$hash])) {
69+
return;
70+
}
71+
$this->arrayNodeVisited[$hash] = true;
6172

62-
public function afterTraverse(array $nodes)
63-
{
73+
if ($placeholderNode->value instanceof Node\Scalar\String_) {
74+
$line = $placeholderNode->value->getAttribute('startLine');
75+
if (null !== $location = $this->getLocation($placeholderNode->value->value, $line, $placeholderNode, ['domain' => $domain])) {
76+
$this->lateCollect($location);
77+
}
78+
} elseif ($placeholderNode->value instanceof Node\Expr\ConstFetch && 'false' === $placeholderNode->value->name->toString()) {
79+
// 'placeholder' => false,
80+
// Do noting
81+
} else {
82+
$this->addError($placeholderNode, 'Form placeholder is not a scalar string');
83+
}
84+
}
6485
}
6586
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Translation\Extractor\Tests\Resources\Php\Symfony;
4+
5+
class GlobalTranslationDomainWithPlaceholderType
6+
{
7+
public function buildForm(FormBuilderInterface $builder, array $options)
8+
{
9+
$builder
10+
->add('username', null, [
11+
'attr' => array(
12+
'placeholder' => 'github.issue_96a.placeholder',
13+
),
14+
'label' => 'github.issue_96a.label'
15+
])
16+
->add('password', null, [
17+
'attr' => array(
18+
'placeholder' => 'github.issue_96b.placeholder',
19+
),
20+
'label' => 'github.issue_96b.label',
21+
'translation_domain' => 'foobar'
22+
])
23+
->add('age', null, [
24+
'placeholder' => 'github.issue_96c.placeholder',
25+
'label' => 'github.issue_96c.label',
26+
'translation_domain' => 'foobar'
27+
])
28+
->add('location', null, [
29+
'placeholder' => 'github.issue_96d.placeholder',
30+
'label' => 'github.issue_96d.label',
31+
]);
32+
}
33+
34+
public function configureOptions(OptionsResolver $resolver)
35+
{
36+
$resolver->setDefaults([
37+
'translation_domain' => 'custom',
38+
]);
39+
}
40+
}

tests/Smoke/AllExtractorsTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ public function testNoException()
6060
$source = $this->translationExists($sc, 'github.issue_82c');
6161
$this->assertEquals('custom', $source->getContext()['domain']);
6262

63+
$source = $this->translationExists($sc, 'github.issue_96a.placeholder');
64+
$this->assertEquals('custom', $source->getContext()['domain']);
65+
$source = $this->translationExists($sc, 'github.issue_96b.placeholder');
66+
$this->assertEquals('foobar', $source->getContext()['domain']);
67+
$source = $this->translationExists($sc, 'github.issue_96c.placeholder');
68+
$this->assertEquals('foobar', $source->getContext()['domain']);
69+
$source = $this->translationExists($sc, 'github.issue_96d.placeholder');
70+
$this->assertEquals('custom', $source->getContext()['domain']);
71+
6372
/*
6473
* It is okey to increase the error count if you adding more fixtures/code.
6574
* We just need to be aware that it changes.

0 commit comments

Comments
 (0)