8000 [Translation] Fix extracting of message from ->trans() method with named params by tugmaks · Pull Request #58409 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Translation] Fix extracting of message from ->trans() method with named params #58409

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
Fix extracting of message from ->trans() method with named params
  • Loading branch information
tugmaks committed Sep 27, 2024
commit 800829a28fbd1fa9522d38f2295b7cdaef3e8b0c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function leaveNode(Node $node): ?Node
if ('trans' === $name || 't' === $name) {
$firstNamedArgumentIndex = $this->nodeFirstNamedArgumentIndex($node);

if (!$messages = $this->getStringArguments($node, 0 < $firstNamedArgumentIndex ? 0 : 'message')) {
if (!$messages = $this->getStringArguments($node, 0 < $firstNamedArgumentIndex ? 0 : 'id')) {
Copy link
Member

Choose a reason for hiding this comment

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

Both the twig callables t and trans refer to the translator trans() method / t function, and all of them have '$message' as first attribute 🤷‍♂️

So i'm not sure to understand here (especially as you changed the MethodVisitor code and the test at the same time)

https://github.com/symfony/twig-bridge/blob/1dad48d7e1c120aada838d6d2ae57a4c5a87390f/Extension/TranslationExtension.php#L96

function t(string $message, array $parameters = [], ?string $domain = null): TranslatableMessage

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You can try to inject TranslatorInterface to your service, call ->trans()

->trans('foo', domain: 'domain') this is extractable

->trans(id: 'foo', domain: 'domain') this is NOT extractable

return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@

<?php echo $view['translator']->trans('default domain', [], null); ?>

<?php echo $view['translator']->trans(message: 'ordered-named-arguments-in-trans-method', parameters: [], domain: 'not_messages'); ?>
<?php echo $view['translator']->trans(domain: 'not_messages', message: 'disordered-named-arguments-in-trans-method', parameters: []); ?>
<?php echo $view['translator']->trans(id: 'ordered-named-arguments-in-trans-method', parameters: [], domain: 'not_messages'); ?>
<?php echo $view['translator']->trans(domain: 'not_messages', id: 'disordered-named-arguments-in-trans-method', parameters: []); ?>

<?php echo $view['translator']->trans($key = 'variable-assignation-inlined-in-trans-method-call1', $parameters = [], $domain = 'not_messages'); ?>
<?php echo $view['translator']->trans('variable-assignation-inlined-in-trans-method-call2', $parameters = [], $domain = 'not_messages'); ?>
<?php echo $view['translator']->trans('variable-assignation-inlined-in-trans-method-call3', [], $domain = 'not_messages'); ?>

<?php echo $view['translator']->trans(domain: $domain = 'not_messages', message: $key = 'variable-assignation-inlined-with-named-arguments-in-trans-method', parameters: $parameters = []); ?>
<?php echo $view['translator']->trans(domain: $domain = 'not_messages', id: $key = 'variable-assignation-inlined-with-named-arguments-in-trans-method', parameters: $parameters = []); ?>

<?php echo $view['translator']->trans('mix-named-arguments', parameters: ['foo' => 'bar']); ?>
<?php echo $view['translator']->trans('mix-named-arguments-locale', parameters: ['foo' => 'bar'], locale: 'de'); ?>
Expand Down
Loading
0