JMSI18nRoutingBundle
JMSI18nRoutingBundle copied to clipboard
language switcher doc missing
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 %}
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 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')
Thanks Stof !!
Hi Stof. Thanks for the point. How would a language switcher could look like then?