Quantcast

Call to a member function _() on a non-object

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Call to a member function _() on a non-object

Kuzma
Hello All!
Currently I'm trying to set-up Zend Translate.
Everything works fine in layout.phtml, but the problems come when I'm trying to call translate method from view script. I get Call to a member function _() on a non-object.

My bootstrap file looks like:
  protected function _initTranslate()
    {
    Zend_Loader::loadClass('Zend_Translate');
               
                $frontendOptions = array('lifeTime' => 10);
                $backendOptions = array('cacheDir' => APPLICATION_PATH.'/languages/cache/');
                $cache = Zend_Cache::factory('Core','File',$frontendOptions,$backendOptions);
                Zend_Translate::setCache($cache);
                $this->translator = new Zend_Translate(
                'gettext',
                APPLICATION_PATH.'/languages/',
                'en',// current locale
                array(
                'scan'=>Zend_Translate::LOCALE_FILENAME
                ));
                Zend_Loader::loadClass('Zend_Validate_Abstract');
                //Zend_Validate_Abstract::setDefaultTranslator($this->translator);
                Zend_Registry::set('Zend_Translate', $this->translator);
                Zend_Loader::loadClass('Zend_Log_Writer_Stream');
                $writer = new Zend_Log_Writer_Stream(APPLICATION_PATH.'/languages/logs/file.log');
                Zend_Loader::loadClass('Zend_Log');
                $log    = new Zend_Log($writer);
                $this->translator->setOptions(array(
                'log'             => $log,
                'logMessage'      => "Unknown messages '%message%' in locale '%locale%'",
                'logUntranslated' => true
                ));
                $locale = $this->translator->getLocale();
                $source = APPLICATION_PATH.'/languages/'.$locale.'.mo';
                $this->translator->addTranslation($source,$locale);
    }
        protected function _initViewHelpers()
        {
                $view = new Zend_View();
                $view->setEncoding('UTF-8');
                $view->doctype('XHTML1_STRICT');
                $view->headTitle('GM');
                $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer($view);
                $viewRenderer->setView($view);
                Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
                Zend_Layout::startMvc(
            array(
                                'layout' => 'layout',
                'layoutPath' => '/application/views/layouts',
            'pluginClass' => 'ZFBlog_Layout_Controller_Plugin_Layout'
            )
        );
        $view = Zend_Layout::getMvcInstance()->getView();
        $view->translator = $this->translator;
                $this->bootstrap('layout');
                $layout=$this->getResource('layout');
        }
I'm calling 'translator' in that way: <?php echo $this->translator->_('Create');?>
Any help would be appreciated!
Thank you!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Call to a member function _() on a non-object

