Symfony:如何翻译 switfmailer 发送的电子邮件

Symfony : How to translate an email sent by switfmailer

我想我正在努力使用 SF4 来做一件简单的事情。

用户注册后,我尝试根据用户区域设置(法语或英语)发送电子邮件。

在订阅者中设置了 $subject, $body 文本,我正在接收电子邮件。

现在我想实现主题和正文的翻译。

这里肯定有一些我不能很好控制的事情,但我尝试了 SF 文档 here 中描述的内容,它在控制器内部工作,但在我的订阅者内部不工作。

我有一些我不理解的非常明显的错误:(

error capture

有什么建议可以指导我吗?

我知道我可以为正文渲染一个树枝模板,但不能为电子邮件的主题渲染。

这是我的订阅者代码:(我在测试时取消评论评论的内容)

namespace App\EventSubscriber;

use App\Entity\User;
use App\Events;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
//use Symfony\Component\Translation\TranslatorInterface;

class RegistrationNotifySubscriber implements EventSubscriberInterface
{
    private $mailer;
    private $sender;

    public function __construct(\Swift_Mailer $mailer, $sender)
    {
        $this->mailer = $mailer;
        $this->sender = $sender;
    }

    public static function getSubscribedEvents(): array
    {
        return [
            Events::USER_REGISTERED => 'onUserRegistrated',
        ];
    }

    public function onUserRegistrated(GenericEvent $event)
    {
        /** @var User $user */
        $user = $event->getSubject();

        //$subject = $translator->trans('registration.email.subject', array(), 'messages'); -- , TranslatorInterface $translator
        $subject = "Your account for a product demonstration";
        $body = "Hello, Your account has been created...";

        $message = (new \Swift_Message())
            ->setSubject($subject)
            ->setTo($user->getUsername())
            ->setFrom($this->sender)
            ->setBody($body, 'text/html')
        ;

        $this->mailer->send($message);
    }
}   

谢谢。

很棒

您需要将 Translator 注入您的订阅者。为此你需要

  1. 接受它作为订阅者构造函数中的参数
  2. 让 Symfony 知道它需要通过编辑 service.yml 文件
  3. 将它传递到那里
// # service.yml #
//AppBundle\EventSubscriber\RegistrationNotifySubscriber:
//        arguments: ['@translator', '@mailer']
//        public: true

use App\Entity\User;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\Translation\TranslatorInterface;

class RegistrationNotifySubscriber implements EventSubscriberInterface
{
    private $mailer;
    private $sender;
    private $translator;

    public function __construct(TranslatorInterface $translator, \Swift_Mailer $mailer, $sender)
    {
        $this->mailer = $mailer;
        $this->sender = $sender;
        $this->translator = $translator;
    }

    public static function getSubscribedEvents(): array
    {
        return [
            Events::USER_REGISTERED => 'onUserRegistrated',
        ];
    }

    public function onUserRegistrated(GenericEvent $event)
    {
        /** @var User $user */
        $user = $event->getSubject();

        $subject = $this->translator->trans('registration.email.subject', array(), 'messages');
        $subject = "Your account for a product demonstration";
        $body = "Hello, Your account has been created...";

        $message = (new \Swift_Message())
            ->setSubject($subject)
            ->setTo($user->getUsername())
            ->setFrom($this->sender)
            ->setBody($body, 'text/html')
        ;

        $this->mailer->send($message);
    }
}