Home > Back-end >  It is possible to auto translate routing in Symfony 5?
It is possible to auto translate routing in Symfony 5?

Time:10-11

I have on my controller the following route:

#[Route('/{_locale<en>}/profile', name: 'profile_en', methods: 'GET')]
#[Route('/{_locale<es>}/perfil', name: 'profile_es', methods: 'GET')]

It is possible to have all my urls translateds (in this case profile) in a file and do something like that for example?

#[Route('/{_locale<%app.supported_locales%>}/profile', name: 'profile', methods: 'GET')]

This should give me all possible locales profile url translated

CodePudding user response:

Since symfony 4.1 they have internationalized routes (a.k.a. Localized Routes)

Quick-preview:

# config/routes.yaml
about_us:
    path:
        en: /about-us
        nl: /over-ons
    controller: App\Controller\CompanyController::about

Since you already use _locale and in your case with php-attributes it should be:

#[Route(path: [
        'en' => '/profile',
        'es' => '/profil'
    ], name: 'profile', methods: 'GET')]

I think, that's exactly what you're looking for. But untested, since I have no way to play with php-8 right now

P.S. You could also rename your route to profile_i18n so each time you (or other devs) use it, you immediately know → it's an internationalized one

  • Related