2017-03-01 87 views
-1

如何調用該函數注入翻譯.PHP

$this->get('translator')->trans() 

在我的班級命名空間迪\ NotificationBundle \經理;

class SMSManager 
{ 
    private function send(){ 

     // call $this->get('translator')->trans() 
    } 
} 

回答

0

,並通過翻譯服務類的依賴性,例如:

class SMSManager 
    { 

public function __construct(
    TranslatorInterface $translator 
) { 
    $this->translator = $translator; 
} 

定義爲例子爲:

acme_sms_manager: 
    class: Di\NotificationBundle\Manager\SMSManager 
    arguments: 
     - '@translator' 

然後簡單地調用爲:

private function send(){ 

    this->translator->trans() 
} 

希望得到這個幫助