symfony 2.0 コマンド (cron によって呼び出される) を介してユーザーにリマインダーを送信しようとしています。問題は、私たちのウェブサイトが多言語であることです (デフォルトはフランス語です)。
リマインダーを送信するループの各ステップでロケールを設定しています。初めてロケールが正しく設定されます。しかし、後続のステップでは、最新のロケールの変更がテンプレートに反映されていないかのように、テンプレートは最初のステップのロケールに変換されます。
ロケールの変更が反映されるようにするには、どうすれば修正できるのでしょうか。
参照用のコード(簡略化)を次に示します。
<?php
// This loop is inside the execute() function of a symfony service (implements ContainerAwareCommand)
// …
// Sending reminders one at a time
foreach ($reminders as $reminder)
{
$message = \Swift_Message::newInstance();
$message->setFrom(array('noreply@domain.com' => 'YourBot'));
// Change locale to that of the user
$this->getContainer()->get('session')->setLocale($reminder->getLocale());
$templating = $this->getContainer()->get('templating');
// Reminder text
$email_message = $templating->render('MyBundle:Reminder:reminder.html.twig');
$message->setSubject('Reminder')
->setTo($reminder->getEmail())
->setBody($email_message, 'text/html');
$this->getContainer()->get('mailer')->send($message);
// Update reminder status
$reminder->setEmailSent(true);
$emSymfony->persist($reminder);
}
// … subsequent code
?>
ご協力いただきありがとうございます!