symfony 2.8 locale重置每个请求

Example:

I am trying to set locale to English:

public function innerPageAction(Request $request, $slug)
{        
    $request->setLocale('en');
    return $this->render('@App/Front/Default/inner.html.twig', [
        ...
    ]);
}

Then I get back to homepage

public function indexAction(Request $request)
{
    $locale = $request->getLocale();
    return $this->render('@App/Front/Default/home.html.twig', [
        'locale' => $locale,
    ]);
}

And this returns my default locale ('lt').

My config file looks like:

parameters:
    locale: lt
framework:
    default_locale:  "%locale%"

I am trying to make locale sticky as it is described here: http://symfony.com/doc/current/session/locale_sticky_session.html but it seems that its setting locale always to defaultLocale. Why?

How I can achieve that when im moving from inner page to homepage, it would return "en" not default locale "lt"?

$request->setLocale('en');

is only temporary meaning it is not persisted, so when you visit another page the request locale takes the default value from the configuration file, if you follow this link (same link that you provided)

class LocaleListener implements EventSubscriberInterface 
{
    private $defaultLocale;

    public function __construct($defaultLocale = 'en')
    {
        $this->defaultLocale = $defaultLocale;
    }

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

        // try to see if the locale has been set as a _locale routing parameter
        if ($locale = $request->attributes->get('_locale')) {
            $request->getSession()->set('_locale', $locale);
        } else {
            // if no explicit locale has been set on this request, use one from the session
            $request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
        }
    }

    public static function getSubscribedEvents()
    {
        return array(
            // must be registered after the default Locale listener
            KernelEvents::REQUEST => array(array('onKernelRequest', 15)),
        );
    }
}

the onKernelRequest method is executed on every request, if the locale was set in session it is applied by using $request->setLocale($locale), this way you'll have a 'sticky' locale on every page.

In your event listener's (or subscriber's) onKernelRequest need an extra line:

    // try to see if the locale has been set as a _locale routing parameter
    if ($locale = $request->attributes->get('_locale')) {
        $request->getSession()->set('_locale', $locale);
        $request->setLocale($locale); // seems that it's SIGNIFICANT
    } else {
        // if no explicit locale has been set on this request, use one from the session
        $request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
    }