1

I'd like to implement locale switcher, but it seems with no luck...

The code below doesn't work because the (Referrer) contains the old value of locale...

How can I redirect to the old Referrer URI with a new value of locale?

-- routing.yml

hello:
  pattern:  /{_locale}/hello/{name}
  defaults: { _controller: JetInformBundle:Default:index, name: 'alexander' }
  requirements:
    _locale: ^en|de|ru|uk$

about:
  pattern:  /{_locale}/about
  defaults: { _controller: JetInformBundle:Default:about }
  requirements:
    _locale: ^en|de|ru|uk$

locale:
  pattern:  /locale/{locale}
  defaults: { _controller: JetInformBundle:Locale:index }

-- DefaultController.php

<?php

namespace Jet\InformBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;

class DefaultController extends Controller
{
    public function indexAction($name, Request $request)
    {
        $request->getSession()->set('referrer', $request->getRequestUri());
        return $this->render('JetInformBundle:Default:index.html.twig',
                             array('name' => $name));
    }

    public function aboutAction(Request $request)
    {
        $request->getSession()->set('referrer', $request->getRequestUri());
        return $this->render('JetInformBundle:Default:about.html.twig'));
    }
}

-- LocaleController.php

<?php

namespace Jet\InformBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;

class LocaleController extends Controller
{
    public function indexAction($locale, Request $request)
    {
        $session = $request->getSession();

        if ($request->hasSession())
            $session->setLocale($locale);

        return $this->redirect($session->get('referrer'));
    }
}

-- index.html.twig

{% extends '::base.html.twig' %}

{% block body %}
<h1>{% trans %}hello.name{% endtrans %} {{ name }}!</h1>
<h3>{% trans %}your.locale{% endtrans %} [{{ app.request.get('_locale') }}]</h3>

{% include 'JetInformBundle:Default:locales.html.twig' %}

<div>
    <p>{% trans%}return.to{% endtrans%} <a href="{{ path('about', { '_locale':   app.request.get('_locale') }) }}">About</a></p>
</div>
{% endblock %}

-- locales.html.twig

<div class="langs">
    <ul>
        <li>
            {% if app.request.get('_locale') == 'ru' %}
                Русский
            {% else %}
                <a href="{{ path('locale', { 'locale': 'ru' }) }}">Русский</a>
            {% endif %}
        </li>
        <li>
            {% if app.request.get('_locale') == 'en' %}
                English
            {% else %}
                <a href="{{ path('locale', { 'locale': 'en' }) }}">English</a>
            {% endif %}
        </li>
        <li>
            {% if app.request.get('_locale') == 'uk' %}
                Украiнська
            {% else %}
                <a href="{{ path('locale', { 'locale': 'uk' }) }}">Украiнська</a>
            {% endif %}
        </li>
        <li>
            {% if app.request.get('_locale') == 'de' %}
                Deutsch
            {% else %}
                <a href="{{ path('locale', { 'locale': 'de' }) }}">Deutsch</a>
            {% endif %}
        </li>
    </ul>
</div>
Sparkup
  • 3,686
  • 2
  • 36
  • 50
Alexander Vasilenko
  • 706
  • 1
  • 11
  • 24
  • Duplicated question: http://stackoverflow.com/questions/7687919/symfony2-language-selector/8387247 – wdev Sep 27 '12 at 13:33

4 Answers4

10

Let me show you my solution. I've written kernel event listener:

<service id="expedio.simple.listener" class="Expedio\SimpleBundle\Listener\Kernel">
      <tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" />
      <argument type="service" id="router" />
  </service>

like the following:

namespace Expedio\SimpleBundle\Listener;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;

class Kernel {

    /**
     * @var \Symfony\Component\DependencyInjection\ContainerInterface
     */
    private $router;

    public function __construct(\Symfony\Component\Routing\Router $router) {
        $this->router = $router;
    }

    public function onKernelRequest(GetResponseEvent $event) {
        if ($event->getRequestType() !== \Symfony\Component\HttpKernel\HttpKernel::MASTER_REQUEST) {
            return;
        }

        /** @var \Symfony\Component\HttpFoundation\Request $request  */
        $request = $event->getRequest();
        /** @var \Symfony\Component\HttpFoundation\Session $session  */
        $session = $request->getSession();

        $routeParams = $this->router->match($request->getPathInfo());
        $routeName = $routeParams['_route'];
        if ($routeName[0] == '_') {
            return;
        }
        unset($routeParams['_route']);
        $routeData = array('name' => $routeName, 'params' => $routeParams);

        //Skipping duplicates
        $thisRoute = $session->get('this_route', array());
        if ($thisRoute == $routeData) {
            return;
        }
        $session->set('last_route', $thisRoute);
        $session->set('this_route', $routeData);
    }
}

It just saves last request route data each time user opens a page. And in controller when user wants to change locale I do this:

/**
 * @Route("/setlocale/{locale}", name="set_locale")
 * @param string $locale
 * @return array
 */
