JMSI18nRoutingBundle icon indicating copy to clipboard operation
JMSI18nRoutingBundle copied to clipboard

language switcher doc missing

Open Tii opened this issue 12 years ago • 4 comments

The documentation is missing a language switcher example, so here is one working for me with Symfony 2.0, symfony 2.1 can use _route_params and avoid using the first loop for filling "routeParams".

for the locals variable, I created it in parameters.ini like this :

locales[] = en locales[] = fr locales[] = nl locales[] = de

and then I loaded it in twig globals thru config.yml :

twig: globals: locales: %locales%

And now, just copy paste the following code where you want to see language switcher in you template

{# Language switcher #}
{% for locale in locales %}                                     
    <li><a href="{{ app.request.attributes.get("_route"), app.request.attributes.all|merge({"_locale": locale})) }}">{{ locale }}</a></li>
{% endfor %}

Tii avatar Aug 14 '12 17:08 Tii

There is a typo in your snippet, I think you forgot the call to the path() method. Plus, I would rather use app.request.query.all instead of app.request.attributes.all:

{# Language switcher #}
{% for locale in locales %}                                     
    <li><a href="{{ path(app.request.attributes.get("_route"), app.request.query.all|merge({"_locale": locale})) }}">{{ locale }}</a></li>
{% endfor %}

MattKetmo avatar Sep 25 '12 10:09 MattKetmo

@MattKetmo The attributes used when matching the route are in the request attributes, not in the query string. So you have to use them. Merging app.request.query.all with it allows to keep the query string, which is even better.

Btw, on Symfony 2.1, you can do better than using app.request.attributes.all (which contains too much stuff): use app.request.attributes.get('_route_params')

stof avatar Sep 25 '12 10:09 stof

Thanks Stof !!

bennyrock20 avatar Apr 25 '13 01:04 bennyrock20

Hi Stof. Thanks for the point. How would a language switcher could look like then?

pmpr avatar May 09 '17 16:05 pmpr