8000 [TwigBridge] export concatenated translations · symfony/symfony@122eaba · GitHub
[go: up one dir, main page]

Skip to content

Commit 122eaba

Browse files
Stephenfabpot
Stephen
authored andcommitted
[TwigBridge] export concatenated translations
1 parent 8b8bffb commit 122eaba

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Bridge\Twig\Node\TransNode;
1515
use Twig\Environment;
16+
use Twig\Node\Expression\Binary\ConcatBinary;
1617
use Twig\Node\Expression\ConstantExpression;
1718
use Twig\Node\Expression\FilterExpression;
1819
use Twig\Node\Expression\FunctionExpression;
@@ -87,6 +88,16 @@ protected function doEnterNode(Node $node, Environment $env): Node
8788
$node->getNode('body')->getAttribute('data'),
8889
$node->hasNode('domain') ? $this->getReadDomainFromNode($node->getNode('domain')) : null,
8990
];
91+
} elseif (
92+
$node instanceof FilterExpression &&
93+
'trans' === $node->getNode('filter')->getAttribute('value') &&
94+
$node->getNode('node') instanceof ConcatBinary &&
95+
$message = $this->getConcatValueFromNode($node->getNode('node'), null)
96+
) {
97+
$this->messages[] = [
98+
$message,
99+
$this->getReadDomainFromArguments($node->getNode('arguments'), 1),
100+
];
90101
}
91102

92103
return $node;
@@ -151,4 +162,28 @@ private function getReadDomainFromNode(Node $node): ?string
151162

152163
return self::UNDEFINED_DOMAIN;
153164
}
165+
166+
private function getConcatValueFromNode(Node $node, ?string $value): ?string
167+
{
168+
if ($node instanceof ConcatBinary) {
169+
foreach ($node as $nextNode) {
170+
if ($nextNode instanceof ConcatBinary) {
171+
$nextValue = $this->getConcatValueFromNode($nextNode, $value);
172+
if (null === $nextValue) {
173+
return null;
174+
}
175+
$value .= $nextValue;
176+
} elseif ($nextNode instanceof ConstantExpression) {
177+
$value .= $nextNode->getAttribute('value');
178+
} else {
179+
// this is a node we cannot process (variable, or translation in translation)
180+
return null;
181+
}
182+
}
183+
} elseif ($node instanceof ConstantExpression) {
184+
$value .= $node->getAttribute('value');
185+
}
186+
187+
return $value;
188+
}
154189
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ public function testExtract($template, $messages)
4343
$m->setAccessible(true);
4444
$m->invoke($extractor, $template, $catalogue);
4545

46+
if (0 === \count($messages)) {
47+
$this->assertSame($catalogue->all(), $messages);
48+
}
49+
4650
foreach ($messages as $key => $domain) {
4751
$this->assertTrue($catalogue->has($key, $domain));
4852
$this->assertEquals('prefix'.$key, $catalogue->get($key, $domain));
@@ -70,6 +74,15 @@ public function getExtractData()
7074

7175
// make sure this works with twig's named arguments
7276
['{{ "new key" | trans(domain="domain") }}', ['new key' => 'domain']],
77+
78+
// concat translations
79+
['{{ ("new" ~ " key") | trans() }}', ['new key' => 'messages']],
80+
['{{ ("another " ~ "new " ~ "key") | trans() }}', ['another new key' => 'messages']],
81+
['{{ ("new" ~ " key") | trans(domain="domain") }}', ['new key' => 'domain']],
82+
['{{ ("another " ~ "new " ~ "key") | trans(domain="domain") }}', ['another new key' => 'domain']],
83+
// if it has a variable or other expression, we can not extract it
84+
['{% set foo = "new" %} {{ ("new " ~ foo ~ "key") | trans() }}', []],
85+
['{{ ("foo " ~ "new"|trans ~ "key") | trans() }}', ['new' => 'messages']],
7386
];
7487
}
7588

0 commit comments

Comments
 (0)
0