Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Translate doesn't find language

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?

like image 482
dextervip Avatar asked Dec 12 '11 19:12

dextervip


2 Answers

Did you try passing a language to Zend_Locale?

$locale = new Zend_Locale('en_US');

Additionally, I found a work around:

$locale = new Zend_Locale(Zend_Locale::BROWSER);

$translate = new Zend_Translate(
    'ini',
    $yourPath,
    null,
    array('scan' => Zend_Translate::LOCALE_DIRECTORY));

// setting the right locale
if ($translate->isAvailable($locale->getLanguage())) {
    $translate->setLocale($locale);
} else {
    $translate->setLocale('en_US');
}

See http://framework.zend.com/issues/browse/ZF-6612 for more details. Note: this is a bug for 1.8, I see you're using 1.10 but the work-around might still work.

This is also a good thread: http://zend-framework-community.634137.n4.nabble.com/how-handle-Locale-td659923.html

Also, Zend_Translate offers an option to disable notices specifically for that class. If the content is being translated, then this (according to Zend) is not an "error" and notices should be disabled.

// as a fourth parameter to Zend_Translate pass it:
array('disableNotices' => true);
like image 170
Yes Barry Avatar answered Sep 23 '22 19:09

Yes Barry


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);

            }
        }


    }

}
like image 41
dextervip Avatar answered Sep 21 '22 19:09

dextervip