0
votes

I'm trying to create language/locale switch urls using Symfony2 and twig which will allow to switch language for route without resetting url:

http://example.com/en/cat/test
http://example.com/it/cat/test
http://example.com/sp/cat/test

... and so on

Here is revelant part of my twig template:

<a href="{{ path(app.request.attributes.get('_route'),{'_locale':'en'})}}">English</a>

My problem is that for example using category url(http://example.com/en/cat/test) gives me exception:

The "_category" route has some missing mandatory parameters ("category").

Which is all okay and understandable since for switcher urls I'm passing only _locale param.

How to pass all current url params?

1
Can you post the file where you define your routes?cheesemacfly

1 Answers

4
votes

You can get all request attributes from app.request and override part of them.

<a href="{{ path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')|merge({'_locale':'en'}))}}">