我的控制器上有以下路線:
#[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')]
這應該給我所有可能的語言環境 已翻譯的個人資料網址
uj5u.com熱心網友回復:
從 symfony 4.1 開始,他們有了國際化路由(又名本地化路由)
- 檔案 → 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-attributes 它應該是:
#[Route(path: [
'en' => '/profile',
'es' => '/profil'
], name: 'profile', methods: 'GET')]
我想,這正是你要找的。但未經測驗,因為我現在無法使用 php-8
PS 您也可以將您的路線重命名為profile_i18n這樣,每次您(或其他開發人員)使用它時,您都會立即知道 → 這是一個國際化的路線
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/322535.html
