Hi.
In a project I'm working on I should show translate url for a better SEO
So I ended up with this code
class App_Controller_Plugin_LangSelector extends Zend_Controller_Plugin_Abstract { const DEFAULT_LOCAL = 'en_UK'; const DEFAULT_LANG = 'en_UK'; protected $_router; protected $_layout; protected $_view; public function __construct() { $front = Zend_Controller_Front::getInstance(); $this->_router = $front->getRouter(); } public function routeStartup(Zend_Controller_Request_Abstract $request) { try{ $i18n = Zend_Registry::get('i18n'); $locale = $i18n['locale']; $lang = $i18n['lang']; } catch(Zend_Exception $e){ $locale = self::DEFAULT_LOCAL; $lang = self::DEFAULT_LANG; } $zl = new Zend_Locale(); $zl->setLocale($locale); Zend_Registry::set('Zend_Locale', $zl); $fileName = APPLICATION_PATH . '/configs/lang/'. $lang . '.php'; if(!file_exists($fileName)){ $fileName = APPLICATION_PATH . '/configs/lang/'. self::DEFAULT_LANG . '.php'; } if(strpos($lang, '_') !== false){ list($lang,$region)= explode('_',$lang); } $translate = new Zend_Translate('array', $fileName , $lang); $translatedController = $translate->translate('live'); $translatedAction = $translate->translate('write'); Zend_Registry::set('Zend_Translate', $translate); $this->_router->addRoute( 'live', new Zend_Controller_Router_Route($translatedController, array('module' => 'default', 'controller' => 'live', 'action' => 'index') )); $this->_router->addRoute( 'livewrite', new Zend_Controller_Router_Route($translatedController.'/'.$translatedAction, array('module' => 'default', 'controller' => 'live', 'action' => 'write') )); $this->_layout = Zend_Controller_Action_HelperBroker::getStaticHelper('Layout'); $this->_view = $this->_layout->getView(); Zend_Form::setDefaultTranslator($translate); $this->_view->translate()->setTranslator($translate); } public function preDispatch(Zend_Controller_Request_Abstract $request) { } } // en_UK.php file return array( /* router */ 'live' => 'live', 'write' => 'write', 'Hello word' => 'Hi. How are you ? ', 'I am the live index' => 'I am the live index', 'I am the live write' => 'I am the live write', ); // it_IT.php file return array( /* router */ 'live' => 'dalvivo', 'write' => 'scrivi', 'Hello word' => 'Ciao. Come va ? ', 'I am the live index' => 'Sono la live index', 'I am the live write' => 'Sono la live write', );
It works fine (mysite.com/dalvivo mysite.com/dalvivo/scrivi ) but it is limited to a route so I found out this reference http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.routes.standard.translated-segments
I update the code to
_router = $front->getRouter(); } public function routeStartup(Zend_Controller_Request_Abstract $request) { try{ $i18n = Zend_Registry::get('i18n'); $locale = $i18n['locale']; $lang = $i18n['lang']; } catch(Zend_Exception $e){ $locale = self::DEFAULT_LOCAL; $lang = self::DEFAULT_LANG; } $zl = new Zend_Locale(); $zl->setLocale($locale); Zend_Registry::set('Zend_Locale', $zl); $fileName = APPLICATION_PATH . '/configs/lang/'. $lang . '.php'; if(!file_exists($fileName)){ $fileName = APPLICATION_PATH . '/configs/lang/'. self::DEFAULT_LANG . '.php'; } if(strpos($lang, '_') !== false){ list($lang,$region)= explode('_',$lang); } $translate = new Zend_Translate('array', $fileName , $lang); Zend_Registry::set('Zend_Translate', $translate); // Set the current locale for the translator $translate->setLocale($lang); // Set it as default translator for routes $route = new Zend_Controller_Router_Route( ':@controller', array( 'module' => 'default', 'controller' => 'live', 'action' => 'index' ) ); $this->_router->addRoute('live', $route); $route->assemble(array('controller' => 'live', 'action' => 'index')); $route = new Zend_Controller_Router_Route( ':@controller/:@action', array( 'module' => 'default', 'controller' => 'live', 'action' => 'write' ) ); $this->_router->addRoute('livewrite', $route); $route->assemble(array('controller' => 'live', 'action' => 'write')); $this->_layout = Zend_Controller_Action_HelperBroker::getStaticHelper('Layout'); $this->_view = $this->_layout->getView(); Zend_Form::setDefaultTranslator($translate); $this->_view->translate()->setTranslator($translate); } public function preDispatch(Zend_Controller_Request_Abstract $request) { } }
It workish because if I put mysite.com/dalvivo instead of seeing the index page I see dalvivo/scrivi
Is there anything wrong in my code ?
Thanks in advance
Bye Whisher
View this message in context: Router Translated segments
Sent from the Zend MVC mailing list archive at Nabble.com.
没有评论:
发表评论