gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 05:49:23 25 4
gpt4 key购买 nike

我有以下路线

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

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

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

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

谢谢

最佳答案

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

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;
}
}

此函数等效于:
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'

代码 aboce 仅用于解释。但是,我建议使用一些自定义的可重用方法构建路由,这些方法可以遍历所有语言,并为 path 提供自定义翻译。和 _title , 而 _controller , '_permission'并且任何其他不可翻译的数据在每个路由翻译中被重用。

用于调试路由 drupal console非常有用
drupal dr (列出所有路线)
drupal dr example.language_en (获取示例路由参数)
drupal dr example.language_fr (获取 fr 示例路由参数)

关于routing - 使用参数翻译 Drupal 8 自定义路由路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56836427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com