Skip to content
Advertisement

Manually switch _locale in symfony 4

I’m absolutely stuck in getting a solution to manually switch the _locale variable in Symfony 4.

I followed these steps, but now I have absolutely no idea how to make a simple switch button in the nav section. I also took a look a this question, but this seems to be an older Symfony version..

Can anyone please help me climb out of this dark hole and explain to me how I can integrate a simple _locale switch button, or at least point me in the right direction?

Advertisement

Answer

The answer is slightly different from this answer which is not applicable in Symfony 4. Start with editing the services.yaml file in the config directory.

JavaScript

Then add a template to integrate the switch button somewhere in your base template.

JavaScript

And finally integrate this brandnew template in your base template.

JavaScript

EDIT for Symfony 4.3.4+

As per the answer of Charles beneath, the locales value in services.yaml file should be inserted with quotes to avoid an unvalid YAML error:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement