4
votes

I encountered a problem with the translation symfony today.

Sent by SwiftMailer, emails are created in personal service.

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

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...

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());
}
2

2 Answers

3
votes

Your command does not process config.yml and then it has no information about your current locale. 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());
}
0
votes

You could also use the 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.

But it is best practice to not inject the complete container but the translator e.g. via twig. Then you could do something like:

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