How to extend Zend View to implement a specific function?

I want to make it as simple as possible for our designers to localize strings in views, which means I want to do this:

... <p><?php echo $this->_("Hello, world!"); ?></p> ... 

An underscore is needed here to allow Poedit to automatically extract all lines for localization. The implementation is trivial:

 public function _($string) { return Zend_Registry::get('Zend_Translate')->_($string); } 

At the moment, I put this directly in Zend_View_Abstract , which is bad (I do not want to do this by modifying any of the Zend library). Ideally, I would extend Zend_View_Abstract to allow me to implement other specific functions that we need, but I don’t know how to set it. An alternative might be to implement a view helper, but the only way I know how to do this makes the code in the view more verbose. Any pointers (no, not like that) would be much appreciated. Thanks!

+6
php localization zend-framework
source share
4 answers

Obviously ignore my paths for your own ...

  • Extend Zend_View
  • Put your method in this extended class
  • Create an instance of the class (for example, in bootstrap)
  • Assign it to ViewRenderer
  • Pass this viewrenderer to the Zend_Controller_Action_HelperBroker's addHelper method
  • Use it in your presentation.

In / library / MegaHAL / Zend / create View.php:

 class MegaHAL_Zend_View extends Zend_View { public function _($string) { return Zend_Registry::get('translate')->_($string); } } 

In your bootstrap:

 require_once APPLICATION_PATH.'../library/MegaHAL/Zend/View.php'; $view = new MegaHAL_Zend_View(); $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer(); $viewRenderer->setView($view); Zend_Controller_Action_HelperBroker::addHelper($viewRenderer); 

In your opinion:

 <p><?php echo $this->_("Hello");?></p> 

I believe that will do what you want, right?

+14
source share

I think you're looking for a way to create custom view helpers .

Example:

 class My_View_Helper extends Zend_View_Helper_Abstract { public function translate($string) { //... } } 

...

 $view->setHelperPath('/path/to/helpers', 'My_View_Helper'); 

...

Then in your views you can use it:

 echo $this->translate("Hello, World!"); 
+2
source share

Although I believe that using helpers in the view will be the correct "Zend-Framework" for this, you can extend Zend_View and implement all the additional methods that you would like to have - this way you will not lose the Zend_View function.

 class My_View extends Zend_View { public function _($string) { return Zend_Registry::get('Zend_Translate')->_($string); } } 

Then you only need to make sure that your new view class has been created and registered as the default view in controller (setting the instance variable Zend_Controller_Action::$view to an instance of your class) and in Zend_Layout (when using layouts, passing your view as a config with view key) when not using ViewRenderer or in ViewRenderer when using ViewRenderer using Zend_Controller_Action_Helper_ViewRenderer::setView() .

+1
source share

When configured correctly, Poedit can automatically extract strings from functions other than _ ().

0
source share

All Articles