Search code examples
phpzend-frameworkzend-translate

Why is my call to Zend Translate not working in the view?


In my view the call $this->translate('test') is not working.

In the bootstrap I have the following code to initiate the translations:

protected function _initTranslate() {
    $apc = $this->getOption('enable_apc_cache');
    if ($apc) {
        $cache = Zend_Registry::get('cache');
        Zend_Translate::setCache($cache);
    }

    // Set the correct locale
    $locale = new Zend_Locale('nl');
    Zend_Registry::set('Zend_Locale', $locale);


    // Form error translations
    $translator = new Zend_Translate(
        'array',
        APPLICATION_PATH.'/../resources/languages',
        'auto',
        array('scan' => Zend_Translate::LOCALE_DIRECTORY)
    );
    Zend_Form::setDefaultTranslator($translator);

    // All other translations, including form labels
    $translate = new Zend_Translate(
        'gettext',
        APPLICATION_PATH.'/../resources/languages/',
        'auto',
        array('scan' => Zend_Translate::LOCALE_DIRECTORY));
    Zend_Registry::set('Zend_Translate', $translate);
}

If I understand correctly (have searched on stackoverflow and Google) putting the translator in the registry with key Zend_Translate should to the trick.

Now in my view when I call <?php echo $this->translate->_('Purchaseorder'); ?> or <?php echo $this->translate->('Purchaseorder'); ?> it gives me an error.

Call to a member function _() on a non-object with the underscore call and Parse error: syntax error, unexpected '(', expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' without the underscore call.

What am I doing wrong/missing? According to http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.initial.translate this should do the trick.


Solution

  • The correct syntax should be:

    <?php echo $this->translate('Purchaseorder'); ?>
    

    You do use this syntax at the top of your post but it seems from the code you mention later on that you're actually using a different syntax there.