Link to index with default {_locale} parameter
-
13-12-2019 - |
Question
I have a language selector in my project to redirect to index page with the locale parameter {_locale}, but when the link is to default locale seted in routing.yml, the link looks different. This is my code:
I generate the link in code:
<a href="{{ path('ProjectBaseBundle_index', {'_locale': country.idlang}) }}">
...
</a>
And here is the routing.yml:
ProjectBaseBundle_index:
pattern: /{_locale}
defaults: { _controller: ProjectBaseBundle:Default:index, _locale: es }
requirements:
_locale: en|fr|de|es
This generate propertly the routes for all languages, buy for the default one looks different:
http://project.dev/app_dev.php/en
http://project.dev/app_dev.php/fr
http://project.dev/app_dev.php/de
http://project.dev/app_dev.php/?_locale=es
es is our default language code. I'm using Symfony 2.1
Anyone know How to generate identical route for the default lang?
Solution 2
I just discovered that it makes sense, because the parameter {_locale} does not exists for the default language, so the system appended the parameter as a GET parameter.
I found a workaround:
ProjectBaseBundle_default:
pattern: /{_locale}
defaults: { _controller: ProjectBaseBundle:Default:index , _locale: %locale% }
ProjectBaseBundle_index:
pattern: /{_locale}
defaults: { _controller: ProjectBaseBundle:Default:index }
requirements:
_locale: en|fr|de|es
and I have opened a issue in Symfony board: https://github.com/symfony/symfony/issues/5135
OTHER TIPS
It happens because of conditions for requirements and provided parameters in UrlGenerator
- unknown parameters passed through the arguments array are automatically appended at the end of the URL as GET parameters.
Did you try to put default locale in config.yml
instead of having it in defaults
section of route?
Symfony-2.0:
framework:
session: { default_locale: es }
Symfony-2.1:
framework:
default_locale: es