0

I'm trying to make a two language app with symfony2. I'm using Doctrine behavior and A2lixtranslationformbundle. I have a listener which listen to change the locale:

namespace George\CoreBundle\EventListener;

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

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')) {
    if ($locale = $request->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 before the default Locale listener
        KernelEvents::REQUEST => array(array('onKernelRequest', 17)),
    );
}
}

In the twig template i want to retrieve the translation trough the session param to see what locale we have:

{{ entity.translate(app.session.get('_locale')).title }}

But the app.session.get('_locale') does not return nothing. What is the problem why the session in the twig do not get this property i have test it in the listener everything looks fine.

  • {{ entity.property|trans }} is the normal use of translate.. Why do you have to trigger translate () with setting the locale inisde your view? Those things should be set/made before any view is loaded.. – Svetoslav Nov 26 '15 at 13:37

1 Answers1

1

In order to retrive the locale in twig you can use the following snippet

{{ app.request.locale }}

for your case will be

{{ entity.translate(app.request.locale).title }}
Alexandru Olaru
  • 6,842
  • 6
  • 27
  • 53
  • ok but what is the problem with the session why i cannot get the '_locale' if i make it in the listener $request->getSession()->set('locale2', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaa'); and print {{app.session.get('locale2')}} everything is ok but when is '_locale' cannot work i don not understand what is the problem – George Plamenov Georgiev Nov 26 '15 at 13:40
  • 1
    You can access it, but keep in mind `{{ app.session }}` from twig does not equal `$_SESSION[]` so make sure you are posting the right path to the locale it could be somewhat ['attribute']['_locale']. Use in your twig `{{ dump(app.session) }}` to see where the locale is stored. – Alexandru Olaru Nov 26 '15 at 13:52
  • no i understand that this is the case but the object $request->getSession()->set('_locale', $locale); is it equal to the {{ app.session }} – George Plamenov Georgiev Nov 26 '15 at 13:54
  • To be faire I did not checked but from logical point it should be a `singleton`, same instance. – Alexandru Olaru Nov 26 '15 at 13:57
  • Yes they are. For some reason i did not get it and in the all unknown i decided to ask the crowd. Thank you guys! – George Plamenov Georgiev Nov 26 '15 at 16:13
  • when we use `{{ app.request.locale }}` where it referring to get the locale value? – mapmalith Nov 01 '16 at 10:39