Skip to content

Commit b64f8f5

Browse files
authored
Respect translation_domain (#73)
* Make sure we respect choice_translation_domain * Make sure we respect translation_domain * cs * Bugfix * Call parent constructor * cs * Fixes
1 parent 5de7593 commit b64f8f5

File tree

8 files changed

+184
-6
lines changed

8 files changed

+184
-6
lines changed

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,3 @@
4141
}
4242
}
4343
}
44-

src/Visitor/Php/Symfony/FormTypeChoices.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public function enterNode(Node $node)
7171
return;
7272
}
7373

74+
$domain = null;
7475
foreach ($node->items as $item) {
7576
if (!$item->key instanceof Node\Scalar\String_) {
7677
continue;
@@ -82,14 +83,24 @@ public function enterNode(Node $node)
8283
continue;
8384
}
8485

86+
if ('choice_translation_domain' === $item->key->value) {
87+
if ($item->value instanceof Node\Scalar\String_) {
88+
$domain = $item->value->value;
89+
} elseif ($item->value instanceof Node\Expr\ConstFetch && 'false' === $item->value->name->toString()) {
90+
$domain = false;
91+
}
92+
93+
continue;
94+
}
95+
8596
if ('choices' !== $item->key->value) {
8697
continue;
8798
}
8899

89100
$choicesNodes[] = $item->value;
90101
}
91102

92-
if (0 === count($choicesNodes)) {
103+
if (0 === count($choicesNodes) || false === $domain) {
93104
return;
94105
}
95106

@@ -111,7 +122,7 @@ public function enterNode(Node $node)
111122
continue;
112123
}
113124

114-
$sl = new SourceLocation($labelNode->value, $this->getAbsoluteFilePath(), $choices->getAttribute('startLine'));
125+
$sl = new SourceLocation($labelNode->value, $this->getAbsoluteFilePath(), $choices->getAttribute('startLine'), ['domain' => $domain]);
115126
$this->collection->addLocation($sl);
116127
}
117128
}

src/Visitor/Php/Symfony/FormTypeLabelExplicit.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,21 @@ public function enterNode(Node $node)
3939
return;
4040
}
4141

42+
$labelNode = null;
43+
$domain = null;
4244
foreach ($node->items as $item) {
4345
if (!$item->key instanceof Node\Scalar\String_) {
4446
continue;
4547
}
4648

49+
if ('translation_domain' === $item->key->value) {
50+
if ($item->value instanceof Node\Scalar\String_) {
51+
$domain = $item->value->value;
52+
} elseif ($item->value instanceof Node\Expr\ConstFetch && 'false' === $item->value->name->toString()) {
53+
$domain = false;
54+
}
55+
}
56+
4757
if ('label' !== $item->key->value) {
4858
continue;
4959
}
@@ -66,7 +76,11 @@ public function enterNode(Node $node)
6676
continue;
6777
}
6878

69-
$this->addLocation($label, $node->getAttribute('startLine'), $item);
79+
$labelNode = $item;
80+
}
81+
82+
if ($labelNode && false !== $domain) {
83+
$this->addLocation($label, $node->getAttribute('startLine'), $item, ['domain' => $domain]);
7084
}
7185
}
7286

src/Visitor/Php/Symfony/FormTypeLabelImplicit.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public function enterNode(Node $node)
3030
}
3131
}
3232

33+
$domain = null;
3334
// use add() function and look at first argument and if that's a string
3435
if ($node instanceof Node\Expr\MethodCall
3536
&& ('add' === $node->name || 'create' === $node->name)
@@ -42,6 +43,14 @@ public function enterNode(Node $node)
4243
if (isset($item->key) && 'label' === $item->key->value) {
4344
$customLabel = true;
4445
}
46+
47+
if (isset($item->key) && 'translation_domain' === $item->key->value) {
48+
if ($item->value instanceof Node\Scalar\String_) {
49+
$domain = $item->value->value;
50+
} elseif ($item->value instanceof Node\Expr\ConstFetch && 'false' === $item->value->name->toString()) {
51+
$domain = false;
52+
}
53+
}
4554
}
4655
}
4756
// actually there's another case here.. if the 3rd argument is anything else, it could well be
@@ -50,10 +59,10 @@ public function enterNode(Node $node)
5059
}
5160

5261
// only if no custom label was found, proceed
53-
if (false === $customLabel) {
62+
if (false === $customLabel && false !== $domain) {
5463
$label = $node->args[0]->value->value;
5564
if (!empty($label)) {
56-
$this->addLocation($label, $node->getAttribute('startLine'), $node);
65+
$this->addLocation($label, $node->getAttribute('startLine'), $node, ['domain' => $domain]);
5766
}
5867
}
5968
}

