Zend Framework: Zend_translate and routing related

2019-07-16 11:01发布

I have implemented Zend_Navigation, Zend_Translate in my application. The routing is setup in Bootstrap.php like below.

$fc = Zend_Controller_Front::getInstance();
        $zl=new Zend_Locale();
        Zend_Registry::set('Zend_Locale',$zl);
        $lang=$zl->getLanguage().'_'.$zl->getRegion();
        $router = $fc->getRouter();
        $route = new Zend_Controller_Router_Route(':lang/:module/:controller/:action/*', 
        array(
    'lang'=>$lang, 'module'=>'default', 'controller'=>'index', 'action'=>'index'
));
$router->addRoute('default', $route);
$fc->setRouter($router);
$fc->registerPlugin( new Plugin_LanguageSetup());   

in LaunguageSetup Plugin i have defined the dispatchLoopStartup method to do the checking of the language param

    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) {
        $this->createLangUrl($request);
        $this->_language = $request->getParam('lang');
        if ((!isset($this->_language)) || !in_array($this->_language, $this->_languagesArray)) {
            $this->_language = 'en_US';
            $request->setParam('lang', 'en_US');
        }
        $file = APPLICATION_PATH.$this->_directory.$this->_language.'.csv';
        $translate = new Zend_Translate('csv', $file, $this->_language);
        Zend_Registry::set('Zend_Translate', $translate);
        $zl = Zend_Registry::get('Zend_Locale');
        $zl->setLocale($this->_language);
        Zend_Registry::set('Zend_Locale', $zl);


//        $fc = Zend_Controller_Front::getInstance();
//        $router = $fc->getRouter();
//        $route = new Zend_Controller_Router_Route(':lang/:module/:controller/:action/*', array(
//            'lang'=>$this->_language, 'module'=>'default', 'controller'=>'index', 'action'=>'index'
//        ));
//        $router->addRoute('default', $route);
//        $fc->setRouter($router);

    }

What happen is the language always have the default value, the 'lang' param never default lang value in route, even if i type it in the address bar manually i.e /en_US/module/controller/action/ It always get revert back to the default Zend_locale();

Only way i can fix it is to setup the route again in the plugin and inject a correct language value as default. Any Idea why?

2条回答
贼婆χ
2楼-- · 2019-07-16 11:13

Sort of a partial solution.

in dispatchLoopStartup

add

$fc = Zend_Controller_Front::getInstance();
$router = $fc->getRouter();
$router->setGlobalParam('lang',$this->_language);

better than redefine and overwrite the route again and 'fake' the language param by changing the default 'lang' value.

it's just less than perfect. Zend_router suppose to pick up the 'lang' param and have them placed in Zend_navigation->menu();

查看更多
成全新的幸福
3楼-- · 2019-07-16 11:31

try and do a var_dump of the 2 vars ( _language, _languagesArray ) before this line

if ((!isset($this->_language)) || !in_array($this->_language, $this->_languagesArray)) {

I suspect that there it should be the problem, because you put yor plugin on dispatchLoopStartup, and then the params might not be populated, i put my plugin on routeShutdown see my implementation of the languange plugin.

查看更多
登录 后发表回答