Zend Framework:Zend_translate 和路由相关问题

发布于 2024-08-07 04:51:12 字数 2100 浏览 9 评论 0原文

我已经在我的应用程序中实现了 Zend_Navigation、Zend_Translate。 路由在 Bootstrap.php 中设置,如下所示。

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

在LaunguageSetup插件中,我定义了dispatchLoopStartup方法来检查语言参数

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

    }

发生的情况是语言始终具有默认值,“lang”参数在路由中永远不会默认lang值,即使我手动在地址栏中输入它即 /en_US/module/controller/action/ 它总是恢复到默认的 Zend_locale();

我可以修复它的唯一方法是在插件中再次设置路由并注入正确的语言值作为默认值。知道为什么吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫九 2024-08-14 04:51:13

有点像部分解决方案。

在dispatchLoopStartup中

添加

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

比重新定义和覆盖路由更好的方法,并通过更改默认的“lang”值来“伪造”语言参数。

它只是不太完美。 Zend_router 假设获取 'lang' 参数并将它们放置在 Zend_navigation->menu();

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

浪菊怪哟 2024-08-14 04:51:12

尝试在这一行之前对2个变量(_language,_languagesArray)进行var_dump,

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

我怀疑这应该是问题所在,因为你将你的插件放在dispatchLoopStartup上,然后参数可能不会被填充,我将我的插件放在routeShutdown上请参阅我的语言插件的实现

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文