tests/Functional/Visitor/Php/Symfony/FormTypeChoicesTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,20 @@ public function testPassedChoices()
7070
$this->assertEquals('label1', $collection->get(0)->getMessage());
7171
$this->assertEquals(9, $collection->get(0)->getLine());
7272
}
73+
74+
public function testChoiceTranslationDomain()
75+
{
76+
$collection = $this->getSourceLocations(new FormTypeChoices(), Resources\Php\Symfony\FormDomainChoiceType::class);
77+
78+
$messageA = $collection->get(0);
79+
$this->assertEquals('label1_a', $messageA->getMessage());
80+
$this->assertEquals('admin', $messageA->getContext()['domain']);
81+
82+
$messageB = $collection->get(2);
83+
$this->assertEquals('label1_b', $messageB->getMessage());
84+
$this->assertNull($messageB->getContext()['domain']);
85+
86+
// We should not have "test_c"
87+
$this->assertEquals(4, $collection->count(), 'We should ignore choices where "choice_translation_domain" is "false"');
88+
}
7389
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the PHP Translation package.
5+
*
6+
* (c) PHP Translation team <tobias.nyholm@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Translation\Extractor\Tests\Functional\Visitor\Php\Symfony;
13+
14+
use Translation\Extractor\Tests\Functional\Visitor\Php\BasePHPVisitorTest;
15+
use Translation\Extractor\Tests\Resources;
16+
use Translation\Extractor\Visitor\Php\Symfony\FormTypeChoices;
17+
use Translation\Extractor\Visitor\Php\Symfony\FormTypeEmptyValue;
18+
use Translation\Extractor\Visitor\Php\Symfony\FormTypeInvalidMessage;
19+
use Translation\Extractor\Visitor\Php\Symfony\FormTypeLabelExplicit;
20+
use Translation\Extractor\Visitor\Php\Symfony\FormTypeLabelImplicit;
21+
use Translation\Extractor\Visitor\Php\Symfony\FormTypePlaceholder;
22+
23+
/**
24+
* @author Rein Baarsma <rein@solidwebcode.com>
25+
*/
26+
class FormTypeLabelTest extends BasePHPVisitorTest
27+
{
28+
private $allFormVisitors;
29+
30+
public function __construct()
31+
{
32+
$this->allFormVisitors = [
33+
new FormTypeChoices(),
34+
new FormTypeEmptyValue(),
35+
new FormTypeInvalidMessage(),
36+
new FormTypeLabelExplicit(),
37+
new FormTypeLabelImplicit(),
38+
new FormTypePlaceholder(),
39+
];
40+
41+
parent::__construct();
42+
}
43+
44+
public function testTranslationDomain()
45+
{
46+
$collection = $this->getSourceLocations($this->allFormVisitors, Resources\Php\Symfony\FormDomainType::class);
47+
48+
$messageA = $collection->get(0);
49+
$this->assertEquals('label1', $messageA->getMessage());
50+
$this->assertEquals('admin0', $messageA->getContext()['domain']);
51+
52+
$messageB = $collection->get(1);
53+
$this->assertEquals('test_b', $messageB->getMessage());
54+
$this->assertEquals('admin1', $messageB->getContext()['domain']);
55+
56+
$messageC = $collection->get(2);
57+
$this->assertEquals('test_c', $messageC->getMessage());
58+
$this->assertNull($messageC->getContext()['domain']);
59+
60+
// We should not have "test_d" or "test_e"
61+
$this->assertEquals(3, $collection->count(), 'We should ignore choices where "translation_domain" is "false"');
62+
}
63+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Translation\Extractor\Tests\Resources\Php\Symfony;
4+
5+
class FormDomainChoiceType
6+
{
7+
public function buildForm(FormBuilderInterface $builder, array $options)
8+
{
9+
10+
$builder->add('test_a', null, [
11+
'choices' => [
12+
'label1_a' => 'key1',
13+
'label2_a' => 'key2',
14+
],
15+
'choice_translation_domain' => 'admin',
16+
]);
17+
18+
// Make sure this will have domain "messages"
19+
$builder->add('test_b', null, [
20+
'choices' => [
21+
'label1_b' => 'key1',
22+
'label2_b' => 'key2',
23+
],
24+
]);
25+
26+
$builder->add('test_C', null, [
27+
'choices' => [
28+
'label1_c' => 'key1',
29+
'label2_c' => 'key2',
30+
],
31+
'choice_translation_domain' => false,
32+
]);
33+
}
34+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Translation\Extractor\Tests\Resources\Php\Symfony;
4+
5+
class FormDomainType
6+
{
7+
public function buildForm(FormBuilderInterface $builder, array $options)
8+
{
9+
10+
$builder->add('test_a', null, [
11+
'label' => 'label1',
12+
'translation_domain' => 'admin0',
13+
]);
14+
15+
$builder->add('test_b', null, [
16+
'translation_domain' => 'admin1',
17+
]);
18+
19+
// Make sure this will have domain "messages"
20+
$builder->add('test_c', null, [
21+
]);
22+
23+
$builder->add('test_d', null, [
24+
'translation_domain' => false,
25+
]);
26+
27+
$builder->add('test_e', null, [
28+
'label' => 'label2',
29+
'translation_domain' => false,
30+
]);
31+
}
32+
}

0 commit comments

Comments
 (0)