Reputation: 1409
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.
Upvotes: 0
Views: 872
Reputation: 884
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
Upvotes: 1
Reputation: 1042
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/
Upvotes: 1