3

I have a few dozen routes defined as follows:

services:
    path:
        en: /en/services
        de: /de/dienstleistungen
    controller: App\Controller\SimplePageController::page

This is using the Localized routing which I believe was introduced in Symfony 4.1.

The routes all have paths for both English and German. Now I want to translate a few specific pages in some other languages. For instance my homepage. The problem is that if I add another language to the route of my homepage, it will not load since it can't create links to other pages that do not have a route defined in this new language.

If I visit /nl/homepage, the following link generation in Twig fails

<link href="{{ path('services') }}">

with the error

Unable to generate a URL for the named route "services" as such route does not exist.

How can I tell Symfony to create links to the English routes if the current language does not have a route for it? Or even better, specify some dynamic path along the lines of:

services:
    path:
        en: /en/services
        de: /de/dienstleistungen
        default: /$1/services
    controller: App\Controller\SimplePageController::page

Edit: I am now using Symfony 5.0.4 and still have the same problem.

Jeroen De Dauw
  • 10,321
  • 15
  • 56
  • 79

2 Answers2

2

Isn't this is what you are looking for ?

# config/packages/translation.yaml
framework:
    translator:
        fallbacks: ['en']

It will tell you if the language isn't defined to use the 'en' as default fallback.

EDIT: Then you might try to use some kind of workaround like this but not sure if this is what you are looking for

# config/services.yaml
parameters:
    app.localesForSpecificRouting: en|de
    app.localesForAnotherSpecificRouting: en|de|fr

Then in your controller you could load specific languages for specific routes

/**
 * @Route("/{_locale}/routing1", name="routing1", requirements={
 *     "_locale"="%app.localesForSpecificRouting%"
 * })
 */
 public function routing1(){...}

 /**
 * @Route("/{_locale}/routing2", name="routing2", requirements={
 *     "_locale"="%app.localesForAnotherSpecificRouting%"
 * })
 */
 public function routing2(){...}
Kurano
  • 72
  • 9
  • We have such a fallback already. This works for regular messages but as far as I can tell it has no impact on routing. – Jeroen De Dauw Feb 26 '20 at 04:28
  • @JeroenDeDauw I edited to add an other idea for you. But it's not the exact same way that you are using. – Kurano Feb 26 '20 at 12:34
2

I'm not sure I understood the problem correctly, but I tested this code locally:

# config/routes.yaml
routing-test:
  path:
    en: /{_locale}/services
    de: /{_locale}/dienstleistungen
  controller: App\Controller\SimplePageController::page
  defaults:
    _locale: '%kernel.default_locale%'
  requirements:
    _locale: ^[a-z]{2}?$

This code results matched URLs, for example :

URL typed: /nl/services 

Matched: routing-test.en /{_locale}/services 
_locale: "nl"

One another test :

URL typed: /fr/dienstleistungen

Matched: routing-test.de /{_locale}/dienstleistungen 
_locale: "fr"

Could you tell me if it helped you ?

EDIT:

Ok, I think I understood a little better, What I tried :

Routing:

# config/routes.yaml
homepage:
    path:
       en: /{_locale}/homepage
       fr: /{_locale}/accueil
    controller: App\Controller\SimplePageController::index
    defaults:
      _locale: '%kernel.default_locale%'
    requirements:
      _locale: ^[a-z]{2}?$

services:
    path:
       en: /{_locale}/services
       de: /{_locale}/dienstleistungen
    controller: App\Controller\SimplePageController::services
    defaults:
      _locale: '%kernel.default_locale%'
    requirements:
      _locale: ^[a-z]{2}?$

Templating:

{# templates/test-routing/index.html.twig #}
{% set locale = app.request.get('_locale') in ['en', 'de'] ? app.request.get('_locale') : 'en' %}
<a href="{{ path('services', { '_locale' : locale }) }}">Routing test</a>

I don't think there is any better way to achieve this goal, not to my knowledge...

  • The issue is not getting the routing itself to work. It is getting Symfony to create links to routes. I added emphasis to my post now. This twig code fails with your example when visiting /nl/whatever: {{ path('routing-test') }}. Error is: Unable to generate a URL for the named route "routing-test" as such route does not exist. – Jeroen De Dauw Feb 28 '20 at 05:14