twigsymfonysymfony-3.1

Symfony3 Language switch in Twig


Gretings, I have a problem with changing languages from Twig template. My goal is to make it possible for user to switch page language according to his needs. I did everything according instruction: Making the Locale "Sticky" during a User's Session

My question is, how to change language, which is stored in session, from Twig template?

My best guess was:

{{ app.session.set('_lang', 'en') }}

But result was, that language change needed page to be refreshed twice to show results, one to set values in session, second to load page according language stored in session. Please advise!


Solution

  • Solution was creating locale prefixes for all controllers and as malcolm offered, using route params:

    {{ path(app.request.attributes.get('_route'), app.request.query.all|merge({'_locale': 'en'})) }}