8000 Fix TranslationNodeVisitor with constant domain · symfony/symfony@9861bc4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9861bc4

Browse files
Fix TranslationNodeVisitor with constant domain
1 parent bf62cbe commit 9861bc4

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/Symfony/Bridge/Twig/NodeVisitor/TranslationNodeVisitor.php

+13
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,19 @@ private function getReadDomainFromNode(Node $node): ?string
158158
return $node->getAttribute('value');
159159
}
160160

161+
if (
162+
$node instanceof FunctionExpression
163+
&& 'constant' === $node->getAttribute('name')
164+
) {
165+
$nodeArguments = $node->getNode('arguments');
166+
if ($nodeArguments->getIterator()->current() instanceof ConstantExpression) {
167+
$value = \constant($nodeArguments->getIterator()->current()->getAttribute('value'));
168+
if (\is_string($value)) {
169+
return $value;
170+
}
171+
}
172+
}
173+
161174
return self::UNDEFINED_DOMAIN;
162175
}
163176

src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
class TwigExtractorTest extends TestCase
2424
{
25+
public const CUSTOM_DOMAIN = 'domain';
26+
2527
/**
2628
* @dataProvider getExtractData
2729
*/
@@ -77,6 +79,9 @@ public static function getExtractData()
7779
// make sure this works with twig's named arguments
7880
['{{ "new key" | trans(domain="domain") }}', ['new key' => 'domain']],
7981

82+
// make sure this works with const domain
83+
['{{ "new key" | trans({}, constant(\'Symfony\\\\Bridge\\\\Twig\\\\Tests\\\\Translation\\\\TwigExtractorTest::CUSTOM_DOMAIN\')) }}', ['new key' => self::CUSTOM_DOMAIN]],
84+
8085
// concat translations
8186
['{{ ("new" ~ " key") | trans() }}', ['new key' => 'messages']],
8287
['{{ ("another " ~ "new " ~ "key") | trans() }}', ['another new key' => 'messages']],

0 commit comments

Comments
 (0)
0