Skip to content
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

[Routing] Document the effect of setting locale on a route with locale prefixes #19825

Merged
merged 1 commit into from
Jun 21, 2024

Conversation

nicwortel
Copy link
Contributor

When using localized routes and importing a collection of routes, the imported routes can be prefixed with a different prefix for each locale (as documented in Localized Routes (i18n)). This makes it easy to have localized routes for /en/about-us and /nl/over-ons without having to define multiple routes.

This behavior is implemented in the
Symfony\Component\Routing\Loader\Configurator\Traits\PrefixTrait by replacing the original route with a cloned version for each locale prefix.

However, any route which has a default value for the _locale parameter will only be prefixed with the prefix for that locale and will not be cloned for the other locale prefixes.

I stumbled upon this because I was trying to implement a route which should be available only for a single locale. I couldn't find any mention of this behavior in the routing documentation.

routing.rst Outdated
is set on any of the imported routes, that route will only be available
with the prefix for that locale. This is useful when you want to import
a collection of routes which contains a route that should only exist
in one of the locales.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about adding a code example instead? This could make the effect more clear (and we can still explain it with an inline comment)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I considered that, but as it makes the tip bigger I thought it would distract too much from the main content.

But I could indeed add an example of a route that defines the locale parameter. Do you think I should use only an attribute as an example (as the YAML, XML and PHP examples in the block above all import the Controller directory with type: attribute)? Or should I include examples in YAML, XML & PHP as well?

The example for using attributes could look something like this:

class EnglishArticleController extends AbstractController
{
    #[Route(path: '/articles/english-only', locale: 'en')]
    public function display(): Response
    {
    }
}

Although I need to think about some better class / method / route names to come up with a more real-life example as per the documentation standards.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I often use the "soccer" / "football" thing to illustrate the "default prefix expect one locale"

  • football (default)
  • us : soccer

In the opposite situation, i guess you could use services in ecommerce that are often restricted to the country where the shop operates, like "express-delivery", cashback options, .. etc ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added an example with annotations and with attributes. I'm not sure if I should add examples for YAML, XML and PHP as well?

@javiereguiluz javiereguiluz merged commit b3d272d into symfony:5.4 Jun 21, 2024
3 checks passed
@javiereguiluz
Copy link
Member

Thanks Nic! This is finally merged.

Note that while merging I did some changes:

  • I removed the route definition example because it looked confusing to have it (it's just a normal route definition)
  • I reworded a bit the explanation to show an example (without code) similar to the example that you created: an English only route that is not imported for nl prefix, only for en

@nicwortel nicwortel deleted the locale_prefix_behavior branch June 21, 2024 16:49
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.

None yet

5 participants