I have been wondering this for a while. Take this example:

<?= Zend_Registry::get('translate')->_('translate me!');

I have my views cluttured with such code. My coworkers also complain often that its a lot to type juste to get translation and since it is repeated all over the place it gets tedious.

Some of them would love some global function that wraps everything in a short name like:

function t($text){
    return Zend_Registry::get('translate')->_($text);

But to me this is not good design and kinda defeats the idea of putting my translation object in the registry.

So I was wondering what others do to avoid having to write all this unnecessary code.

One solution would be to do in my controller:

$this->view->t = Zend_Registry::get('translate);

and then in my view just:

<?= $this->t->_('translate me!'); ?>

Another would be to create a view helper that does the job:

<?= this->translate('translate me!');?>

But it is more work and again adds a layer of logic on top of the already pretty robust and straigthforward Zend_Registry+ZendTranslate.


closed as off-topic by 200_success Dec 23 '15 at 9:38

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it." – 200_success
If this question can be reworded to fit the rules in the help center, please edit the question.


The two real questions are:

  1. What are the advantages of having this in Zend_Registry?
  2. What would you loose when using a helper function?

I believe the answers to these questions lead to this: use the helper function. This will:

  • save you a lot of typing,
  • highlight more importants parts of the code,
  • only add a tiny new layer of abstraction which is trivial to understand.

"Good design" is not about factories, registries, or whatever. It's about code that is easy to maintain and improve. The function is not going to make this any harder. If there's a translation error, the fix will stay the same: was the key correct? Is the stored translation correct? Still easy.

  • 2
    \$\begingroup\$ @Iznogood: Using the helper function tightly couples the code which the original does not. Thus you have the downside of tight coupling thus harder to specialize in the future. If this is a tradeoff you are willing to make then fine. \$\endgroup\$ – Martin York Feb 29 '12 at 18:27
  • \$\begingroup\$ @Iznogood: Can you not create a child view that has a translate method(). This gives you the best of both worlds. A default implementation (that is short and easy to use) and the ability to override the definition in a further derived type. \$\endgroup\$ – Martin York Feb 29 '12 at 18:49
  • \$\begingroup\$ @LokiAstari What does it couple with what? I'm sure you're right, but I don't understand. \$\endgroup\$ – Quentin Pradet Feb 29 '12 at 20:06
  • \$\begingroup\$ @LokiAstari Seeing as how ZF is full of singletons and encourages you to use the registry all over the place, tight coupling is pretty much par for the course anyway :) \$\endgroup\$ – GordonM Mar 1 '12 at 15:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.