3
votes

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.

2
Do you mean "how to generate fallback to english language"?qdequippe
That is one possible solution, so sure.Jeroen De Dauw
I did. It does not show routing fallback from languages not defined at all for a route. Even so I tried to adapt this to routes.yml syntax (so no controller annotations) and got the same errors I got before posting here on SO.Jeroen De Dauw
I believe you should provide example twig code that actually creates a problem. It's not on the question, and may be part of the reason you are not getting useful answers.yivi

2 Answers

2
votes

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(){...}
2
votes

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...