public function setLocaleAction($locale) {
    /** @var \Symfony\Component\HttpFoundation\Session $session  */
    $session = $this->get('session');
    $session->setLocale($locale);
    $last_route = $session->get('last_route', array('name' => 'index'));
    $last_route['params']['_locale'] = $locale;
    return ($this->redirect($this->generateUrl($last_route['name'], $last_route['params'])));
}
Vladislav Rastrusny
  • 29,378
  • 23
  • 95
  • 156
  • This is very nice! Have things improved in Symfony2.1? Do you know if this is implemented already? This is a beautiful post. – Mick Jul 31 '12 at 17:20
3

I provide my solution using the approach with

$request->headers->get('referer');

as suggested by gilden

/**
 * Your locale changing controller
 */
public function localeAction($locale)
{
    $request = $this->get('request');
    $router = $this->get('router');
    $context = $router->getContext();
    $frontControllerName = basename($_SERVER['SCRIPT_FILENAME']);

    if($request->hasSession())
    {
        $session = $request->getSession();
        $session->setLocale($locale);
        $context->setParameter('_locale', $locale);

        //reconstructs a routing path and gets a routing array called $route_params
        $url = $request->headers->get('referer');
        $urlElements = parse_url($url);
        $routePath = str_replace('/' . $frontControllerName, '', $urlElements['path']); //eliminates the front controller name from the url path
        $route_params = $router->match($routePath);

        // Get the route name
        $route = $route_params['_route'];

        // Some parameters are not required to be used, filter them
        // by using an array of ignored elements.
        $ignore_params = array('_route' => true, '_controller' => true, '_locale' => true);
        $route_params = array_diff_key($route_params, $ignore_params);

        $url = $this->get('router')->generate($route, $route_params);

        return $this->redirect($url);
    }
}
eux
  • 71
  • 1
  • 7
1

You must store an array of your route attributes in the session instead of a single url.

/**
 * You should set the 'referrer' in every controller in your application. This
 * should probably be handled as an event to save all the hassle.
 */
public function anyAction(Request $request)
{
    $request->getSession()->set('referrer', $request->attributes->all());

    // ...
}

/**
 * Your locale changing controller
 */
public function localeAction($locale, Request $request)
{
    if($request->hasSession())
    {
        $session = $request->getSession();
        $session->setLocale($locale);

        $route_params = $session->get('referrer');

        // Get the route name
        $route = $route_params['_route'];

        // Some parameters are not required to be used, filter them
        // by using an array of ignored elements.
        $ignore_params = array('_route' => true, '_controller' => true);
        $route_params = array_diff_key($route_params, $ignore_params);

        $url = $this->get('router')->generate($route, $route_params);
        return $this->redirect($url);
    }
}

For future reference and to anyone stumbling over this: you don't have to store the referrer attribute in your session by setting it in EVERY controller. You can retrieve the previous url from the headers property:

$request->headers->get('referer'); // Single 'r' everywhere!

For additional info, consult:

  • Symfony2 Request class on github
  • Symfony2 ParameterBag class on github
hakre
  • 193,403
  • 52
  • 435
  • 836
kgilden
  • 10,336
  • 3
  • 50
  • 48
0

Gilden, thanks a lot for idea, but it's doesn't work anyway...

Here is the controller LOCALE SWITCHER:

class LocaleController extends Controller
{
public function changeAction($locale, Request $request)
{
    if ($request->hasSession())
    {
        $session = $request->getSession();
        $session->setLocale($locale);

        $route_params = $session->get('jet_referrer');
        $route_name = $route_params['_route'];

        // Some parameters are not required to be used, filter them
        // by using an array of ignored elements.
        $ignore_params = array('_route' => true, '_controller' => true,
                               '_template_default_vars' => true);
        $route_params = array_diff_key($route_params, $ignore_params);

        $url = $this->get('router')->generate($route_name, $route_params);
        return $this->redirect($url);
    }
}
}

Here is the controller of BUSINESS LOGIC:

/**
 * @Route("/{_locale}")
 */
class TestController extends Controller
{
/**
 * @Route("/", name="_test")
 * @Template()
 */
public function indexAction(Request $request)
{
    $request->getSession()->set('jet_referrer', $request->attributes->all());
    return array();
}

/**
 * @Route("/hello/{name}", name="_test_hello")
 * @Template()
 */
public function helloAction($name, Request $request)
{
    $request->getSession()->set('jet_referrer', $request->attributes->all());
    return array('name' => $name);
}
}

Regarding IGNORED params for array_key_diff()...

_controller
_locale
_route
_template
_template_default_vars
_template_vars

I've tried to include few of them for the new route generation, but with no luck... Don't understand what is going on...

It's seems just a hack... Do we have more sofisticated way to realize this simple feature in Symfony2 ?? RAW PHP allows me to do that much & much more easily...

Thanks again.

Alexander Vasilenko
  • 706
  • 1
  • 11
  • 24