Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Routing] Generate URL for internationalized routes with the _locale given as parameter getting removed #34469

Copy link
Copy link
Closed
@korhankale

Description

@korhankale
Issue body actions

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?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.