Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translating Drupal 8 Custom Route Paths with Parameters

I have the following route

my_module.order_details:
    path: '/account/orders/{orderId}'

Is there any way in Drupal 8 that I can translate this route path once?

For all the other routes I have been getting away with adding one url alias in the language I require a translation in. However, because this has the parameter {orderId} that doesn't appear to work, and I can't find a way to add a wild card into url aliases.(which would sort my problem, I think)

I know I could potentially create a translated url alias for each orderId but I would like to avoid that if possible.

Thanks

like image 555
Shamrockonov Avatar asked Nov 07 '22 16:11

Shamrockonov


1 Answers

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)

like image 90
zanvidmar Avatar answered Dec 28 '22 06:12

zanvidmar