Symfony2:如何翻译服务中的模板

时间:2013-10-10 17:45:24

标签: templates symfony dependency-injection translation twig

我创建了一个服务,我注入了翻译器&模板服务。 我用它来发送来自控制器的邮件

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

我认为服务中的模板不使用调用控制器的请求区域设置,并使用默认值设置为所有应用程序。 如何设置区域设置以告知注入的模板服务使用它?

有我的代码:

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

感谢您的帮助,

2 个答案:

答案 0 :(得分:0)

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

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

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

    // Rest of your code

答案 1 :(得分:0)

好的,问题出在我的xliff文件中...... 我的服务没问题一切顺利。