如何在Zend Framework中为每个URL添加语言

时间:2012-01-09 23:19:40

标签: zend-framework

所以,我设置了一种简单的方法来改变语言:

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

        $router = $this->_frontController->getRouter();

        $route = new Zend_Controller_Router_Route(
            ':language/:controller/:action/*',
            array(
                 'language' => 'en',
                 'module' => 'default',
                 'controller' => 'index',
                 'action' => 'index'
            )
        );
        $router->addRoute('lang_default', $route);

    }

    protected function _initLanguage()
    {
        $front = Zend_Controller_Front::getInstance();
        $front->registerPlugin(new JP_Controller_Plugin_Language());
    }

class JP_Controller_Plugin_Language
    extends Zend_Controller_Plugin_Abstract
{
    public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {

        $locale = new Zend_Locale();
        $options = array('scan' => Zend_Translate::LOCALE_FILENAME);
        $translate = new Zend_Translate('array', APPLICATION_PATH . '/translations/', 'auto', $options);

        $requestParams = $this->getRequest()->getParams();
        $language = (isset($requestParams['language'])) ? $requestParams['language'] : false;
        if ($language == false) {
            $language = ($translate->isAvailable($locale->getLanguage())) ? $locale->getLanguage() : 'en';
        }
        if (!$translate->isAvailable($language)) {
            throw new Zend_Controller_Action_Exception('This page, in this language doesn\'t exist', 404);
        } else {
            $locale->setLocale($language);
            $translate->setLocale($locale);

            Zend_Form::setDefaultTranslator($translate);


            setcookie('lang', $locale->getLanguage(), null, '/');
            Zend_Registry::set('Turniri_Language', $language);
            Zend_Controller_Front::getInstance()->setBaseUrl('/'.Zend_Registry::get('Turniri_Language'));
            Zend_Registry::set('Zend_Locale', $locale);
            Zend_Registry::set('Zend_Translate', $translate);
        }
    }
}

所以,现在我有了一个愿望。我想使用到目前为止一直使用的相同方法,比如像这样设置表单操作:

$form->setAction("/path/to/some/action");

但我也希望系统自动将语言ID添加到网址中。怎么可以这样做?

使用setBaseUrl()没有这样做。

0 个答案:

没有答案