3

我正在开发一个带有两个模块(管理员和公共)的多语言 Zend 应用程序,我想在 url 中传递语言代码,所以在我的引导程序中我有:

受保护的功能_initAutoload(){

    $this->bootstrap('frontController');
    $this->_front = $this->getResource('frontController');

    $autoLoader = new Zend_Loader_Autoloader_Resource(array(
                'basePath' => APPLICATION_PATH,
                'namespace' => '',
                'resourceTypes' => array(
                    'form' => array(
                        'path' => 'admin/forms/',
                        'namespace' => 'Admin_Form_',
                    ),
                    'model' => array(
                        'path' => 'models/',
                        'namespace' => 'Model_'
                    )
                )
            ));

    $autoLoader_ = new Zend_Application_Module_Autoloader(array(
                'basePath' => APPLICATION_PATH . '/public/',
                'namespace' => 'Public_',
                'resourceTypes' => array(
                    'forms' => array(
                        'path' => 'forms/',
                        'namespace' => 'Public_Form_'
                    )
                )
            ));

    return $autoLoader;
}

protected function _initConfig() {
    $config = new Zend_Config_Xml(BASE_PATH . '/config.xml', APPLICATION_ENV);
    $registry = Zend_Registry::getInstance();
    $registry->set('config', $config);
    return $config;
}

protected function _initDb() {
    $this->bootstrap('config');
    $config = $this->getResource('config');
    $db = Zend_Db::factory($config->database->adapter, $config->database);
    $db->setFetchMode(Zend_Db::FETCH_OBJ);
    $db->query("SET NAMES 'utf8';");
    $db->query("SET CHARACTER SET 'utf8';");
    Zend_Db_Table::setDefaultAdapter($db);
    return $db;
}

protected function _initRoutes() {
    $router = $this->_front->getRouter();
    $router->removeDefaultRoutes();

    $language = new Zend_Controller_Router_Route(':language', array('language' => 'es'));

    $module = new Zend_Controller_Router_Route_Module(
                    array(
                        'module' => 'public',
                        'controller' => 'index',
                        'action' => 'index'
                    ),
                    $this->_front->getDispatcher(),
                    $this->_front->getRequest()
    );

    $module->isAbstract(true);

    $default = new Zend_Controller_Router_Route_Chain();
    $default->chain($language);
    $default->chain($module);

    $router->addRoute('default', $default);

}

问题是即使我没有指定语言,我也希望它能够工作。我该怎么做?如何提取语言代码 (en) 以在其中使用_initLocale

提前致谢

4

3 回答 3

1

这是我用于路由的引导程序初始化。它包含用于使用多语言路由并具有模块和 url halper 工作的所有解决方案:

public function _initRoutes() {
    $this->bootstrap('FrontController');
    $this->_frontController = $this->getResource('FrontController');
    $router = $this->_frontController->getRouter();

    if (isset($_GET['lang'])) {
        $lang = $_GET['lang'];
    } else {
        // auto recognition of language
        $locale = new Zend_Locale();
        $lang = $locale->getLanguage();
    }

    $langRoute = new Zend_Controller_Router_Route(
                    ':lang/',
                    array(
                        'lang' => $lang
                    )
    );

    $defaultRoute = new Zend_Controller_Router_Route(
                    ':controller/:action/*',
                    array(
                        'module' => 'default',
                        'controller' => 'index',
                        'action' => 'index'
                    )
    );

    $defaultRoute = $langRoute->chain($defaultRoute);

    $adminRoute = new Zend_Controller_Router_Route(
                    'admin/:controller/:action/*',
                    array(
                        'module' => 'admin',
                        'controller' => 'index',
                        'action' => 'index'
                    )
    );

    $router->addRoute('langRoute', $langRoute);
    $router->addRoute('defaultRoute', $defaultRoute);
    $router->addRoute('adminRoute', $adminRoute);
}
于 2012-12-22T21:13:10.600 回答
0

您需要链接语言路线中的所有路线。看这里:

http://robertbasic.com/blog/chaining-routes-in-zend-framework/

两个模块:

$defaultRoute = new Zend_Controller_Router_Route(  
        ':module/:controller/:action', 
于 2011-06-12T09:10:58.840 回答
0

这是一个通用的插件,允许我们创建自己的路线而无需记住语言。它还设置 Zend_Translator。这是一个基类,为了提高速度,我建议使用 Zend_Cache,因为下面的代码会影响代码效率(如果你有 +100 条路由,我会说它是必需的)。

<?php

class PsScripts_Controller_Plugin_Lang extends Zend_Controller_Plugin_Abstract {

    private function initTranslator($locale){
        $translate = new Zend_Translate(array('adapter' => 'tmx',
                    'content' => APPLICATION_PATH . '/configs/translations.tmx',
                    'locale' => $locale));
        Zend_Registry::set('Zend_Translate', $translate);
    }

    public function routeStartup(\Zend_Controller_Request_Abstract $request) {
        $locale = new Zend_Locale('pl_PL'); //default locale
        if (preg_match('/\/([a-z]{2})([\/].*)/', $request->getRequestUri(),$matches)){ //if locale is found in request
            $lang = $matches[1]; //obtain locale
            /* @var $locale Zend_Locale */
            switch ($lang){
                case 'en':
                    $locale->setLocale('en_GB');
                    break;
            }
            Zend_Registry::set('Zend_Locale',$locale);
            $this->initTranslator($locale);
            $router = Zend_Controller_Front::getInstance()->getRouter();
            /* @var $router Zend_Controller_Router_Rewrite */
            $langRoute = new Zend_Controller_Router_Route(
                ':lang', 
                array(
                    'lang' => $lang
                ),
                array(
                    'lang' => '[a-z]{0,2}'
                )
            );
            $routes = $router->getRoutes();
            foreach ($routes as $name=>$route){
                if ($name != 'default'){
                    /* @var $route Zend_Controller_Router_Route */
                    $router->removeRoute($name);
                    /* @var $lang Zend_Controller_Router_Route */
                    $chain = new Zend_Controller_Router_Route_Chain();
                    $chain->chain($langRoute)->chain($route);
                    $router->addRoute($name,$chain);
                }
            }
            $router->route($request);
        } else {
            $this->initTranslator($locale);
        }
        parent::routeStartup($request);
    }

}
于 2013-05-10T17:31:28.273 回答