Zend_Translate |路线颠倒了

时间:2012-07-28 15:25:17

标签: zend-framework localization routes zend-translate zend-config

我在使用ZF1.12应用程序的自定义路由时遇到问题。语言参数的切换有效,但路径的转换是相反的 - 例如:

http://www.my-app.de/de/clients/login

http://www.my-app.de/en/kunden/anmelden

为了管理我的语言,我注册了一个插件:

<?php

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

   if (substr($request->getRequestUri(), 0, -1) == $request->getBaseUrl()) {
        $locale = new Zend_Locale();
        $lang = $locale->getLanguage();
        $url = "http://my-app.de/" . $lang;
        Zend_Controller_Front::getInstance()->getResponse()->setRedirect($url);
    }


}

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

if ($lang !== 'en' && $lang !== 'fr' && $lang !== 'de') {
    $request->setParam('language','de');
    }

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

    switch($lang)
    {
    case ("de"):
        $locale = 'de_DE';
        break;
    case ("en"):
        $locale = 'en_EN';
        break;
    case ("fr"):
        $locale = 'fr_FR';
        break;
    }

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

$translate = new Zend_Translate('csv', APPLICATION_PATH . '/configs/lang/'. $lang . '.csv' , $lang);
Zend_Registry::set('Zend_Translate', $translate);

}
}

这很好用。在我的application.ini中:

; Translator -------------------------------------------------------------------
resources.locale.registry_key = "locale" 
resources.locale.default = "de_DE"  

resources.translate.locale = "de"
resources.translate.registry_key = "Zend_Translate"
resources.translate.adapter = "csv"
resources.translate.content = APPLICATION_PATH "/configs/lang/de.csv"
resources.translate.options.scan = "directory"

; Routes -----------------------------------------------------------------------
resources.router.routes.defaultmodule.type = Zend_Controller_Router_Route_Module
resources.router.routes.defaultmodule.abstract = On
resources.router.routes.defaultmodule.defaults.module = "default"

resources.router.routes.language.type = Zend_Controller_Router_Route
resources.router.routes.language.route = ":language"
resources.router.routes.language.reqs.language = "^(de|en)$"
resources.router.routes.language.defaults.language = "de"

resources.router.routes.default.type = Zend_Controller_Router_Route_Chain
resources.router.routes.default.chain = "language, defaultmodule"

resources.router.routes.login.route = ":language/@clients/@login"
resources.router.routes.login.defaults.module = "clients"
resources.router.routes.login.defaults.controller = "index"
resources.router.routes.login.defaults.action = "login"

例如,我尝试在视图脚本中使用此代码切换语言:

<?php
$requestUri = Zend_Controller_Front::getInstance()->getRequest()->getRequestUri();
$uri = explode("/", $requestUri);
$lang = $uri[1];
?>

<?php if ($lang != "de"):?><a href="<?php echo $this->url(array('language' => 'de'), null, false);?>"><?php echo $this->translate('lang_de'); ?></a>&nbsp;<?php endif?>
<?php if ($lang != "en"):?><a href="<?php echo $this->url(array('language' => 'en'), null, false);?>"><?php echo $this->translate('lang_en'); ?></a>&nbsp;<?php endif?>

当我读取请求参数时,语言被正确切换,但路线被反转。任何想法如何解决这个问题?

谢谢你,抱歉我的英语不好。

0 个答案:

没有答案