我创建了一个服务,在其中注入翻译和模板服务。我用它从控制器发送邮件。
一切正常,除了用于生成电子邮件正文的树枝模板未翻译。
我认为服务中的模板不使用调用控制器的请求语言环境,而是使用为所有应用程序设置的默认语言环境。如何设置语言环境来告诉注入的模板服务使用它?
有我的代码:
class Mailer
{
private $mailer;
private $templating;
private $siteUrl;
private $fromName;
private $fromAddress;
private $translator;
private $doctrine;
/**
* @param \Swift_Mailer $mailer
* @param EngineInterface $templating
* @param RegistryInterface $doctrine
* @param Translator $translator
* @param string $siteUrl
* @param string $fromName
* @param string $fromAddress
*/
public function __construct(RegistryInterface $doctrine, \Swift_Mailer $mailer, EngineInterface $templating, Translator $translator, $siteUrl = "", $fromName = "", $fromAddress = "")
{
$this->mailer = $mailer;
$this->templating = $templating;
$this->siteUrl = $siteUrl;
$this->fromName = $fromName;
$this->fromAddress = $fromAddress;
$this->doctrine = $doctrine;
$this->translator = $translator;
}
public function sendIndentConfirm(Indent $indent)
{
$customer = $indent->getCustomer();
$purchases = $indent->getPurchases();
$subject = $this->translator->trans("Confirmation of your order");
$customerBody = $this->templating->render('ZamaECommerceBundle:Mailer:indentCustomerConfirm.html.twig', array(
"indent" => $indent,
"subject" => $subject
));
$this->sendEmail(
$customerBody,
$subject,
array(
$customer->getUsername() => $customer->getFirstName() . " " . $customer->getLastName()
)
);
}