Zend_Controller_Router_Route: Could not find a translator

一世执手 提交于 2019-12-14 02:06:02

问题


I am developing a multilanguage application. In the bootstrap there is the routes setup:

protected function _initRoutes() {

    $this->bootstrap('frontController');
    $router = $this->frontController->getRouter();

    // PAGES ROUTE
    $page = new Zend_Controller_Router_Route(
        ':language/:ident',
        array(
            'module'  => 'core',
      'controller' => 'pagine',
      'action'  => 'view'             
        ),
        array(
            'ident' => '[a-zA-Z-_0-9]{3,}',
         'language' => '[a-z]{2}'
        )
    );

    $registrazione = new Zend_Controller_Router_Route(
        ':language/@utenti/@registrati',
        array(
            'module'  => 'core',
'controller' => 'utenti',
'action'  => 'registrazione'
        ),
        array(
         'language' => '[a-z]{2}'
        )
    );

    $router->addRoute('page', $page);
    $router->addRoute('registrazione', $registrazione);

    .....

}

I cannot set the default translator to Zend_Controller_Router_Route (for translated segments) because i don't know the language parameter in the request object. I get the language parameter in Multilanguage Plugin during the "routeShutdown":

class Activa_Controller_Plugin_Multilanguage extends Zend_Controller_Plugin_Abstract {

public function routeShutdown(Zend_Controller_Request_Abstract $request) {
    $language = $request->getParam("language");
    $locale   = new Zend_Locale($language);

    $translate = new Zend_Translate('array', APPLICATION_PATH.'/config/lang/'.$language.'.php', $locale);

    Zend_Registry::set('Zend_Locale', $locale);
    Zend_Registry::set('Zend_Translate', $translate);

    Zend_Controller_Router_Route::setDefaultTranslator($translate);
    ////////////////////////
    // BUT NOW IS TOO LATE
    ////////////////////////
}

When i type the address "http://servername/it/utenti/registrati" i get the exception with the message "Could not find a translator".

How can i fix it? Antonio (Italy)


回答1:


Create a routeStartup() plugin, in which you scan the request URI for the locale/language, for example with a simple Regex. Then create and set the default translator in the Router.



来源:https://stackoverflow.com/questions/2960555/zend-controller-router-route-could-not-find-a-translator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!