gpt4 book ai didi

php - Symfony2 在路由中使用默认语言环境(一种语言的一个 URL)

转载 作者:可可西里 更新时间:2023-10-31 22:47:26 24 4
gpt4 key购买 nike

我目前正在使用 Symfony2 开发一个网站,我需要翻译它。使用 Symfony2 提供的工具非常容易。但是我遇到了一个问题:

我想要一种语言(即一个 URL,一种语言)的特定 URL(带前缀),但具有默认语言。具体来说:

假设默认语言是英文,那么

  • http://example.com/fr/hello 用法语显示页面
  • http://example.com/it/hello 用意大利语显示页面
  • http://example.com/en/hello 重定向到 http://example.com/hello(因为 en 是默认语言)
  • http://example.com/hello 以英文显示页面(默认语言)

我天真地尝试像这样配置我的路由:

#routing.yml
_welcome:
pattern: /{_locale}/hello
defaults: { _controller: AcmeDemoBundle:Welcome:hello, _locale: en}

但那是行不通的(http://example.com/en/hello 只显示英文页面和 http://example.com/hello 返回 404 错误)。

当然也可以每次创建两条路由,但是非常繁琐。所以我正在寻找一个干净的解决方案。

顺便说一句,我注意到我正在寻找的 URL 行为正是 Symfony2 官方文档所采用的行为:

http://symfony.com/fr/doc/current/book/translation.html 显示法语翻译

http://symfony.com/it/doc/current/book/translation.html 显示意大利语翻译

http://symfony.com/en/doc/current/book/translation.html 重定向到 http://symfony.com/doc/current/book/translation。 html(以英文显示页面)

最佳答案

安装 JMSI18nBundle并应用策略 prefix_except_default

bundle 会负责为您创建路由。

配置:

jms_i18n_routing:
default_locale: en
locales: [de, en]
strategy: prefix_except_default

可以在 bundle 的 documentation 中找到更多信息。 .

关于php - Symfony2 在路由中使用默认语言环境(一种语言的一个 URL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22086755/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com