Forcing the Twig Locale

后端 未结 4 1997
一向
一向 2021-02-03 21:31

I would like to use the Twig template system to template my e-mails. The locale of the e-mail should be based on a user setting, not from the session or request locale. How can

4条回答
  •  情歌与酒
    2021-02-03 22:04

    u can do this: send a paramater (e.g. locale) to template

    public function indexAction($name)
    {
        $message = \Swift_Message::newInstance()
            ->setSubject('Hello Email')
            ->setFrom('send@example.com')
            ->setTo('recipient@example.com')
            ->setBody(
                $this->renderView(
                    'HelloBundle:Hello:email.txt.twig',
                    array('name' => $name, 'locale' => 'nl_NL'),
                )
            )
        ;
        $this->get('mailer')->send($message);
    
        return $this->render(...);
    }
    

提交回复
热议问题