Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c32e249

Browse files
bug #53357 [Translation] Fix TranslationNodeVisitor with constant domain (VincentLanglet)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [Translation] Fix `TranslationNodeVisitor` with constant domain | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Issues | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead --> | License | MIT Related to #53356 but fixing only the twig part. Commits ------- d14795d [Translation] Fix `TranslationNodeVisitor` with constant domain
2 parents 52b0fed + d14795d commit c32e249

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

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

+16
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,22 @@ 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+
$constantName = $nodeArguments->getIterator()->current()->getAttribute('value');
168+
if (\defined($constantName)) {
169+
$value = \constant($constantName);
170+
if (\is_string($value)) {
171+
return $value;
172+
}
173+
}
174+
}
175+
}
176+
161177
return self::UNDEFINED_DOMAIN;
162178
}
163179

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

+7
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,11 @@ 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+
['{% trans from constant(\'Symfony\\\\Bridge\\\\Twig\\\\Tests\\\\Translation\\\\TwigExtractorTest::CUSTOM_DOMAIN\') %}new key{% endtrans %}', ['new key' => self::CUSTOM_DOMAIN]],
85+
['{{ t("new key", {}, constant(\'Symfony\\\\Bridge\\\\Twig\\\\Tests\\\\Translation\\\\TwigExtractorTest::CUSTOM_DOMAIN\')) | trans() }}', ['new key' => self::CUSTOM_DOMAIN]],
86+
8087
// concat translations
8188
['{{ ("new" ~ " key") | trans() }}', ['new key' => 'messages']],
8289
['{{ ("another " ~ "new " ~ "key") | trans() }}', ['another new key' => 'messages']],

0 commit comments

Comments
 (0)