使用 Symfony Messenger 异步发送电子邮件时如何翻译电子邮件? [英] How to translate emails when sending them asynchronously with Symfony Messenger?

查看:40
本文介绍了使用 Symfony Messenger 异步发送电子邮件时如何翻译电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 Symfony 邮件程序配置为使用 Messenger 发送电子邮件.

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?

在我的控制器中:

$mailer->send(
            $user->email,
            $this->translator->trans('mails.recover.subject'),
            'email/client/password-recovery.html.twig',
            compact('user', 'hash', 'target')
        );

模板:

{% 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

信使配置:

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

如果翻译正确,邮件的主题看起来更好,邮件正文没有

如果我删除该行

'Symfony\Component\Mailer\Messenger\SendEmailMessage':  async

在 messegner 配置中,翻译工作.

in messegner config, translation work.

推荐答案

您遇到的问题是 Translator 组件从 请求 中获取用户的区域设置,并且在按时间异步发送邮件时邮件实际上是发送的,请求很长时间完成并消失了,上下文是消息使用者之一,没有请求区域设置信息.

The problem you have is that the Translator component gets the user's locale form the request, and when sending your mails asynchronously by the time the mail is actually sent the request is long finished and gone, the context is one of the message consumer and there is no request locale information.

对此有两种解决方案:

例如像这样:

$mailer->send(
        $user->email,
        $this->translator->trans('mails.recover.subject'),
        'email/client/password-recovery.html.twig',
            [
                'user' => $user,
                'hash' => $hash,
                'target' => $target,
                'labels' => [
                  'header' => $this->translator
                                   ->trans('mails.recover.subject', [ 'name' => $user->getName()]),
                  'text1'  => $this->translator
                                    ->trans('mails.recover.text1', ['url', => $defaulUrl])
            ]
);

然后在您的模板中直接使用这些值:

And then in your template you use the values directly:

{% extends 'email/base.html.twig' %}

{% block content %}
    <h2>{{ texts.header }}</h2>
    <p style="margin: 25px 0;">{{ texts.text1 }}</p>
{% endblock %}

这将是我的首选方法,因为它使模板尽可能简单并且易于在不同的上下文中重用.模板本身不需要知道与其内容的实际呈现无关的任何内容.

This would be my preferred approach, since it makes the template as dumb as possible and easy to reuse in different contexts. The templates themselves do not need to know anything not pertaining the actual rendering of its content.

$mailer->send(
        $user->email,
        $this->translator->trans('mails.recover.subject'),
        'email/client/password-recovery.html.twig',
            [
                'user'   => $user,
                'hash'   => $hash,
                'target' => $target,
                'requestLocale' =>  $locale
                // get the locale from the request
                // (https://symfony.com/doc/current/translation/locale.html)
            ]
);

然后在您使用的过滤器中使用接收到的语言环境,如此处:

Then you use the received locale in the filter you are using, as described here:

<h2>{{ 'mails.recover.header' | trans({'%name%': user.name}, 'app', requestLocale) }}</h2>


虽然我更喜欢第一个,但使用任一选项都可以让您获得所需的结果.


While I prefer the first one, playing with either option should let you get your desired results.

这篇关于使用 Symfony Messenger 异步发送电子邮件时如何翻译电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