0

我创建了一个服务,在其中注入翻译和模板服务。我用它从控制器发送邮件。

一切正常,除了用于生成电子邮件正文的树枝模板未翻译。

我认为服务中的模板不使用调用控制器的请求语言环境,而是使用为所有应用程序设置的默认语言环境。如何设置语言环境来告诉注入的模板服务使用它?

有我的代码:

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()
        )
    );
}
4

2 回答 2

0

好的,问题出在我的 xliff 文件中……我的服务没有问题,一切正常。

于 2013-10-10T23:19:25.743 回答
0

在控制器中调用方法时,您可以将其$locale作为参数传递。sendIndentConfirm

public function sendIndentConfirm(Indent $indent, $locale)
{
    $this->translator->setLocale($locale);

    $customer = $indent->getCustomer();
    $purchases = $indent->getPurchases();

    // Rest of your code
于 2013-10-10T21:58:40.063 回答