donglu2761 2014-07-25 13:27
浏览 35
已采纳

当我在zf2中发送电子邮件时,翻译不起作用

I'm using the mailing library from zend framework 2, and also I'm using the translator functionality. But the translations only work inside the template .phtml, and not work in the service function for send the email, for example in translating the subject of the email. This is my code inside my service function to send an email:

$mailFactory = $this->getDependency('mail');
$viewModel = new ViewModel(array(
    'variable' => $x
));
$viewModel->setTemplate('template.phtml');
$message = $mailFactory->prepareMessage($viewModel);
$message->getHeaders()
    ->addHeaderLine('MIME-Version', '1.0')
    ->addHeaderLine('Content-type', 'text/html; charset=UTF-8');
$message->setSubject($this->translate('Recover your password'));
$message->setEncoding("UTF-8");
$message->addTo($email, $fullname);
$transport = $mailFactory->getTransport();

return $transport->send($message);

The function $this->translate('...') calls to a class that call this code: return $this->translator->translate($text), this works well in all my application, but only works wrong when I send any email (with an exception of the template.phtml, inside this file the translator works well). All my mails received, have the subject in english (not translated) and the content in spanish (translated)

  • 写回答

1条回答 默认 最新

  • dqvtm82066 2014-07-25 14:08
    关注

    You have to pass the translator to the service to use it.

    So in your module.config.php under the service_manager key you can do it with a factory or an initializer.

    Factory:

    'service_manager' => array(
        'factories' => array(
            'YourNamespace\YourMailService' => function ($serviceManager) {
                $mailService = new YourNamespace\YourMailService();
                $mailService->setTranslator($serviceManager->get('translator'));
                return $mailService;
            },
        )
    ),
    

    You need the method setTranslator in your service class!

    Inititalizer:

    'service_manager' => array(
        'initializers' => array(
            function ($instance, $sm)
            {
                if ($instance instanceof \YourNamespace\TranslatorAwareInterface) {
                    $instance->setTranslator($sm->get('translator'));
                }
            }
        ),
    ),
    

    With an initializer you need the interface TranslatorAwareInterface:

    namespace YourNamespace;
    
    use Zend\Mvc\I18n\Translator;
    
    interface TranslatorAwareInterface
    {
        public function setTranslator(Translator $translator);
    }
    

    And your service need to implement that interface:

    class YourMailService implements TranslatorAwareInterface
    {
        protected $translator;
    
        public function setTranslator(Translator $translator)
        {
            $this->translator = $translator;
        }
    }
    

    In both cases you can use the translator like:

    $message->setSubject($this->translator->translate('Recover your password'));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