Zend Framework: Zend_translate and Routing Issue
I have applied Zend_Navigation, Zend_Translate in my application. Routing is configured in Bootstrap.php as shown 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 the LaunguageSetup plugin I have defined the dispatchLoopStartup method to check the language parameter
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 happens is the language always has a default, the "lang" parameter never has the default lang in the route even if I type it in the address bar manually, i.e. / en_US / module / controller / action / It always returns back to default Zend_locale ();
The only way I can fix it is to re-configure the route in the plugin and enter the correct language by default. Any idea why?
source to share
try doing var_dump of two vars (_language, _languagesArray) before this line
if ((!isset($this->_language)) || !in_array($this->_language, $this->_languagesArray)) {
I suspect there must be a problem because you add the yor plugin to dispatchLoopStartup and then the parameters may not be populated, I set my plugin to routeShutdown see my languange plugin implementation .
source to share
Sorting a partial solution.
in dispatchLoopStartup
add
$fc = Zend_Controller_Front::getInstance();
$router = $fc->getRouter();
$router->setGlobalParam('lang',$this->_language);
better than redefining and rewriting the route again and "spoofing" the language setting by changing the default "lang".
he's just not perfect. Zend_router suppose we want to pick up the lang parameter and put them in Zend_navigation-> menu ();
source to share