Symfony2在路由中使用默认语言环境(一种URL用于一种语言) [英] Symfony2 Use default locale in routing (one URL for one language)

查看:87
本文介绍了Symfony2在路由中使用默认语言环境(一种URL用于一种语言)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用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显示意大利语traduction

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

解决方案

安装 JMSI18nBundle 并应用策略prefix_except_default. /p>

该捆绑包将为您创建路线.

配置:

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

更多信息可以在捆绑软件的文档中找到.

I am currently developing a website with Symfony2 and I need to translate it. With tools provided by Symfony2 it’s very easy. But I encounter a problem:

I would like to have specific URL (with prefix) to a language (ie, one URL, a single language), but with a default language. Concretely:

Assume that the default language is English, so

  • http://example.com/fr/hello display the page in French
  • http://example.com/it/hello display the page in Italian
  • http://example.com/en/hello redirect to http://example.com/hello (because en is the default language)
  • http://example.com/hello display of course the page in English (default language)

I naively try to configure my routing like this:

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

But that’s don’t work (http://example.com/en/hello just display the page in English and http://example.com/hello return 404 error).

It’s possible, of course, to create two routes each time, but it is very tedious. So I’m looking for a clean solution.

Incidentally, I noticed that the behavior I was looking for with URL was exactly the one adopted by the official documentation of Symfony2:

http://symfony.com/fr/doc/current/book/translation.html display French traduction

http://symfony.com/it/doc/current/book/translation.html display Italian traduction

http://symfony.com/en/doc/current/book/translation.html redirect to http://symfony.com/doc/current/book/translation.html (which display the page in English)

解决方案

Install JMSI18nBundle and apply the strategy prefix_except_default.

The bundle will take care of creating the routes for you.

configuration:

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

Further information can be found in the bundle's documentation.

这篇关于Symfony2在路由中使用默认语言环境(一种URL用于一种语言)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