10000 [Translation] Fix constant domain resolution in PhpAstExtractor · symfony/symfony@c43f6c0 · GitHub
[go: up one dir, main page]

Skip to content

Commit c43f6c0

Browse files
VincentLangletnicolas-grekas
authored andcommitted
[Translation] Fix constant domain resolution in PhpAstExtractor
1 parent 0294563 commit c43f6c0

File tree

7 files changed

+39
-15
lines changed

7 files changed

+39
-15
lines changed

src/Symfony/Component/Translation/Extractor/PhpAstExtractor.php

+5
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ public function extract(iterable|string $resource, MessageCatalogue $catalogue):
4646
{
4747
foreach ($this->extractFiles($resource) as $file) {
4848
$traverser = new NodeTraverser();
49+
50+
// This is needed to resolve namespaces in class methods/constants.
51+
$nameResolver = new NodeVisitor\NameResolver();
52+
$traverser->addVisitor($nameResolver);
53+
4954
/** @var AbstractVisitor&NodeVisitor $visitor */
5055
foreach ($this->visitors as $visitor) {
5156
$visitor->initialize($catalogue, $file, $this->prefix);

src/Symfony/Component/Translation/Extractor/Visitor/AbstractVisitor.php

+11
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,17 @@ private function getStringValue(Node $node): ?string
119119
return $node->expr->value;
120120
}
121121

122+
if ($node instanceof Node\Expr\ClassConstFetch) {
123+
try {
124+
$reflection = new \ReflectionClass($node->class->toString());
125+
$constant = $reflection->getReflectionConstant($node->name->toString());
126+
if (false !== $constant && \is_string($constant->getValue())) {
127+
return $constant->getValue();
128+
}
129+
} catch (\ReflectionException) {
130+
}
131+
}
132+
122133
return null;
123134
}
124135
}

src/Symfony/Component/Translation/Extractor/Visitor/ConstraintVisitor.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public function beforeTraverse(array $nodes): ?Node
3232
}
3333

3434
public function enterNode(Node $node): ?Node
35+
{
36+
return null;
37+
}
38+
39+
public function leaveNode(Node $node): ?Node
3540
{
3641
if (!$node instanceof Node\Expr\New_ && !$node instanceof Node\Attribute) {
3742
return null;
@@ -100,11 +105,6 @@ public function enterNode(Node $node): ?Node
100105
return null;
101106
}
102107

103-
public function leaveNode(Node $node): ?Node
104-
{
105-
return null;
106-
}
107-
108108
public function afterTraverse(array $nodes): ?Node
109109
{
110110
return null;

src/Symfony/Component/Translation/Extractor/Visitor/TransMethodVisitor.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public function beforeTraverse(array $nodes): ?Node
2525
}
2626

2727
public function enterNode(Node $node): ?Node
28+
{
29+
return null;
30+
}
31+
32+
public function leaveNode(Node $node): ?Node
2833
{
2934
if (!$node instanceof Node\Expr\MethodCall && !$node instanceof Node\Expr\FuncCall) {
3035
return null;
@@ -53,11 +58,6 @@ public function enterNode(Node $node): ?Node
5358
return null;
5459
}
5560

56-
public function leaveNode(Node $node): ?Node
57-
{
58-
return null;
59-
}
60-
6161
public function afterTraverse(array $nodes): ?Node
6262
{
6363
return null;

src/Symfony/Component/Translation/Extractor/Visitor/TranslatableMessageVisitor.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public function beforeTraverse(array $nodes): ?Node
2525
}
2626

2727
public function enterNode(Node $node): ?Node
28+
{
29+
return null;
30+
}
31+
32+
public function leaveNode(Node $node): ?Node
2833
{
2934
if (!$node instanceof Node\Expr\New_) {
3035
return null;
@@ -53,11 +58,6 @@ public function enterNode(Node $node): ?Node
5358
return null;
5459
}
5560

56-
public function leaveNode(Node $node): ?Node
57-
{
58-
return null;
59-
}
60-
6161
public function afterTraverse(array $nodes): ?Node
6262
{
6363
return null;

src/Symfony/Component/Translation/Tests/Extractor/PhpAstExtractorTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
final class PhpAstExtractorTest extends TestCase
2222
{
23+
public const OTHER_DOMAIN = 'not_messages';
24+
2325
/**
2426
* @dataProvider resourcesProvider
2527
*/
@@ -124,6 +126,7 @@ public function testExtraction(iterable|string $resource)
124126
'variable-assignation-inlined-with-named-arguments-in-trans-method' => 'prefixvariable-assignation-inlined-with-named-arguments-in-trans-method',
125127
'mix-named-arguments-without-parameters' => 'prefixmix-named-arguments-without-parameters',
126128
'mix-named-arguments-disordered' => 'prefixmix-named-arguments-disordered',
129+
'const-domain' => 'prefixconst-domain',
127130
],
128131
'validators' => [
129132
'message-in-constraint-attribute' => 'prefixmessage-in-constraint-attribute',

src/Symfony/Component/Translation/Tests/Fixtures/extractor-ast/translation.html.php

+5
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,8 @@
6262
<?php echo $view['translator']->trans('mix-named-arguments-disordered', domain: 'not_messages', parameters: []); ?>
6363

6464
<?php echo $view['translator']->trans(...); // should not fail ?>
65+
66+
<?php
67+
use Symfony\Component\Translation\Tests\Extractor\PhpAstExtractorTest;
68+
echo $view['translator']->trans('const-domain', [], PhpAstExtractorTest::OTHER_DOMAIN);
69+
?>

0 commit comments

Comments
 (0)
0