0

I have the following path defined in my routing.yml:

path: '/department/{department_node}/qualifications/{qualification_id}'

I need to translate that route into other languages, for example into Spanish:

path: '/departamento/{department_node}/titulaciones/{qualification_id}'

department_node would be a "department" node

qualification_id would be a dynamic id from a web service

So the result should be:

en: /department/department-title/qualifications/99
es: /departamento/department-title/titulaciones/99

As the qualification_id is a dynamic value, I can't create an alias, as I would have many after a while and they would be out of date. Does anyone know any method of translating the routes in this case?

1
  • @Hudri is right, you can't translate a route. The language specific things are handled in path processing, for which core aliasing is not the only one way to do this, you could use a custom path processor to translate parts of the path. BTW I think the easiest way would be to move the dynamic part to a query parameter.
    – 4uk4
    Commented Jun 29, 2020 at 13:23

1 Answer 1

2

Routes != URLs, you can't translate a route, you only can translate URL aliases.

One route has only one path, which - through optional wildcards - could match more than one URL. Because no single part in your desired URLs is stable, you can't do this with one route. (And AFAIK the first /part/ of a route path must be constant too in Drupal, it cannot start with a wildcard.)

An URL alias is an optional, separate entity that matches an arbitrary URL to an existing route.

You either have to dynamically create/update URL aliases, or add new route only for the spanish translation. But it is allowed to reuse the same controller for multiple routes, so overhead would only be a single entry in a routing.yml file.

4
  • I understand, so if I want to add a new route for Spanish, I should also control the aliases, because otherwise I would get different results when using the language selector
    – Javier Rey
    Commented Jun 29, 2020 at 12:01
  • Don't know your requirements, but in this case I guess I wouldn't use URL aliases at all. Making the controller language-aware is IMHO much simpler than constantly creating/updating URL aliases. You have got two routes, with two wildcards each, leading to one controller, who has to serve the proper entity in the proper language.
    – Hudri
    Commented Jun 29, 2020 at 12:08
  • Yes, the problem is that when using the controllers, the department should be an id, and not the name, which is what I'm sending right now cause it's an alias
    – Javier Rey
    Commented Jun 29, 2020 at 12:42
  • Yes, automatic entity upcasting only works with numeric IDs. But you also can implement custom logic in your controller and manually load the corresponding department node using the string/slug variable from the route. Something like entityQuery->condition('field_foo', $variableNameFromRoute)
    – Hudri
    Commented Jun 29, 2020 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.