I'm having trouble with own defined routes of my ZF1.12 Application. The switch of the language Parameters works, but the translation of the routes is reversed - for example:
http://www.my-app.de/de/clients/login
http://www.my-app.de/en/kunden/anmelden
For manage my language I register a plugin:
<?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);
}
}
This works fine. In my 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"
For example I tried to switch the language with this code in a view script:
<?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> <?php endif?>
<?php if ($lang != "en"):?><a href="<?php echo $this->url(array('language' => 'en'), null, false);?>"><?php echo $this->translate('lang_en'); ?></a> <?php endif?>
When I read the request params the language is switched correctly,but the routes are reversed. Any Idea how can I fix this?
Thank you and sorry for my bad english.