0
votes

After doing some R&D I found that Zend\I18n will be best option available to use with-in my application for translation purposes. But I am having a major issue i.e it seems that I have to instantiate a new instance of translation whenever I need translation. A quick dirty way is to use global object and I am not going to use that. I am using Zend components e.g Zend Loader + Zend I18N only.

The question is how to manage global objects given zend framework. I don't want to change class constructors for dependency injection.Please provide me some suggestion.

Edit

As someone suggested using Zend_Registry but that is deprecated in Zf2.However, closely related thing in Zf2 is ServiceManager. But I am not getting a good example of how to initialize a ServiceManager at application level and use it to get global translation service ( if I am right ).

1
Dependency injection is the solution, and you've said you don't want to do that. - Tim Fountain
@TimFountain thanks , unfortunately I have lots of classes - sakhunzai
Considering I18n is supposed to be a thing of the VIEW and you have $this->translate() for that, I don't understand the need of having a "global object". If ever you need the i18n component in your controller, just write yourself a controllerplugin that gets the i18n stuff. If you need it elsewhere, you're doing wrong. - Sam
@Sam , my application is not fully MVC structure like Zend views etc. I am trying to embed it into my application. So I am not using Zend views/forms etc, just some required components. If that is costly I ll stop the idea of embedding. - sakhunzai

1 Answers

0
votes

If you want to use classes like translator in global scope you should use Zend_Registry.

All you have to do is set registry value with:

Zend_Registry::set('translator', $translator)

then you can get your instance from anywhere:

$translator = Zend_Registry::get('translator');

Of course 'translator' instance is just an example. You can get more detail in manual here