2013-11-20 1 views
2

Я использую класс команды для отправки почты cron. Как я могу настроить языковой стандарт для отправки электронной почты?Как установить locale в командный класс symfony2.1

$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('[email protected]') 
      ->setTo('[email protected]') 
      ->setCharset('UTF-8') 
      ->setContentType('text/html')  
      ->setBody($this->renderView('mainBundle:Email:default.html.twig')); 

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

ответ

7

Я решил эту проблему, я использовал:

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

Для установки локали в командной классе для хрон почты перевода.

+0

Великий решение. Спасибо! –

+1

Отлично, может ли автор или кто-то подтвердить этот ответ, пожалуйста? –

1

Если у вас есть ContainerAwareCommand, вы можете прочитать значение из файла конфигурации и вставить его в переводчику вручную, как это: (Испытано в SF 2.8)

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