weierophinney
Administrator
-- Kuzma <[hidden email]> wrote
(on Wednesday, 18 November 2009, 01:35 PM -0800):
> Currently I'm trying to set-up Zend Translate.
> Everything works fine in layout.phtml, but the problems come when I'm trying
> to call translate method from view script. I get Call to a member function
> _() on a non-object.
>
> My bootstrap file looks like:
>   protected function _initTranslate()
>     {
>     Zend_Loader::loadClass('Zend_Translate');

Just a note: with Zend_Application, autoloading is enabled by default.
You don't need to do a call to Zend_Loader::loadClass(); just use the
class.

<snip>
> protected function _initViewHelpers()
> {

Another problem is here: you need to tell the bootstrap that this method
is dependent on your translate resource:

    $this->bootstrap('Translate');

Once you've done that, you can be sure that $this->translator is
defined.

> $view = new Zend_View();
> $view->setEncoding('UTF-8');
> $view->doctype('XHTML1_STRICT');
> $view->headTitle('GM');
> $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer($view);
> $viewRenderer->setView($view);
> Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
> Zend_Layout::startMvc(
>             array(
> 'layout' => 'layout',
>                 'layoutPath' => '/application/views/layouts',
>             'pluginClass' => 'ZFBlog_Layout_Controller_Plugin_Layout'
>             )
>         );
>         $view = Zend_Layout::getMvcInstance()->getView();
>         $view->translator = $this->translator;

Don't do this; use the translate() view helper. You can do that by
simply injecting the translator into the translate() view helper:

    $view->getHelper('Translate')->setTranslator($this->translator);

Then, in your view scripts, just invoke the translate() view helper:

    echo $this->translate('Create');

> $this->bootstrap('layout');
> $layout=$this->getResource('layout');
> }
> I'm calling 'translator' in that way: <?php echo
> $this->translator->_('Create');?>

See above.

You may also want to try simply passing the Zend_Translate instance into
Zend_Registry in your _initTranslate() method:
   
    Zend_Registry::set('Zend_Translate', $translator);

There are a number of translation-aware components that will
automatically find this, including the translate() view helper. This
makes initialization *much* simpler.

--
Matthew Weier O'Phinney
Project Lead            | [hidden email]
Zend Framework          | http://framework.zend.com/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Call to a member function _() on a non-object

Kuzma
In reply to this post by Kuzma
Thank you for your reply!
I must have only this translator's call: echo $this->translator->_('')
Otherwise PoEdit won't accept values for translation. It needs this form.
I've added in _initView that one line (->bootstrap('translate')):

        protected function _initViewHelpers()
        {
                $view = new Zend_View();
                $view->setEncoding('UTF-8');
                $view->doctype('XHTML1_STRICT');
                $view->headTitle('GM');
                $view->env = APPLICATION_ENV;
                $view->addHelperPath("Tinymce/View/Helper","Tinymce_View_Helper")
                ->addHelperPath("Views/Helpers","Views_Helpers",'Link')
        ->addHelperPath("ZendX/JQuery/View/Helper/", "ZendX_JQuery_View_Helper");
                ZendX_JQuery::enableView($view);    
                $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer($view);
               
                $viewRenderer->setView($view);
                Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);

                Zend_Layout::startMvc(
            array(
                                'layout' => 'layout',
                'layoutPath' => '/application/views/layouts',
            'pluginClass' => 'ZFBlog_Layout_Controller_Plugin_Layout'
            )
        );
       
        $view = Zend_Layout::getMvcInstance()->getView();
                $this->bootstrap('layout')
                         ->bootstrap('translate');
                //$this->bootstrap('Translate');
                $view->translator = $this->translator;
               
                $layout=$this->getResource('layout');
               
               
                $config=new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml', 'nav');
                $navigation=new Zend_Navigation($config);
               
                $view->navigation($navigation)->setAcl($this->_acl)->setRole(Zend_Registry::get('role'));
               
        }
But I'm still receiving that error
Still can't find solution :(
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Call to a member function _() on a non-object

jasonistaken
On 19/11/2009 1:13 AM, Kuzma wrote:
>
> Thank you for your reply!
> I must have only this translator's call: echo $this->translator->_('')
> Otherwise PoEdit won't accept values for translation. It needs this form.
> I've added in _initView that one line (->bootstrap('translate')):
>

That's not necessarily true. You can configure poEdit to search for any function, including calls to
`$this->translate()`

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

SV: Call to a member function _() on a non-object

Danny Fröberg
In reply to this post by Kuzma
Hi,
This one helped me when I was faced with the exact same problem in poedit;
http://www.tornstrand.com/2008/03/29/string-localization-with-gettext-and-zend-framework/

/Danny


> -----Ursprungligt meddelande-----
> Från: Kuzma [mailto:[hidden email]]
> Skickat: den 19 november 2009 10:13
> Till: [hidden email]
> Ämne: Re: [fw-general] Call to a member function _() on a non-object
>
>
> Thank you for your reply!
> I must have only this translator's call: echo $this->translator->_('')
> Otherwise PoEdit won't accept values for translation. It needs this form.
> I've added in _initView that one line (->bootstrap('translate')):
>
>         protected function _initViewHelpers()
>         {
>                 $view = new Zend_View();
>                 $view->setEncoding('UTF-8');
>                 $view->doctype('XHTML1_STRICT');
>                 $view->headTitle('GM');
>                 $view->env = APPLICATION_ENV;
>                 $view-
> >addHelperPath("Tinymce/View/Helper","Tinymce_View_Helper")
>                 ->addHelperPath("Views/Helpers","Views_Helpers",'Link')
>         ->addHelperPath("ZendX/JQuery/View/Helper/",
> "ZendX_JQuery_View_Helper");
>                 ZendX_JQuery::enableView($view);
>                 $viewRenderer = new
> Zend_Controller_Action_Helper_ViewRenderer($view);
>
>                 $viewRenderer->setView($view);
>
> Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
>
>                 Zend_Layout::startMvc(
>             array(
>                                 'layout' => 'layout',
>                 'layoutPath' => '/application/views/layouts',
>                 'pluginClass' => 'ZFBlog_Layout_Controller_Plugin_Layout'
>             )
>         );
>
>         $view = Zend_Layout::getMvcInstance()->getView();
>                 $this->bootstrap('layout')
>                          ->bootstrap('translate');
>                 //$this->bootstrap('Translate');
>                 $view->translator = $this->translator;
>
>                 $layout=$this->getResource('layout');
>
>
>                 $config=new Zend_Config_Xml(APPLICATION_PATH .
> '/configs/navigation.xml',
> 'nav');
>                 $navigation=new Zend_Navigation($config);
>
>
> $view->navigation($navigation)->setAcl($this->_acl)-
> >setRole(Zend_Registry::get('role'));
>
>         }
> But I'm still receiving that error :-(
> Still can't find solution :(
> --
> View this message in context: http://old.nabble.com/Call-to-a-member-
> function-_%28%29-on-a-non-object-tp26416195p26421243.html
> Sent from the Zend Framework mailing list archive at Nabble.com.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Call to a member function _() on a non-object

