Description
Symfony version(s) affected: 4.3.8
Description
There is a problem with the generated URL with internationalized routes in case you want to generate the URL for a route X and giving a locale different from the one in the current request object.
I defined the following route
* @Route(
* {
* "fr-be": "/{_locale}/cartes/parrainage",
* "nl-be": "/{_locale}/kaarten/vriend-uitnodiging"
* },
* name="homepage",
* defaults={"_locale": "fr-be"},
* requirements={"_locale": "fr-be|nl-be"}
* )
which gives me these two routes:
homepage.fr-be ANY ANY ANY /{_locale}/cartes/parrainage
homepage.nl-be ANY ANY ANY /{_locale}/kaarten/vriend-uitnodiging
If I want to generate the URL with nl-be as locale (with fr-be as my current locale), I'll do the following call:
$urlGenerator->generate('homepage', ['_locale' => 'nl-be']);
This call will return: /fr-be/kaarten/vriend-uitnodiging
which is not the result I want (should be /nl-be/kaarten/vriend-uitnodiging) since the locale given as parameter should override the one from the current RequestContext object injected to the UrlGenerator. (Am I wrong?)
Possible Solution
In CompiledUrlGenerator we have the following lines:
if (($this->compiledRoutes[$name.'.'.$locale][1]['_canonical_route'] ?? null) === $name) {
unset($parameters['_locale']);
$name .= '.'.$locale;
break;
}
If I understood correctly, if the 'homepage.nl-be' route exists, we unset the _locale index.
(Which means I can't define my route as shown above but have to explicitly define the locale in each path (i.e: "fr-be": "/fr-be/cartes/parrainage") to avoid the {_locale} parameter being replaced by the locale from the current request.
If I remove the unset instruction (and only keep the concat for the name), these 3 calls return the correct result:
$urlGenerator->generate('homepage', ['_locale' => 'fr-be']); "/fr-be/cartes/parrainage"
$urlGenerator->generate('homepage', ['_locale' => 'nl-be']); "/nl-be/kaarten/vriend-uitnodiging"
$urlGenerator->generate('homepage'); "/fr-be/cartes/parrainage"
In case this isn't a bug, why do we unset the _locale index?