Zend framework2 在zend framework 2中使用会话更改语言

Zend framework2 在zend framework 2中使用会话更改语言,zend-framework2,zend-translate,zend-session,Zend Framework2,Zend Translate,Zend Session,我想让用户更改网站的语言成为可能。在Module.php中,我写了以下内容: public function onBootstrap(MvcEvent $e) { $e->getApplication()->getServiceManager('translator'); $eventManager = $e->getApplication()->getEventManager(); $module

我想让用户更改网站的语言成为可能。在Module.php中,我写了以下内容:

 public function onBootstrap(MvcEvent $e)
    {
        $e->getApplication()->getServiceManager('translator');

        $eventManager        = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);

        $eventManager->attach(\Zend\Mvc\MvcEvent::EVENT_DISPATCH, array($this, 'bootstrapSession'), 10);

        $config = $this->getConfig();

        \Locale::setDefault('de');
        \Zend\Validator\AbstractValidator::setDefaultTranslator(
                $e->getApplication()
                ->getServiceManager()
                ->get('translator')
        );


        if ($session->language !== NULL)
        {
            $e->getApplication()->getServiceManager()->get('translator')->setLocale($session->language);
    }

    public function bootstrapSession()
    {
        $config = $this->getConfig();

        $sessionConfig = new Session\Config\SessionConfig();
        $sessionConfig->setOptions($config['session']);

        $sessionManager = new Session\SessionManager($sessionConfig);
        $sessionManager->start();

        var_dump($sessionManager);

        Session\Container::setDefaultManager($sessionManager);
    }

    public function getServiceConfig()
    {
        var_dump('halloo');
        return array(
                'factories' => array(
                        'session' => function() {
                            $session = Session\Container::getDefaultManager()->getStorage();
                            return $session;
                        },
                ),
        );
    }
在IndexController.php中,我想更改语言并在模块中获得它。因此,语言发生了变化。 以下是我的行动:

public function enAction()
    {   
        $session = $this->getServiceLocator()->get('session');
        $session->language = 'en';

        return $this->redirect()->toRoute('home');  
    }
浏览器不会显示错误,但语言不会更改。有人看到错误并能帮助我吗?

Module.php 控制器 只需从控制器更改语言即可

/**
 *
 * @return \Zend\View\Model\ViewModel 
 */
public function changelocaleAction(){

    // disable layout
    $result = new ViewModel();
    $result->setTerminal(true);

    // variables
    $event   = $this->getEvent(); 
    $matches = $event->getRouteMatch(); 
    $myLocale = $matches->getParam('locale');
    $redirect = $matches->getParam('redirecturl', '');

    // translate
    $sessionContainer = new Container('locale');

    switch ($myLocale){
        case 'fr_FR':
            break;
        case 'en_US':
            break;
        default :
            $myLocale = 'en_US';
    }

    $sessionContainer->offsetSet('mylocale', $myLocale);

    // redirect
    switch ($redirect){
        case '':
            $this->redirect()->toRoute('home');
            break;
        default :
            $this->redirect()->toUrl(urldecode($redirect));
    }

    return $result;
}

谢谢,但当我这样做时,出现了一个错误:致命错误:无法使用Zend\Session\Container作为容器,因为该名称已在C:\xampp\htdocs\pimp\module\Application\src\Application\Controller\IndexController.phpI中使用,请不要像在代码中那样启动会话,这可能是您的错误。sessionContainer是在创建过程中启动的()。请参见其他问题,SlmLocale也可以为您执行此操作。
/**
 *
 * @return \Zend\View\Model\ViewModel 
 */
public function changelocaleAction(){

    // disable layout
    $result = new ViewModel();
    $result->setTerminal(true);

    // variables
    $event   = $this->getEvent(); 
    $matches = $event->getRouteMatch(); 
    $myLocale = $matches->getParam('locale');
    $redirect = $matches->getParam('redirecturl', '');

    // translate
    $sessionContainer = new Container('locale');

    switch ($myLocale){
        case 'fr_FR':
            break;
        case 'en_US':
            break;
        default :
            $myLocale = 'en_US';
    }

    $sessionContainer->offsetSet('mylocale', $myLocale);

    // redirect
    switch ($redirect){
        case '':
            $this->redirect()->toRoute('home');
            break;
        default :
            $this->redirect()->toUrl(urldecode($redirect));
    }

    return $result;
}