简体   繁体   English

Symfony2电子邮件主题翻译。 命令和控制器之间的区别

[英]Symfony2 Email subject translation. Difference between Command and Controller

I encountered a problem with the translation symfony today. 我今天遇到了翻译symfony的问题。

Sent by SwiftMailer, emails are created in personal service. 由SwiftMailer发送,电子邮件是在个人服务中创建的。

case 1: function sendWelcomeEmailMessage is called in a Controller, simply when a new user is registered, the trans key 'registration.email.welcome.subject' is well translated 案例1:在Controller中调用函数sendWelcomeEmailMessage,只是在注册新用户时,trans键'registration.email.welcome.subject'被很好地翻译

case 2: function sendReflationEmailMessage is called in a symfony2 Command, but here, the key 'registration.email.welcome.subject' (same key for test) is not translated... 情况2:在symfony2命令中调用函数sendReflationEmailMessage,但是在这里,键'registration.email.welcome.subject'(测试的相同键)未被翻译...

Someone has an idea ? 有人有想法吗?

/**
 * @param UserInterface $user
 */
public function sendWelcomeEmailMessage(UserInterface $user)
{
    $params = $this->parameters['registration']['welcome'];
    $rendered = $this->templating->render(
        $params['template'], [
            'user' => $user
        ]
    );

    $subject = $this->translator->trans('registration.email.welcome.subject');

    $this->sendEmailMessage($rendered, $subject, $params['from_email'], $user->getEmail());
}

/**
 * @param UserInterface $user
 */
public function sendReflationEmailMessage(UserInterface $user)
{
    $params = $this->parameters['registration']['reflation'];
    $rendered = $this->templating->render(
        $params['template'], [
            'user' => $user
        ]
    );

    $subject = $this->translator->trans('registration.email.welcome.subject'); // Same trans key, only for test

    $this->sendEmailMessage($rendered, $subject, $params['from_email'], $user->getEmail());
}

Your command does not process config.yml and then it has no information about your current locale. 您的命令不处理config.yml ,然后它没有关于您当前语言环境的信息。 You need to set it explicitly: 您需要明确设置它:

/**
 * @param UserInterface $user
 */
public function sendReflationEmailMessage(UserInterface $user)
{
    $params = $this->parameters['registration']['reflation'];
    $rendered = $this->templating->render(
        $params['template'], [
            'user' => $user
        ]
    );

    $this->translator->setLocale("en_EN");
    $subject = $this->translator->trans('registration.email.welcome.subject'); // Same trans key, only for test

    $this->sendEmailMessage($rendered, $subject, $params['from_email'], $user->getEmail());
}

You could also use the ContainerAwareness : 您还可以使用ContainerAwareness

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;

class MyCommand extends ContainerAwareCommand
{
    public function sendReflationEmailMessage(UserInterface $user)
    {
        $params = $this->parameters['registration']['reflation'];
        $rendered = $this->templating->render(
            $params['template'], [
                'user' => $user
            ]
        );

        $subject = $this->getContainer()->get('translator')->trans('registration.email.welcome.subject');

        $this->sendEmailMessage($rendered, $subject, $params['from_email'], $user->getEmail());
    }
}

This should process your config.yml . 这应该处理你的config.yml

But it is best practice to not inject the complete container but the translator eg via twig. 最好的做法是不要注入完整的容器,而是注入翻译器,例如通过树枝。 Then you could do something like: 然后你可以这样做:

$this->twig->getExtension('translator')->trans('registration.email.welcome.subject', array(), 'translation_domain');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM