0

i found that is simple to set the translation in zend framework. You just have to make some files with the languages and get the locale from the user agent. The only thing i can't find out is "where" should i place the call to zend_translate class. So i have these lines:

 <?php
 // load required classes
 require_once 'Zend/Loader.php';
 Zend_Loader::loadClass('Zend_Translate');
 Zend_Loader::loadClass('Zend_Locale');
 Zend_Loader::loadClass('Zend_Registry');

 // initialize locale and save in registry
 // auto-detect locale from browser settings
 try {
   $locale = new Zend_Locale('browser');
 } catch (Zend_Locale_Exception $e) {
   $locale = new Zend_Locale('en');  
 }
 $registry = Zend_Registry::getInstance();
 $registry->set('Zend_Locale', $locale);

And i don't know if i should set them in the controller or in the view, as it would be obvious to do with thos other lines:

 <title><?php echo this->translate('Title'); ?></title>

So do i have to set the class in the controller and pass the variable to the view? Thank you for the help.

softwareplay
  • 1,379
  • 4
  • 28
  • 64
  • It trows an error in _initTranslate in the line i set the local filename. Can you check my syntax for this line? array('scan' => Zend_Translate::text.php it says the "text" constant isn't found. What should i write in there? – softwareplay Jan 10 '14 at 17:33

2 Answers2

1

You could set Zend_Locale in controller, but in general you should do this "higher", because basicaly you would like to access zend_locale from other controllers. In general you initialise your registry in your bootstrap or just in index.php file. Here is nice guide on how to setup zend_locale: http://delboy1978uk.wordpress.com/category/languages/php/zend-framework/zend_registry/

MSadura
  • 1,032
  • 13
  • 18
  • 1
    @softwareplay - I cant write comment to your post so I'll comment my own. Regarding your second question - you can't replace LOCALE_FILENAME with your own file because it is a constatnt that tells zend how to automaticly search for your language files(optionally LOCALE_DIRECTORY). You should put your file in language folde with proper name for example (in root directory) languages/text.en.php and scan will search for your file. – MSadura Jan 10 '14 at 22:19
1

You can init Zend_Translate into the boostrap by creating the _initTranslate function:

 protected function _initTranslate()
{
    // (optional) get cache
    $cache = $this->bootstrap('cachemanager')
                  ->getResource('cachemanager')
                  ->getCache('generic');
    $translate = new Zend_Translate(
        'gettext',
        APPLICATION_PATH . '/languages',
        'fr',
        array(
            'scan' => Zend_Translate::LOCALE_FILENAME,
            'logUntranslated' => false
        )
    );
    $translate->setCache($cache);
    Zend_Registry::set('Zend_Translate', $translate);

    // Traducteur par defaut pour les classes suivantes
    Zend_Validate_Abstract::setDefaultTranslator($translate);
    Zend_Form::setDefaultTranslator($translate);
    return $translate;
}

So the translator will be available into the entire app

ebelair
  • 844
  • 11
  • 27