Skip to content

Commit

Permalink
Merge pull request #588 from Steveb-p/fix-named-parameter-filter
Browse files Browse the repository at this point in the history
Fix named parameter used for `trans` filter
  • Loading branch information
goetas committed Aug 22, 2024
2 parents 42655d8 + f381267 commit d14af48
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 6 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:
push:
branches:
- "master"
- "1.x"

jobs:
phpunit:
Expand All @@ -29,7 +30,7 @@ jobs:
- "highest"
steps:
- name: "Checkout"
uses: "actions/checkout@v2"
uses: "actions/checkout@v4"
with:
fetch-depth: 2

Expand All @@ -47,7 +48,7 @@ jobs:
args: extra.symfony.require ${{ matrix.symfony-version }}

- name: "Install dependencies with Composer"
uses: "ramsey/composer-install@v1"
uses: "ramsey/composer-install@v3"
with:
dependency-versions: "${{ matrix.dependencies }}"
composer-options: "${{ matrix.composer-options }}"
Expand Down
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
5 changes: 5 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
</testsuite>
</testsuites>

<php>
<env name="SYMFONY_PHPUNIT_REQUIRE" value="nikic/php-parser:^4.9" />
<env name="SYMFONY_PHPUNIT_VERSION" value="9.5" />
</php>

<filter>
<whitelist>
<directory>./</directory>
Expand Down

0 comments on commit d14af48

Please sign in to comment.