umpirsky
Poedit -> catalog -> settings -> keywords -> add your function name, in this case 'translate'.

Poedit will look for translate() calls and put in all strings. If you need to inject variables into your translation, use sprintf, like:

<? $this->sprintf(%this->translate('You added %s items.'), $num_of_items) ?>

Regards,
Saša Stamenković


On Thu, Nov 19, 2009 at 12:46 PM, Danny Fröberg <[hidden email]> wrote:
Hi,
This one helped me when I was faced with the exact same problem in poedit;
http://www.tornstrand.com/2008/03/29/string-localization-with-gettext-and-zend-framework/

/Danny


> -----Ursprungligt meddelande-----
> Från: Kuzma [mailto:[hidden email]]
> Skickat: den 19 november 2009 10:13
> Till: [hidden email]
> Ämne: Re: [fw-general] Call to a member function _() on a non-object
>
>
> Thank you for your reply!
> I must have only this translator's call: echo $this->translator->_('')
> Otherwise PoEdit won't accept values for translation. It needs this form.
> I've added in _initView that one line (->bootstrap('translate')):
>
>         protected function _initViewHelpers()
>         {
>                 $view = new Zend_View();
>                 $view->setEncoding('UTF-8');
>                 $view->doctype('XHTML1_STRICT');
>                 $view->headTitle('GM');
>                 $view->env = APPLICATION_ENV;
>                 $view-
> >addHelperPath("Tinymce/View/Helper","Tinymce_View_Helper")
>                 ->addHelperPath("Views/Helpers","Views_Helpers",'Link')
>         ->addHelperPath("ZendX/JQuery/View/Helper/",
> "ZendX_JQuery_View_Helper");
>                 ZendX_JQuery::enableView($view);
>                 $viewRenderer = new
> Zend_Controller_Action_Helper_ViewRenderer($view);
>
>                 $viewRenderer->setView($view);
>
> Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
>
>                 Zend_Layout::startMvc(
>             array(
>                                 'layout' => 'layout',
>                 'layoutPath' => '/application/views/layouts',
>                 'pluginClass' => 'ZFBlog_Layout_Controller_Plugin_Layout'
>             )
>         );
>
>         $view = Zend_Layout::getMvcInstance()->getView();
>                 $this->bootstrap('layout')
>                          ->bootstrap('translate');
>                 //$this->bootstrap('Translate');
>                 $view->translator = $this->translator;
>
>                 $layout=$this->getResource('layout');
>
>
>                 $config=new Zend_Config_Xml(APPLICATION_PATH .
> '/configs/navigation.xml',
> 'nav');
>                 $navigation=new Zend_Navigation($config);
>
>
> $view->navigation($navigation)->setAcl($this->_acl)-
> >setRole(Zend_Registry::get('role'));
>
>         }
> But I'm still receiving that error :-(
> Still can't find solution :(
> --
> View this message in context: http://old.nabble.com/Call-to-a-member-
> function-_%28%29-on-a-non-object-tp26416195p26421243.html
> Sent from the Zend Framework mailing list archive at Nabble.com.


Loading...