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

Conversation

tugmaks
Copy link
Contributor

@tugmaks tugmaks commented Sep 27, 2024

Q A
Branch? 6.4
Bug fix? yes
New feature? No
Deprecations? No
Issues Will be explained in this PR
License MIT

Hi, this PR fixes a problem of an extracting translations from \Symfony\Contracts\Translation\TranslatorInterface::trans() method
As you can see the first argument is named as id, but not message, as expected in Visitor

@@ -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

@nicolas-grekas
Copy link
Member

Thank you @tugmaks.

@nicolas-grekas nicolas-grekas merged commit fc4825e into symfony:6.4 Sep 28, 2024
4 of 10 checks passed
This was referenced Oct 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants