Skip to content

Commit

Permalink
Fix named parameter used for trans filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Steveb-p committed Aug 22, 2024
1 parent 84224b6 commit f381267
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@

{{ "foo.bar4" | transchoice(5, {'%name%': 'Johannes'}, 'app') }}

{% trans %}text.default_domain{% endtrans %}
{% trans %}text.default_domain{% endtrans %}

{{ "foo.bar5"|trans(domain='app') }}
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@
{{ "foo.bar4" | trans({'%count%': 5, '%name%': 'Johannes'}, 'app') }}

{% trans %}text.default_domain{% endtrans %}

{{ "foo.bar5"|trans(domain='app') }}
8 changes: 8 additions & 0 deletions Tests/Translation/Extractor/File/TwigFileExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

Expand Down Expand Up @@ -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'));
}

Expand Down
21 changes: 18 additions & 3 deletions Translation/Extractor/File/TwigFileExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit f381267

Please sign in to comment.