Symfony2 locale languages whole page event listener

kernel, listener, service, symfony, translation

Solution

Yes for some reason you need to use a listener:

<?php

namespace Your\Bundle\Listener;

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

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

  if ($locale = $request->attributes->get('_locale')) {
      $request->getSession()->set('_locale', $locale);
  } else {
      $request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
  }
  }

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

Register your listener in your service.xml:

<service id="my.listener" class="Your\Bundle\Listener\LocaleListener">
    <argument>%locale%</argument>
    <tag name="kernel.event_subscriber"/>
</service>

An example how to implement the language switcher in your twig template:

{% for locale in ['en', 'fr','zh'] %}
    <li>
      <a href="{{ path(app.request.get('_route'), app.request.get('_route_params')|merge({'_locale' : locale})) }}">
        {% if locale == 'en' %}
        <img title="English" src="{{ asset('bundles/fkmywebsite/images/UnitedStates.png') }}" alt="English" height="30" width="30"/>
        {% elseif locale == 'fr' %}
        <img title="Français" src="{{ asset('bundles/fkmywebsite/images/France.png') }}" alt="Français" height="30" width="30"/>
        {% endif %}
      </div>
    </li>
{% endfor %}

Problem

I need to create a system like the facebook lang system when a user clicks on language to example france('fr') the page will reload and all the content in messages.fr.yml will be displayed... I tried to make route like `/language/{localExtension}` but when I setLocale there and then redirected it didn't work... I don't know i assuming its just for certain page not global? Because when i set `setLocale('fr')` at the top of my controller it works... I found some articles when is using the `www.example.com/contact/en`, `/contact/fr` etc But i want `/contact` and content displayed from previous chosen language. Or default 'en' like now when user didn't change it... I was googling all day and i think it should be done with... service => listener and... on kernel.request? or something like that. Here are intresting link Symfony2 wrong locale detection? i think that's what i need? or? I tried to set service and create listener but some errors appear and i don't even know if this is the way how to create it :/

Original source

Related problems