From f3812674c2ac0dd9b2ec96b30f14b22c4a943576 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Niedzielski?= Date: Wed, 21 Aug 2024 13:05:01 +0200 Subject: [PATCH] Fix named parameter used for `trans` filter --- .../File/Fixture/simple_template.html.twig | 4 +++- .../Fixture/simple_template_sf5.html.twig | 2 ++ .../Extractor/File/TwigFileExtractorTest.php | 8 +++++++ .../Extractor/File/TwigFileExtractor.php | 21 ++++++++++++++++--- 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/Tests/Translation/Extractor/File/Fixture/simple_template.html.twig b/Tests/Translation/Extractor/File/Fixture/simple_template.html.twig index 2d56ebef..f17182bf 100644 --- a/Tests/Translation/Extractor/File/Fixture/simple_template.html.twig +++ b/Tests/Translation/Extractor/File/Fixture/simple_template.html.twig @@ -18,4 +18,6 @@ {{ "foo.bar4" | transchoice(5, {'%name%': 'Johannes'}, 'app') }} -{% trans %}text.default_domain{% endtrans %} \ No newline at end of file +{% trans %}text.default_domain{% endtrans %} + +{{ "foo.bar5"|trans(domain='app') }} diff --git a/Tests/Translation/Extractor/File/Fixture/simple_template_sf5.html.twig b/Tests/Translation/Extractor/File/Fixture/simple_template_sf5.html.twig index 09b09664..0a576dfe 100644 --- a/Tests/Translation/Extractor/File/Fixture/simple_template_sf5.html.twig +++ b/Tests/Translation/Extractor/File/Fixture/simple_template_sf5.html.twig @@ -17,3 +17,5 @@ {{ "foo.bar4" | trans({'%count%': 5, '%name%': 'Johannes'}, 'app') }} {% trans %}text.default_domain{% endtrans %} + +{{ "foo.bar5"|trans(domain='app') }} diff --git a/Tests/Translation/Extractor/File/TwigFileExtractorTest.php b/Tests/Translation/Extractor/File/TwigFileExtractorTest.php index 3818baa6..9299d230 100644 --- a/Tests/Translation/Extractor/File/TwigFileExtractorTest.php +++ b/Tests/Translation/Extractor/File/TwigFileExtractorTest.php @@ -99,6 +99,10 @@ public function testExtractSimpleTemplateInSF5() $message->addSource($fileSourceFactory->create($fixtureSplInfo, 19)); $expected->add($message); + $message = new Message('foo.bar5', 'app'); + $message->addSource($fileSourceFactory->create($fixtureSplInfo, 21)); + $expected->add($message); + $this->assertEquals($expected, $this->extract('simple_template_sf5.html.twig')); } @@ -162,6 +166,10 @@ public function testExtractSimpleTemplate() $message->addSource($fileSourceFactory->create($fixtureSplInfo, 21)); $expected->add($message); + $message = new Message('foo.bar5', 'app'); + $message->addSource($fileSourceFactory->create($fixtureSplInfo, 23)); + $expected->add($message); + $this->assertEquals($expected, $this->extract('simple_template.html.twig')); } diff --git a/Translation/Extractor/File/TwigFileExtractor.php b/Translation/Extractor/File/TwigFileExtractor.php index 7f21c8b7..1bff8f55 100644 --- a/Translation/Extractor/File/TwigFileExtractor.php +++ b/Translation/Extractor/File/TwigFileExtractor.php @@ -27,6 +27,7 @@ use JMS\TranslationBundle\Translation\FileSourceFactory; use Symfony\Bridge\Twig\Node\TransNode; use Twig\Environment; +use Twig\Node\Expression\AbstractExpression; use Twig\Node\Expression\ConstantExpression; use Twig\Node\Expression\FilterExpression; use Twig\Node\Node; @@ -97,11 +98,11 @@ protected function doEnterNode(Node $node, Environment $env) } $id = $idNode->getAttribute('value'); - $index = $name === 'trans' ? 1 : 2; $domain = 'messages'; $arguments = iterator_to_array($node->getNode('arguments')); - if (isset($arguments[$index])) { - $argument = $arguments[$index]; + + $argument = $this->findDomainArgument($arguments, $name); + if (null !== $argument) { if (! $argument instanceof ConstantExpression) { return $node; @@ -145,6 +146,20 @@ protected function doEnterNode(Node $node, Environment $env) return $node; } + private function findDomainArgument(array $arguments, string $name): ?AbstractExpression + { + if (isset($arguments['domain'])) { + return $arguments['domain']; + } + + $index = $name === 'trans' ? 1 : 2; + if (isset($arguments[$index])) { + return $arguments[$index]; + } + + return null; + } + /** * @return int */