Search code examples
phpzend-framework2zend-translate

Zend framework 2 translator in model


How to get translator in model?

Inside view we can get translator using this code

$this->translate('Text')

Inside controller we can get translator using this code

$translator=$this->getServiceLocator()->get('translator');

$translator->translate("Text") ;

But how to get translator in model?

I'd tried so many ways to get service locator in models 2 of those

1)Using MVC events

    $e=new MvcEvent();
    $sm=$e->getApplication()->getServiceManager();
    $this->translator = $sm->get('translator');

if i pring $sm it is showing null. but it works fine in Model.php onBootstrap

2)Created one model which implements ServiceLocatorAwareInterface SomeModel.php

    <?php

namespace Web\Model;

use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class SomeModel implements ServiceLocatorAwareInterface
{
    protected $services;

    public function setServiceLocator(ServiceLocatorInterface $locator)
    {
        $this->services = $locator;
    }

    public function getServiceLocator()
    {
        return $this->services;
    }
}

and used that inside my model

        $sl = new SomeModel();
        $sm=$sl->getServiceManager();
        var_dump($sm); exit;
        $this->translator = $sm->get('translator');

this is also printing null.


Solution

  • If you don't need the servicemanager instance in your model, simply inject translator instance to it.

    For example:

    // Your model's constructor
    
    class MyModel {
      // Use the trait if your php version >= 5.4.0
      use \Zend\I18n\Translator\TranslatorAwareTrait;
    
      public function __construct( $translator )
      {
         $this->setTranslator( $translator ); 
      }
    
      public function modelMethodWhichNeedsToUseTranslator()
      {
        // ...
        $text = $this->getTranslator()->translate('lorem ipsum');
        // ...
      }
    }
    

    When you creating your model first time on service or controller level

    class someClass implements ServiceLocatorAwareInterface {
        public function theMethodWhichCreatesYourModelInstance()
        {
        // ...
        $sm = $this->getServiceLocator();
        $model = new \Namespace\MyModel( $sm->get('translator') )
        // ...
        }
    }
    

    If you need to instantiate your model (new MyModel();) on multiple methods/classes, consider to writing a factory for it.

    Here is a nice article about Dependency Injection and PHP by Ralph Schindler for more detailed comments about this approach.