可以在 Symfony 5 中自动翻译路由吗?
It is possible to auto translate routing in Symfony 5?
我的控制器上有以下路线:
#[Route('/{_locale<en>}/profile', name: 'profile_en', methods: 'GET')]
#[Route('/{_locale<es>}/perfil', name: 'profile_es', methods: 'GET')]
是否可以将我的所有 url 翻译(在本例中为配置文件)放在一个文件中并执行类似的操作?
#[Route('/{_locale<%app.supported_locales%>}/profile', name: 'profile', methods: 'GET')]
这应该给我所有可能的语言环境 + 配置文件 url 已翻译
从 symfony 4.1 开始,他们有了国际化路由(a.k.a。本地化路由)
- 文档 → https://symfony.com/doc/current/routing.html#localized-routes-i18n
- 宣布 → https://symfony.com/blog/new-in-symfony-4-1-internationalized-routing
快速预览:
# config/routes.yaml
about_us:
path:
en: /about-us
nl: /over-ons
controller: App\Controller\CompanyController::about
因为您已经使用了 _locale
,并且在您使用 php 属性的情况下,它应该是:
#[Route(path: [
'en' => '/profile',
'es' => '/profil'
], name: 'profile', methods: 'GET')]
我想,这正是您要找的。
但是未经测试,因为我现在没法玩php-8
P.S。你也可以将你的路线重命名为 profile_i18n
这样每次你(或其他开发者)使用它时,你都会立即知道 → 这是一个国际化的路线
我的控制器上有以下路线:
#[Route('/{_locale<en>}/profile', name: 'profile_en', methods: 'GET')]
#[Route('/{_locale<es>}/perfil', name: 'profile_es', methods: 'GET')]
是否可以将我的所有 url 翻译(在本例中为配置文件)放在一个文件中并执行类似的操作?
#[Route('/{_locale<%app.supported_locales%>}/profile', name: 'profile', methods: 'GET')]
这应该给我所有可能的语言环境 + 配置文件 url 已翻译
从 symfony 4.1 开始,他们有了国际化路由(a.k.a。本地化路由)
- 文档 → https://symfony.com/doc/current/routing.html#localized-routes-i18n
- 宣布 → https://symfony.com/blog/new-in-symfony-4-1-internationalized-routing
快速预览:
# config/routes.yaml
about_us:
path:
en: /about-us
nl: /over-ons
controller: App\Controller\CompanyController::about
因为您已经使用了 _locale
,并且在您使用 php 属性的情况下,它应该是:
#[Route(path: [
'en' => '/profile',
'es' => '/profil'
], name: 'profile', methods: 'GET')]
我想,这正是您要找的。 但是未经测试,因为我现在没法玩php-8
P.S。你也可以将你的路线重命名为 profile_i18n
这样每次你(或其他开发者)使用它时,你都会立即知道 → 这是一个国际化的路线