2013-11-20 2 views
2

나는 cron 메일을 보내기 위해 명령 클래스를 사용하고 있습니다. 어떻게 이메일을 보낼 로케일을 설정할 수 있습니까?명령 클래스 symfony2.1에서 로케일을 설정하는 방법

$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'); 

하는 크론 메일 번역 명령 클래스의 로케일을 설정하려면

나는 $this->getRequest()->setLocale('de');하고 오류가 발생을했습니다.

+0

훌륭한 솔루션입니다. 고마워! –

+1

우수, 저자 또는 누군가가이 답변을 확인해 주시겠습니까? –

1

당신이 ContainerAwareCommand있을 때, 당신은 당신의 config 파일에서 값을 읽고 수동으로이 같은 번역기에 삽입 할 수 있습니다 (SF 2.8에서 테스트)

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