8000 [Translation] Fix `TranslationNodeVisitor` with constant domain by VincentLanglet · Pull Request #53357 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Translation] Fix TranslationNodeVisitor with constant domain #53357

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 1 commit into from
Jan 23, 2024
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
[Translation] Fix TranslationNodeVisitor with constant domain
  • Loading branch information
VincentLanglet authored and nicolas-grekas committed Jan 23, 2024
commit d14795dbb12a3506f93b72e3ce32c6c439e6a0aa
16 changes: 16 additions & 0 deletions src/Symfony/Bridge/Twig/NodeVisitor/TranslationNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,22 @@ private function getReadDomainFromNode(Node $node): ?string
return $node->getAttribute('value');
}

if (
$node instanceof FunctionExpression
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the updated fixture I would have expected a FilterExpression here. But that makes me think that we probably need to provide the fix for the trans tag, t() function as well as the trans filter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added test and they are now supported 52b111a

&& 'constant' === $node->getAttribute('name')
) {
$nodeArguments = $node->getNode('arguments');
if ($nodeArguments->getIterator()->current() instanceof ConstantExpression) {
$constantName = $nodeArguments->getIterator()->current()->getAttribute('value');
if (\defined($constantName)) {
$value = \constant($constantName);
if (\is_string($value)) {
return $value;
}
}
}
}

return self::UNDEFINED_DOMAIN;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

class TwigExtractorTest extends TestCase
{
public const CUSTOM_DOMAIN = 'domain';

/**
* @dataProvider getExtractData
*/
Expand Down Expand Up @@ -77,6 +79,11 @@ public static function getExtractData()
// make sure this works with twig's named arguments
['{{ "new key" | trans(domain="domain") }}', ['new key' => 'domain']],

// make sure this works with const domain
['{{ "new key" | trans({}, constant(\'Symfony\\\\Bridge\\\\Twig\\\\Tests\\\\Translation\\\\TwigExtractorTest::CUSTOM_DOMAIN\')) }}', ['new key' => self::CUSTOM_DOMAIN]],
['{% trans from constant(\'Symfony\\\\Bridge\\\\Twig\\\\Tests\\\\Translation\\\\TwigExtractorTest::CUSTOM_DOMAIN\') %}new key{% endtrans %}', ['new key' => self::CUSTOM_DOMAIN]],
['{{ t("new key", {}, constant(\'Symfony\\\\Bridge\\\\Twig\\\\Tests\\\\Translation\\\\TwigExtractorTest::CUSTOM_DOMAIN\')) | trans() }}', ['new key' => self::CUSTOM_DOMAIN]],

// concat translations
['{{ ("new" ~ " key") | trans() }}', ['new key' => 'messages']],
['{{ ("another " ~ "new " ~ "key") | trans() }}', ['another new key' => 'messages']],
Expand Down
0