Translate in the controller #4551

Closed
pdobrigkeit opened this Issue May 28, 2013 · 9 comments

5 participants

@pdobrigkeit

How are you supposed to use the Translator inside the controller, i.e. when you are returning a JsonModel and want to translate error messages? Should there be a controller plugin, or is there another approach that should be taken here?

@localheinz
Zend Framework member

You could retrieve the translator like this

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

and then translate whatever is needed.

Also see @weierophinney's comments in #4305.

@pdobrigkeit

Well, since there is a discusion whether the ServiceLocator actually should be injected into the controllers, I would consider this not the "correct" approach, even though it is what I now wound up doing.

@localheinz
Zend Framework member

@pdobrigkeit

The only problem I see with injecting the dependencies of a controller in the factory is that

  • you may not need all dependencies in every action or
  • you may not need them at all times in a specific action

Thus, you may have a better design, but a performance penalty because you have to create services that are not needed in a specific case. However, this might result in smaller controllers, with smaller (more dedicated) responsibilities.

@weierophinney
Zend Framework member
@localheinz
Zend Framework member

Matthew, can you point us to the documentation for these lazy and deferred service types? I would like to find out more about it. Deeply appreciate it!

@localheinz
Zend Framework member

Ah, I see - right here: #2995.

For those interested, the corresponding PRs are right here:

@localheinz
Zend Framework member
@Maks3w Maks3w closed this Jul 15, 2013
@FernandoSJr

localheinz thanks thanks thanks!!

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment