1
votes

I follow this http://symfony.com/it/doc/current/cookbook/controller/error_pages.html

but when I'm in 404 page my locale change in my default locale

my locale defualt is "it" but when I'm on en (mysite/en/foo where foo dosen't exist) my locale switch to "it"

i try also this Listener http://symfony.com/it/doc/current/cookbook/session/locale_sticky_session.html

and when

if ($locale = $request->attributes->get('_locale')) {

my $locale is null and the switch to default locale

1

1 Answers

0
votes

I found one solution, is not the Best Solution but works

in my homepage

HomepageController

public function indexAction()
{
    $request = $this->get('request');
    /**
     * Store it into session the user language
     */
    $sessionId  = $this->get("session");
    if($sessionId->get("lingua")==""){
        $this->get('session')->set('lingua', $request->getLocale());
    }

    return array();

}

in the other bundle of FOSUserBundle that have been overwritten

UserBundle/Controller/RegistrationController.php (for example)


public function customAction() {

    /**
     * Store it into session the user language
     */
    $sessionId  = $this->container->get("session");
    if($sessionId->get("lingua")==""){
        $this->container->get("session")->set('lingua',  $this->container->get('request')->getLocale());
    }

    return array(

    );
}

Then i create a service like: http://symfony.com/doc/current/cookbook/session/locale_sticky_session.html

services.xml

services:
    acme_locale.locale_listener:
        class: Acme\LocaleBundle\EventListener\LocaleListener
        arguments: ["%kernel.default_locale%",@session]
        tags:
            - { name: kernel.event_subscriber }

LocaleListener



namespace Acme\LocaleBundle\EventListener;

use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session;


class SnLocaleListener implements EventSubscriberInterface
{
    private $defaultLocale;
    private $session;


    public function __construct($defaultLocale = 'it', $session)
    {
        $this->defaultLocale = $defaultLocale;
        $this->session = $session;
    }

    public function onKernelRequest(GetResponseEvent $event)
    {
        $request = $event->getRequest();
        if (!$request->hasPreviousSession()) {
            return;
        }

        //incapsula la lingua in sessione se esiste
        $locale_session = $this->session->get('lingua');

        if ($locale_session == "") {

            // prova a vedere se il locale sia stato impostato come parametro _locale di una rotta
            if ($locale = $request->attributes->get('_locale')) {
                $request->getSession()->set('_locale', $locale);
            } else {
                // se non trova un locale esplicito in questa richiesta, usa quello della sessione
                $request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
            }

        } else {
            $request->setLocale($request->getSession()->get('_locale', $locale_session));
            $request->getSession()->set('_locale', $locale_session);
            $request->attributes->set('_locale', $locale_session);
        }


    }

    public static function getSubscribedEvents()
    {
        return array(
            // deve essere registrato prima dell'ascoltatore predefinito di locale
            KernelEvents::REQUEST => array(array('onKernelRequest', 17)),
        );
    }
}

sorry for my english is not perfect .. This solution for me works, It will not be the best