How to use translation in the controller using Zend?

Usually a custom translation in a view with this code:

<?php echo $this->translate("hello"); ?> 

How to get a transfer in the controller?

+7
source share
3 answers

If you create the zend_translate object at boot time, you can set it to Zend_Registry for later use:

 Zend_Registry::set('translate', $translate); 

and then use it in the controller:

 $translate = Zend_Registry::get('translate'); $translate->translate("hello"); 

As far as I know, Zend_Controller does not include native support for zend_translate.

+5
source

To use the translation in the controller:

 $this->view->translate('Something to translate'); 

Or create an assistant for translation if you want to keep everything clean and beautiful (although I don't think it's worth it).

+21
source

Or using the service locator (ZF2):

 $translator = $this->getServiceLocator()->get('translator'); $feed->setTitle($translator->translate('My RSS Feed')); 
0
source

All Articles