0

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

有人知道问题是什么吗?

路由代码:

 // 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);
    }
4

1 回答 1

0

您应该通过翻译器告诉路由器使用哪个语言环境,我在引导程序中执行此操作:

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 doesn't reflect in app

于 2013-02-18T15:08:13.553 回答