【问题标题】:How to set locale in command class symfony2.1如何在命令类 symfony2.1 中设置语言环境
【发布时间】:2013-12-04 09:48:55
【问题描述】:

我正在使用命令类来发送 cron 邮件。如何设置发送电子邮件的区域设置?

$this->getRequest()->setLocale('de'); 

就我而言,每个用户都有不同的语言环境。 如何在命令类中设置语言环境?

我尝试了$this->getRequest()->setLocale('de'); 并收到错误:

namespace IT\bBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;  
use Symfony\Component\Console\Input\InputArgument;  
use Symfony\Component\Console\Input\InputInterface;  
use Symfony\Component\Console\Input\InputOption;  
use Symfony\Component\Console\Output\OutputInterface;

class CronCommand extends ContainerAwareCommand {
    protected function configure()
    {
        $this
            ->setName('send:emails')
            ->setDescription('Envio programado de emails');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $message = \Swift_Message::newInstance()
            ->setSubject('bla bla')
            ->setFrom('x@x.com')
            ->setTo('x@gmail.com')
            ->setCharset('UTF-8')
            ->setContentType('text/html')       
            ->setBody($this->renderView('mainBundle:Email:default.html.twig'));

        $this->getContainer()->get('mailer')->send($message);
        $output->writeln('Enviado!');
    } }

【问题讨论】:

    标签: symfony-2.1


    【解决方案1】:

    我解决了这个问题,我使用了:

    $this->getContainer()->get('translator')->setLocale('de');
    

    在命令类中设置 cron 邮件翻译的语言环境。

    【讨论】:

    • 太好了,请问作者或其他人可以验证这个答案吗?
    【解决方案2】:

    当您有ContainerAwareCommand 时,您可以从配置文件中读取值并将其手动插入翻译器,如下所示:(在 SF 2.8 中测试)

    $locale = $this->getContainer()->getParameter('locale');
    $this->getContainer()->get('translator')->setLocale($locale);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-12
      • 2014-05-29
      • 2014-01-01
      • 2019-08-27
      • 2019-08-27
      • 2011-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多