【问题标题】:It is possible to auto translate routing in Symfony 5?可以在 Symfony 5 中自动翻译路由吗?
【发布时间】:2021-10-11 09:28:48
【问题描述】:

我的控制器上有以下路线:

#[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')]

这应该会给我所有可能的语言环境 + 个人资料网址已翻译

【问题讨论】:

标签: php symfony symfony5


【解决方案1】:

从 symfony 4.1 开始,他们有了国际化路线(又名本地化路线)

快速预览:

# 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

附:您也可以将您的路线重命名为profile_i18n,这样每次您(或其他开发人员)使用它时,您就会立即知道 → 这是一个国际化路线

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-13
    • 2013-11-29
    • 1970-01-01
    • 2020-09-29
    • 2022-11-30
    相关资源
    最近更新 更多