如何在 Cake 3.0 中的链接的 URL 中包含语言段

How to include language segment in the URL for links in Cake 3.0

我需要像这样 url 中的语言构建一个多语言站点 http://example.org/jp/users/login,但我想避免将语言编码到每个 link网站。我实际上有这个用于路由:

$routes->connect(
    '/:lang/:controller/:action/*',
    ['prefix' => 'customer'],
    ['routeClass' => 'DashedRoute', 'lang' => '[a-z]{2}']
);

而且我必须在 link 秒内执行此操作:

$this->Html->link(__('List Users'), ['lang' => 'en', 'controller' => 'users', 'action' => 'index']);

有什么方法可以自动添加 lang,我可以只为 links 做这个吗?

$this->Html->link(__('List Users'), ['controller' => 'users', 'action' => 'index']);

只需将 'persist' => ['lang'] 添加到 $routes->connect() 语句的选项数组。