phpsymfonyroutesinternationalizationtwig

Symfony2-Rookie: Setting Path dynamically


ok,i managed to put code for my testpage to switch languages in combination with JMSTranslationBundle like this in twig

<li>
<a href="{{ path("main", {"_locale": "en","name": name}) }}">
<img src="{{ asset('img/flags/gb.png') }}"></a></li>
<li>
<a href="{{ path("main", {"_locale": "de","name": name}) }}">
<img src="{{ asset('img/flags/de.png') }}"></a></li>

but this will be working for the path ("main") how can i make it dynamically work for the page/route i am currently working on, including needed parameter (in this case "name": name ? so if i am currently on english page of "about us", i can automatically switch to german page of about us, including its parameters? is it possible? or do i have to hardcode each twig page/template with the paths?


Solution

  • This describes an extension exactly like you are looking for: http://blog.viison.com/post/15619033835/symfony2-twig-extension-switch-locale-current-route

    The ScopeWideningInjectionException can be fixed by injecting the @service_container to retrieve the router and request instead of injecting them directly.