Issues

ZF-2763: Zend_View_Helper_Translate getLocale() and setLocale() fatal error

Description

Directly from the manual:


<?php
// our example adapter
$adapter = new Zend_Translate('array', array('simple' => 'einfach'), 'de');
Zend_Registry::set('Zend_Translate', $adapter);

// within your view
echo $this->translate()->getLocale();
?>

this returns a FATAL ERROR: Fatal error: Call to a member function getLocale() on a non-object

Even this:


<?php
// within your view
$adapter = new Zend_Translate('array', array('simple' => 'einfach'), 'de');
$this->translate()->setTranslator($adapter)->translate('simple');
// this returns 'einfach'
?>

returns a FATAL ERROR: Fatal error: Call to a member function setTranslator() on a non-object

Maybe I think because the translate() method, if it find a Zend_Translate object in Registry will not return himself but only the translated string, so getLocale() and setLocale() cannot be executed.

Comments

If seems that you are using outdated code.

I rewrote the mentioned example that you can use it within one file simulating an extra view:


<?php
require_once 'Zend/Registry.php';
require_once 'Zend/Translate.php';
require_once 'Zend/View.php';

$adapter = new Zend_Translate('array', array('simple' => 'einfach'), 'de');
Zend_Registry::set('Zend_Translate', $adapter);

$view = new Zend_View();
echo $view->translate()->getLocale();

As you see it's exactly the same as yours but instead of splitting the files to bootstrap and view I made one file. There is no exception returned, but the set locale.

Could it be that you executed the example as it is from one file, without splitting it to bootstrap and view, as mentioned ?

Issue wring executed... no problem found. You are free to reopen when you have additional informations.

Well, maybe we have found the problem :-)

This is the code take from snapshot:


if (null === $messageid) {
    return $this;
}

if (null === ($translate = $this->getTranslator())) {
    return $messageid;
}

And this is the code from the 1.5.0 version


if ($this->translate === null) {
    require_once 'Zend/Registry.php';
    if (!Zend_Registry::isRegistered('Zend_Translate')) {
        if (empty($messageid)) {
            return $this;
        } else {
            return $messageid;
        }
    } else {
        $this->translate = Zend_Registry::get('Zend_Translate');
    }
}

With the code from snapshot, this helper works fine.

Many thanks.

This issue should have been fixed for the 1.5 release.