K. Maliszewski
K. Maliszewski

Reputation: 333

Switch locale in URL with different arguments on Symfony2

I have 3 links to switch locale in Symfony2.

 <a href="{{ path( app.request.get('_route'), {'_locale': 'pl'}) }}"><img alt="Polski" class="flags" src="{{ asset('bundles/gogscms/images/pol.png') }}"></a>
 <a href="{{ path( app.request.get('_route'), {'_locale': 'en'}) }}"><img alt="English" class="flags" src="{{ asset('bundles/gogscms/images/eng.png') }}"></a>
 <a href="{{ path( app.request.get('_route'), {'_locale': 'it'}) }}"><img alt="Italiano" class="flags" src="{{ asset('bundles/gogscms/images/ita.png') }}"></a>

Everything is good, when I don't have any additional argument in Controlers. E.g ( pattern: /{_locale} )

public function indexAction(){

...

    }

Unfortunally, when I have some additional argument (e.g: pattern: /page/{id}/{name}/{_locale} ) I get error in these links

route has some missing mandatory parameters

Anyone has got any idea how should look like correct link? This should work always - when there is no arguments and when is n or n + 1 arguments.

Or maybe there is another way to change the language?

Upvotes: 1

Views: 76

Answers (1)

BENARD Patrick
BENARD Patrick

Reputation: 30975

You can find this in the app.request.attributes.

If you don't know them :

{{path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')|merge ({'_locale':'pl' }))  }}

Upvotes: 1

Related Questions