Zend Navigation和Zend Locale

时间:2013-02-17 19:06:33

标签: zend-framework navigation zend-locale

我正在忙着用ZF1建立一个多语言网站。一切运作良好,期待一件事,多语言部分与zend导航相结合。当我打开开发页面的URL时,标准语言是英语(http://website.nl/en)。导航栏(基于zend导航)显示如下所示的de url:/ en / blog,/ en / events等。但是当我将de语言从英语更改为荷兰语(http://website.nl/nl)时,导航栏stil显示/ en /家,/ en /博客等。

有人知道问题是什么吗?

Routingcode:

 // Routing
 $front = Zend_Controller_Front::getInstance();

 // Remove default router
 $router = $front->getRouter();
 $router->removeDefaultRoutes();

 // Add routers from config
 $config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/routes.xml');
    $router->addConfig($config, 'routes');

导航代码:

    $this->bootstrap('layout');
    $this->bootstrap('view'); 
    $layout = $this->getResource('layout');
    $view = $layout->getView();

    // Set path to navigation.xmls
    $config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml');

    // ACL
    $acl = new Application_Model_Acl();

    // Get the identity of the user
    $auth = Zend_Auth::getInstance()->getIdentity();

    $navigation = new Zend_Navigation($config);
    $view->navigation($navigation)
        ->setAcl($acl)
        ->setRole($auth->role);

路由xml的一部分              

    <cameloth type="Zend_Controller_Router_Route_Hostname">
        <route>websiteurl</route>
        <defaults _layout="sitecameloth/layout" module="siteCameloth" />
        <chains>
            <locale>
                <route>:locale</route>
                <defaults controller="index" locale="en" /> 
                <reqs locale="[a-z]{2,3}" />
                <chains>
                    <index>
                        <route></route>
                        <defaults _layout="sitecameloth/front" action="index" />
                    </index>

                    <news>
                        <route>blog</route>
                        <defaults controller="news" />
                        <chains>
                            <list>
                                <route>:page</route>
                                <defaults action="list" page="1" />
                            </list>
                            <read>
                                <route>:id/:title</route>
                                <defaults action="read" />
                            </read>
                        </chains>
                    </news>

区域设置插件

    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
    $lang = $request->getParam('locale', '');

    if ($lang !== 'en' && $lang !== 'nl')
        $request->setParam('locale', 'en');

    $lang = $request->getParam('locale');

    if ($lang == 'en')
        $locale = 'en_US';
    else
        $locale = 'nl_NL';

    $zl = new Zend_Locale();
    $zl->setLocale($locale);
    Zend_Registry::set('Zend_Locale', $zl);

    $translate = new Zend_Translate('gettext', APPLICATION_PATH . '/languages/' . $locale . '.mo');
    Zend_Registry::set('Zend_Translate', $translate);
    }

1 个答案:

答案 0 :(得分:0)

你应该告诉路由器通过翻译器使用哪个语言环境,我在bootstrap中这样做:

protected function _initRewrite()
    {
        $translator = new Zend_Translate('array', APPLICATION_PATH . '/language/url-fr.php', 'fr');
        $translator->addTranslation(APPLICATION_PATH . '/language/url-en.php', 'en');

        // Set the current locale for the translator
        $locale = Zend_Registry::get('Zend_Locale');
        $translator->setLocale($locale);

        // Set it as default translator for routes
        Zend_Controller_Router_Route::setDefaultTranslator($translator);

        $front_controller = Zend_Controller_Front::getInstance();
        $router = $front_controller->getRouter();

        $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini', APPLICATION_ENV);
        $router->addConfig($config, 'routes');

        $router->addDefaultRoutes();
    }

有关我的回答的更多信息:Zend Navigation setting language parameter for route doesnt reflect in app