I configured the Symfony mailer to send emails with messenger.
https://symfony.com/doc/current/mailer.html#sending-messages-async
I have my emails in two languages and I rely on the requests to detect the language, but now the emails are not translated.
How can I get the messages to be translated in the language detected in the request?
In my controller:
$mailer->send(
$user->email,
$this->translator->trans('mails.recover.subject'),
'email/client/password-recovery.html.twig',
compact('user', 'hash', 'target')
);
Template:
{% extends 'email/base.html.twig' %}
{% block content %}
<h2>{{ 'mails.recover.header' | trans({'%name%': user.name}) }}</h2>
<p style="margin: 25px 0;">
{{ 'mails.recover.text1' | trans({'%url%': url('default')}) | raw }}
</p>
// More code
Messenger config:
framework:
messenger:
# Uncomment this (and the failed transport below) to send failed messages to this transport for later handling.
# failure_transport: failed
transports:
# https://symfony.com/doc/current/messenger.html#transport-configuration
async: '%env(MESSENGER_TRANSPORT_DSN)%'
# failed: 'doctrine://default?queue_name=failed'
# sync: 'sync://'
routing:
# Route your messages to the transports
# 'App\Message\YourMessage': async
'Symfony\Component\Mailer\Messenger\SendEmailMessage': async
Looking better the subject of the mail if it is translated correctly, the body of the mail does not
If I remove the line
'Symfony\Component\Mailer\Messenger\SendEmailMessage': async
in messegner config, translation work.