使用参数翻译 Drupal 8 自定义路由路径

Sha*_*nov 5 routing translate drupal-8

我有以下路线

my_module.order_details:
    path: '/account/orders/{orderId}'
Run Code Online (Sandbox Code Playgroud)

在 Drupal 8 中有什么方法可以翻译这条路由路径吗?

对于所有其他路由,我一直在使用我需要翻译的语言添加一个 url 别名。但是,因为它的参数 {orderId} 似乎不起作用,我找不到方法将通配符添加到 url 别名中。(我认为这可以解决我的问题)

我知道我可能会为每个 orderId 创建一个翻译的 url 别名,但如果可能的话,我想避免这种情况。

谢谢

zan*_*mar 1

使用动态路由进行路由转换的示例:

your_module.routing.yml

route_callbacks:
  - '\Drupal\your_module\DynamicRoutes\DynamicRoutes::routes'
Run Code Online (Sandbox Code Playgroud)

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;
  }
}
Run Code Online (Sandbox Code Playgroud)

该函数等效于:

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'
Run Code Online (Sandbox Code Playgroud)

上面的代码仅用于解释。不过,我建议使用一些自定义可重用方法来构建路由,该方法可以迭代所有语言,并使用 和 的自定义翻译path,而_title任何其他不可翻译的数据都在每个路由翻译中重用。_controller'_permission'

对于调试路由drupal 控制台非常有用

drupal dr(列出所有路线)

drupal dr example.language_en(获取示例路由参数)

drupal dr example.language_fr(获取 fr 示例路由参数)