Skip to content

[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

Merged
merged 1 commit into from
Jan 4, 2020

Conversation

fancyweb
Copy link
Contributor

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.

@@ -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'])) {
Copy link
Contributor Author

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'));
Copy link
Contributor Author

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).

@@ -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']);
Copy link
Contributor Author

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.

Copy link
Member

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?

Copy link
Contributor Author

@fancyweb fancyweb Dec 26, 2019

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']);
Copy link
Contributor Author

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.

@fancyweb fancyweb changed the title [Routing] Fix i18n routing when the url contains the locale [WIP][Routing] Fix i18n routing when the url contains the locale Dec 24, 2019
@chalasr chalasr added this to the 4.3 milestone Dec 24, 2019
@fancyweb fancyweb changed the title [WIP][Routing] Fix i18n routing when the url contains the locale [Routing] Fix i18n routing when the url contains the locale Dec 26, 2019
@nicolas-grekas
Copy link
Member

Thank you @fancyweb.

nicolas-grekas added a commit that referenced this pull request Jan 4, 2020
…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
@nicolas-grekas nicolas-grekas merged commit cd40bb8 into symfony:4.3 Jan 4, 2020
@fancyweb fancyweb deleted the routing-fix-i18n branch January 4, 2020 12:57
This was referenced Jan 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants