Peach
Peach

Reputation: 19

change language with session in zend framework 2

I want make possible for user change the language of the webside. In the Module.php I wrote this:

 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;
                        },
                ),
        );
    }

In the IndexController.php I want to change the language and get it after in the module. So that the language changes. Here is my action:

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

        return $this->redirect()->toRoute('home');  
    }

The browser shows no error but the language doesn't change. Does someone see a error and can help me?

Upvotes: 0

Views: 2475

Answers (1)

Remi Thomas
Remi Thomas

Reputation: 1528

Module.php

public function onBootstrap(MvcEvent $e){
// session container
$sessionContainer = new \Zend\Session\Container('locale');

// test if session language exists
if(!$sessionContainer->offsetExists('mylocale')){
    // if not use the browser locale
    if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
        $sessionContainer->offsetSet('mylocale', \Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']));
    }else{
        $sessionContainer->offsetSet('mylocale', 'en_US');
    }

}

// translating system
$translator = $serviceManager->get('translator');
$translator ->setLocale($sessionContainer->mylocale)
            ->setFallbackLocale('en_US');

$mylocale = $sessionContainer->mylocale;

Controller

Just change the language from controller

/**
 *
 * @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;
}

From Zend Framework Multi Language Integration Steps

Upvotes: 1

Related Questions