我想使用樹枝模板系統爲我的電子郵件創建模板。電子郵件的區域設置應該基於用戶設置,而不是來自會話或請求區域設置。渲染枝條模板時如何強制區域設置?強制樹枝區域設置
該手冊確實提到了如何to force the locale for the Translator。但是我想將這個語言環境傳遞給render()方法,以便在該語言環境中呈現樹枝模板內部的翻譯。
這與在模板中將轉換爲不同,因爲我認爲這會強制在特定區域設置的模板內進行翻譯。
因此,採取從Symfony的例子,我在尋找這樣的事情:
public function indexAction($name)
{
$message = \Swift_Message::newInstance()
->setSubject('Hello Email')
->setFrom('[email protected]')
->setTo('[email protected]')
->setBody(
$this->renderView(
'HelloBundle:Hello:email.txt.twig',
array('name' => $name),
'nl_NL' // <-- This would be nice!
)
)
;
$this->get('mailer')->send($message);
return $this->render(...);
}
是的,但我不認爲模板會自動爲{%trans%}塊使用此區域設置,是不是? – rolandow 2013-03-08 12:52:35
不,你可以強制轉換過濾器使用語言環境,你想'{{「Hello」| trans({},「messages」,locale)}}',翻譯器組件自動使用請求中定義的語言環境,if你想改變它'$ this-> get('translator') - > setLocale($ locale);' – 2013-03-25 09:47:27