-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Routing] Fix i18n routing when the url contains the locale #35101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
8d1a488
to
9c08a1c
Compare
@@ -53,6 +52,16 @@ public function generate($name, $parameters = [], $referenceType = self::ABSOLUT | |||
|
|||
list($variables, $defaults, $requirements, $tokens, $hostTokens, $requiredSchemes) = $this->compiledRoutes[$name]; | |||
|
|||
if (isset($defaults['_canonical_route']) && isset($defaults['_locale'])) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This if = we are in i18n routing. Checking isset($defaults['_locale']) is a safety.
$this->routeCollection->add('test.en', (new Route('/testing/is/fun'))->setDefault('_canonical_route', 'test')); | ||
$this->routeCollection->add('test.nl', (new Route('/testen/is/leuk'))->setDefault('_canonical_route', 'test')); | ||
$this->routeCollection->add('test.fr', (new Route('/tester/est/amusant'))->setDefault('_canonical_route', 'test')); | ||
$this->routeCollection->add('test.en', (new Route('/testing/is/fun'))->setDefault('_locale', 'en')->setDefault('_canonical_route', 'test')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a route is i18n, it always has a default _locale. The test must reflect that (same for the other ones).
src/Symfony/Component/Routing/Tests/Generator/Dumper/CompiledUrlGeneratorDumperTest.php
Show resolved
Hide resolved
src/Symfony/Component/Routing/Tests/Generator/Dumper/CompiledUrlGeneratorDumperTest.php
Show resolved
Hide resolved
@@ -40,7 +40,6 @@ public function generate($name, $parameters = [], $referenceType = self::ABSOLUT | |||
if (null !== $locale) { | |||
do { | |||
if (($this->compiledRoutes[$name.'.'.$locale][1]['_canonical_route'] ?? null) === $name) { | |||
unset($parameters['_locale']); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not needed anymore because it is handled by the new code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wouldn't it be OK to do $parameters['_locale'] = $locale
here, and remove the other changes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No because it's not enough to handle the case where the generator locale is "fr" + you just ask for the "foo.en" route. In this case the locale remains "fr".
$parameters['_locale'] = $defaults['_locale']; | ||
} | ||
} else { | ||
unset($parameters['_locale']); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We always unset the _locale parameter if the url does not have the _locale variable because the good i18n route has already been found and we don't want the parameter.
src/Symfony/Component/Routing/Tests/Generator/Dumper/CompiledUrlGeneratorDumperTest.php
Show resolved
Hide resolved
src/Symfony/Component/Routing/Generator/CompiledUrlGenerator.php
Outdated
Show resolved
Hide resolved
9c08a1c
to
cd40bb8
Compare
Thank you @fancyweb. |
…e (fancyweb) This PR was merged into the 4.3 branch. Discussion ---------- [Routing] Fix i18n routing when the url contains the locale | Q | A | ------------- | --- | Branch? | 4.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | #34469 | License | MIT | Doc PR | - This PR fixes different scenarios with i18n routing. Commits ------- cd40bb8 [Routing] Fix i18n routing when the url contains the locale
This PR fixes different scenarios with i18n routing.