php - 可以在 Symfony 5 中自动翻译路由吗?
问题描述
我的控制器上有以下路线:
#[Route('/{_locale<en>}/profile', name: 'profile_en', methods: 'GET')]
#[Route('/{_locale<es>}/perfil', name: 'profile_es', methods: 'GET')]
可以将我所有的网址翻译(在这种情况下为配置文件)放在一个文件中并执行类似的操作吗?
#[Route('/{_locale<%app.supported_locales%>}/profile', name: 'profile', methods: 'GET')]
这应该给我所有可能的语言环境+个人资料网址已翻译
解决方案
从 symfony 4.1 开始,他们有了国际化的路线(又名 Localized Routes)
- 文档 → 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
以便每次您(或其他开发人员)使用它时,您立即知道→这是一个国际化的
推荐阅读
- maven - Maven MultiModule 本地安装不更新
- php - Laravel 获取 # 字符后的参数值
- android - 安卓应用更新后数据丢失
- xcode - 如何使用 Swift 包管理器仅在一个包上重置包缓存
- r - 根据与字符串向量 R 的部分匹配为每个 df 行分配类别
- flutter - 如何比较 Dart 中构造不同的两个日期?
- javascript - 应该如何在codepen中使用此代码添加以下部分任何人都知道我想在此标题下添加关于部分和工作部分
- powerapps - PowerApps 库 - 混合屏幕
- python - 我有带有情绪分数的词典,我想从标记化的推文中找到这些词并添加分数
- r - 带有此类数据的图表,我如何找到每个“集群”的峰值和谷值?在 R 中