views:

728

answers:

3

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?

A: 

Shouldn't it be $this->getRequest()->getParam('param') ? That would explain the problem.

tharkun
He's inside a plugin, not a controller.
smack0007
that produce the same param as $request->getParam('param');I have done a print_r on $router->getRoutes(); it show the correct path now, but my Zend_navigation setup for some reason always takes the 'lang' default value and all my link end up created with default 'lang' regardless what the getParam('lang') is.
Dan
which would mean that the navigation is not properly setup meaning it is not properly fed with the $request resp. $route
tharkun
yep it's inside the plugin, they produce same result. worse come to worse i loop and render the navigation. Just wondering the reason for this kind of behavior that's all.
Dan
A: 

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.

solomongaby
I tried print_r the route figure out its fixed for some reason. But My Zend_Navigation still takes the default 'lang'. I have had a look on your sample, did you use Zend_Navigation with this setup? I am wondering if there's any tricks. It seems that when using $view->navigation()->Menu() it always output the default 'lang'. I am using an xml file to store the navigation data.
Dan
A: 

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

Dan