我正在尝试在 symfony2 (2.3.0) 控制台命令中使用翻译器,但我无法使其工作。这是我到目前为止所做的:
use Symfony\Component\Translation\IdentityTranslator;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class SendMessageCommand extends ContainerAwareCommand
{
protected function configure() {
$this->setName('mycommand:sendmessage')->setDescription('send message.');
}
protected function execute(InputInterface $input, OutputInterface $output) {
$translator = $this->getContainer()->get('translator');
echo $translator->trans('my.translation.key'); // not working
}
}
my.translation.key
存在于messages.yml 中。知道如何使它工作吗?
谢谢!