路由中的区域设置,默认语言也没有参数 [英] Locale in Routing, Default Language also without Parameter
问题描述
我想在 Symfony2 安装上通过 url 定义页面语言.我的路由通过控制器内的注释工作.
I want to define the page language via url on a Symfony2 installation. My routing works via annotation inside the controller.
路由.yml
_index:
resource: "@MyMainBundle/Controller/SiteController.php"
type: annotation
siteController.php
siteController.php
/**
* @Route( "/{_locale}/{site}", name="_site_site", defaults={"_locale" = "en"}, requirements={"site" = "about|cities|download|blog", "_locale" = "en|de|fr|es"} )
*/
这很安静,但我想要的是以下网址调用相同的操作.
This works quiet well, but waht I want, is that the following url call the same action.
http://example.com/download
http://example.com/en/download
http://example.com/de/download
如果没有 languge-parameter,页面应该切换回默认语言,但这是我可以在我的操作中处理的事情.
Without the languge-parameter, the page should switch back to the default language, but this is something I can handle inside my action.
我找到了这个答案,但根本无法让它发挥作用.Symfony2 默认路由区域设置
I found this Answer, but could not get it to work at all. Symfony2 default locale in routing
推荐答案
只需添加另一个不包含语言环境的 @Route
注释.
Just add another @Route
annotation that does not include the locale.
/**
* @Route("/{_locale}/{site}/")
* @Route("/{site}/")
*/
这篇关于路由中的区域设置,默认语言也没有参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!