I've got a Zend Translate Issue. I have configure the zend translate in the bootstrap like below
public function _initTranslate() {
$locale = new Zend_Locale();
Zend_Registry::set('Zend_Locale', $locale);
$translate = new Zend_Translate(array(
'adapter' => 'ini'
)
);
$translate->addTranslation(
array(
'content' => APPLICATION_PATH . '/configs/languages/pt.ini',
'locale' => 'pt'
)
);
$translate->addTranslation(
array(
'content' => APPLICATION_PATH . '/configs/languages/en.ini',
'locale' => 'en'
)
);
$translate->setLocale($locale);
Zend_Registry::set('Zend_Translate', $translate);
}
I've added the languages and in my views I used translate helper but it shows me the following erros
Notice: The language 'en' has to be added before it can be used.
in C:\xampp\ZendFramework-1.11.10\library\Zend\Translate\Adapter.php
on line 443
Notice: No translation for the language 'en' available.
in C:\xampp\ZendFramework-1.11.10\library\Zend\Translate\Adapter.php
on line 456
I've followed zendframework reference guide. What I am doing wrong?
I solved ths issue with the code below:
public function _initTranslate() {
$this->bootstrap('locale');
if($this->hasResource('locale')){
$locale = $this->getResource('locale');
}
$translate = new Zend_Translate(array(
'adapter' => 'ini',
'disableNotices' => true,
)
);
$translate->getAdapter()->addTranslation(
array(
'content' => APPLICATION_PATH . '/configs/languages/pt.ini',
'locale' => 'pt'
)
);
$translate->getAdapter()->addTranslation(
array(
'content' => APPLICATION_PATH . '/configs/languages/en.ini',
'locale' => 'en'
)
);
if($translate->getAdapter()->isAvailable($locale->getLanguage())){
$translate->getAdapter()->setLocale($locale->getLanguage());
}else{
$translate->getAdapter()->setLocale('en');
}
Zend_Registry::set('Zend_Locale', $locale);
Zend_Registry::set('Zend_Translate', $translate);
}
And I coded a plugin to change language at runtime by just passing a GET variable Ex.: &lang=en
class Sistema_Plugin_Translate extends Zend_Controller_Plugin_Abstract {
public function preDispatch(Zend_Controller_Request_Abstract $request) {
$translate = Zend_Registry::get('Zend_Translate');
$locale = Zend_Registry::get('Zend_Locale');
$session = new Zend_Session_Namespace('language');
//verifica se existe GET e valida
if ($request->isGet()) {
$filter = new Zend_Filter_Alpha();
$param = $filter->filter($request->getParam('lang'));
if (!empty($param) && $locale->isLocale($param) && $translate->getAdapter()->isAvailable($param)) {
$translate->getAdapter()->setLocale($param);
$session->language = $param;
}
}
//verifica se o idioma do browser está disponivel se não coloca um idioma padrão
if (!$translate->getAdapter()->isAvailable($locale->getLanguage())) {
//linguagem não disponivel,seta idioma en
$translate->getAdapter()->setLocale('en');
}
//verifica se há sessão com idioma definido
if (isset($session->language)) {
if ($translate->getAdapter()->isAvailable($session->language)) {
$translate->getAdapter()->setLocale($session->language);
}
}
}
}