Example of route translation with dynamic routes:
your_module.routing.yml
route_callbacks:
- '\Drupal\your_module\DynamicRoutes\DynamicRoutes::routes'
your_module/src/DynamicRoutes/DynamicRoutes.php
<?php
namespace Drupal\your_module\DynamicRoutes;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
/**
* Listens to the dynamic trousers route events.
*/
class DynamicRoutes {
public function routes(){
$route_collection = new RouteCollection();
$route_lang_en = new Route(
// path
'/example-lang-en',
// defaults
[
// example controller
'_controller' => '\Drupal\system\Controller\SystemController::systemAdminMenuBlockPage',
'_title' => 'Your title en'
],
// requirements:
[
'_permission' => 'access content',
]
);
$route_collection->add('example.language_en', $route_lang_en);
$route_lang_fr = new Route(
'/example-lang-fr',
[
'_controller' => '\Drupal\system\Controller\SystemController::systemAdminMenuBlockPage',
'_title' => 'Your title fr'
],
['_permission' => 'access content']
);
$route_collection->add('example.language_fr', $route_lang_fr);
return $route_collection;
}
}
This function is equivalent for:
example.language_en:
path: '/example-lang-en'
defaults:
_controller => '\Drupal\system\Controller\SystemController::systemAdminMenuBlockPage',
_title => 'Your title en'
requirements:
_permission: 'access content'
example.language_fr:
path: '/example-lang-fr'
defaults:
_controller => '\Drupal\system\Controller\SystemController::systemAdminMenuBlockPage',
_title => 'Your title fr'
requirements:
_permission: 'access content'
The code aboce is for explanation only. However I suggest that routes are build whit some custom reusable method that iterate trough all languages, with custom translation for path
and _title
, while _controller
, '_permission'
and any other untranslatable data is reused in each route translation.
For debugging routes drupal console is very useful
drupal dr
(list all routes)
drupal dr example.language_en
(get en example route parameters)
drupal dr example.language_fr
(get fr example route parameters)