Symfony 5 - 国际化路由 - 带有空或动态语言环境的前缀路由

时间:2021-05-21 21:27:30

标签: routes locale symfony5

以下声明有效:

app_localized:
    resource: "../src/Controller/"
    type: annotation
    prefix:
        en: '' # don't prefix URLs for English, the default locale
        de: '/de'
        es: '/es'

所以我们可以访问以下网址:

  • /
  • /de
  • /es

但是当支持的语言发生变化时,我不想每年更新这个项目的配置文件。我想声明如下路线:

app_localized:
    resource: "../src/Controller/"
    type: annotation
    prefix:
        en: '' # don't prefix URLs for English, the default locale
        '*': {_locale}

因此可以访问以下网址:

  • /
  • /de
  • /es
  • /nl
  • /fr
  • 还有更多

但遗憾的是这行不通。将语言环境定义为模式也很好: ^[a-z]{2}([-])?([A-Za-z]{2})?$

任何想法如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我通常定义两种路由:一种用于无前缀路由,另一种用于带前缀

这样的事情可能会奏效

app_localized:
  resource: "../src/Controller/"
  type: annotation
  prefix: '/{_locale}'
  name_prefix: 'localized_'

app_default:
  resource: "../src/Controller/"
  type: annotation
  prefix: '/'
  name_prefix: 'default_'
  defaults:
    _locale: en