Skip to content

[TwigBridge] export concatenated translations #39352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/Symfony/Bridge/Twig/NodeVisitor/TranslationNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Bridge\Twig\Node\TransNode;
use Twig\Environment;
use Twig\Node\Expression\Binary\ConcatBinary;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\FilterExpression;
use Twig\Node\Expression\FunctionExpression;
Expand Down Expand Up @@ -87,6 +88,16 @@ protected function doEnterNode(Node $node, Environment $env): Node
$node->getNode('body')->getAttribute('data'),
$node->hasNode('domain') ? $this->getReadDomainFromNode($node->getNode('domain')) : null,
];
} elseif (
$node instanceof FilterExpression &&
'trans' === $node->getNode('filter')->getAttribute('value') &&
$node->getNode('node') instanceof ConcatBinary &&
$message = $this->getConcatValueFromNode($node->getNode('node'), null)
) {
$this->messages[] = [
$message,
$this->getReadDomainFromArguments($node->getNode('arguments'), 1),
];
}

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

return self::UNDEFINED_DOMAIN;
}

private function getConcatValueFromNode(Node $node, ?string $value): ?string
{
if ($node instanceof ConcatBinary) {
foreach ($node as $nextNode) {
if ($nextNode instanceof ConcatBinary) {
$nextValue = $this->getConcatValueFromNode($nextNode, $value);
if (null === $nextValue) {
return null;
}
$value .= $nextValue;
} elseif ($nextNode instanceof ConstantExpression) {
$value .= $nextNode->getAttribute('value');
} else {
// this is a node we cannot process (variable, or translation in translation)
return null;
}
}
} elseif ($node instanceof ConstantExpression) {
$value .= $node->getAttribute('value');
}

return $value;
}
}
13 changes: 13 additions & 0 deletions src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public function testExtract($template, $messages)
$m->setAccessible(true);
$m->invoke($extractor, $template, $catalogue);

if (0 === \count($messages)) {
$this->assertSame($catalogue->all(), $messages);
}

foreach ($messages as $key => $domain) {
$this->assertTrue($catalogue->has($key, $domain));
$this->assertEquals('prefix'.$key, $catalogue->get($key, $domain));
Expand Down Expand Up @@ -70,6 +74,15 @@ public function getExtractData()

// make sure this works with twig's named arguments
['{{ "new key" | trans(domain="domain") }}', ['new key' => 'domain']],

// concat translations
['{{ ("new" ~ " key") | trans() }}', ['new key' => 'messages']],
['{{ ("another " ~ "new " ~ "key") | trans() }}', ['another new key' => 'messages']],
['{{ ("new" ~ " key") | trans(domain="domain") }}', ['new key' => 'domain']],
['{{ ("another " ~ "new " ~ "key") | trans(domain="domain") }}', ['another new key' => 'domain']],
// if it has a variable or other expression, we can not extract it
['{% set foo = "new" %} {{ ("new " ~ foo ~ "key") | trans() }}', []],
['{{ ("foo " ~ "new"|trans ~ "key") | trans() }}', ['new' => 'messages']],
];
}

Expand Down