j2dab
j2dab

Reputation: 73

Zend Framework - view object set in bootstrap not available in view phtml files

I have been trying to set an object in the Bootstrap.php file and would like to be able to use this in all view phtml files.

As an example I set up Zend_Translate in an _init function as follows:

function _initTranslations() {
    $this->bootstrap('layout');
    $layout = $this->getResource('layout');
    $view = $layout->getView();

    $translate = new Zend_Translate('gettext', 
                                    APPLICATION_PATH.'/languages',
                                    null,
                                    array('scan' => Zend_Translate::LOCALE_FILENAME));
    $session = new Zend_Session_Namespace('translation'); // get session to check if user set lang
    $locale = new Zend_Locale();
    if (isset($session->language)) {
        // if user has previously set the lang we use this setting
        $requestedLanguage = $session->language;
        $locale->setLocale($requestedLanguage);
    } else {
        // otherwise we use the browser's setting
        $locale->setLocale(Zend_Locale::BROWSER);
        $requestedLanguage = key($locale->getBrowser());
    }
    if (in_array($requestedLanguage, $translate->getList())) {
        $language = $requestedLanguage;
    } else {
        $language = 'en';
    }
    Zend_Registry::set('locale', $locale);
    $translate->setLocale($language);
    $view->translate = $translate;
}

I would have thought that the $view->translate = $translate; would be sufficient to make the $translate object available in my views but it can only be accessed in my layout.phtml

Am relativley new to Zend Framework and suppose this issue has come up to others before but my internet research did not give me an answer so I was hoping someone here could point me to the right direction?

Upvotes: 1

Views: 1256

Answers (2)

Asciant
Asciant

Reputation: 2160

Although you have found your answer, you can access the view resource in your bootstrap with the follow:

$this->bootstrap('view');
$view = $this->getResource('view');

Upvotes: 0

Ivan Marjanovic
Ivan Marjanovic

Reputation: 1049

You only need to set Zend_Translate to Zend_Registry in Bootstrap and you ca use translate view helper in any phml.

Put this line at the end:

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

Upvotes: 2

Related Questions