Symfony Twig-获取Twig中的默认语言环境
我正在我的网站上添加一个语言切换程序 我的参数如下:Symfony Twig-获取Twig中的默认语言环境,symfony,path,twig,translation,locale,Symfony,Path,Twig,Translation,Locale,我正在我的网站上添加一个语言切换程序 我的参数如下: avc_coming_soon: resource: "@AVCComingSoonBundle/Controller/" type: annotation prefix: /{_locale} defaults: _locale: en requirements: _locale: |fr 默认情况下设置了en 在我的小树枝上,我做到了: <div class="languages">
avc_coming_soon:
resource: "@AVCComingSoonBundle/Controller/"
type: annotation
prefix: /{_locale}
defaults:
_locale: en
requirements:
_locale: |fr
默认情况下设置了en
在我的小树枝上,我做到了:
<div class="languages">
<ul class="lang-menu">
<li class="en"><a href="{{ path(app.request.get('_route'), {'_locale': 'en' }) }}"><img src="{{ asset('images/flag_en.gif') }}" alt="EN"></a></li>
<li class="fr"><a href="{{ path(app.request.get('_route'), {'_locale': 'fr'}) }}"><img src="{{ asset('images/flag_fr.gif') }}" alt="FR"></a></li>
</ul>
</div>
如何在twig中获取默认的_语言环境
{{ path(app.request.get('_route'), {'_locale': <<<default>>> }) }}
{{path(app.request.get('u route'),{'u locale':}}}
谢谢:)您需要使用
app.request.attributes
:
{{ path(app.request.get('_route'), {'_locale': app.request.attributes.get('_locale') }) }}
如果控制器中有配置,则需要在功能参数中设置默认值:
/**
* @Route("/", name="coming_soon", options={"expose"=true}, requirements={"_locale" = "fr|en"})
*/
public function indexAction($_locale = 'en')
{
...
}
我使用这个捆绑包:
多亏了这个捆绑包,我再也不用和当地人打交道了
您可以将bundle配置为除了默认语言环境之外的所有内容的前缀(这是您不希望看到的,读取其他答案的注释)
对于语言切换器,您只需在任何给定路径上设置\u locale。不起作用,因为如果我在“www.mysite.com/fr”上,然后单击“English”,路径将变为“www.mysite.com/fr”,而不是“www.mysite.com/”。和/或如果我在“www.mysite.com/”上,然后单击“English”,路径就会变成“www.mysite.com/”,这是另一个问题。当你请求
mysite.com/
时,为什么你会被重定向到mysite.com/fr
?你有重定向规则吗?没有,当我请求mysite.com/
时,网站是英文的,当我请求mysite.com/fr
时,网站是法文的。我需要的是,当我在mysite.com/fr
上单击“英语”时,请求必须是mysite.com/
,而不是mysite.com/en
。在我的控制器中,我这样做了:*@Route(“/”,name=“coming\u soon”,options={“expose”=true},requirements={“\u locale”=“fr | en”})
这是你问的另一个问题。但我更新了我的答案,将其考虑在内。
/**
* @Route("/", name="coming_soon", options={"expose"=true}, requirements={"_locale" = "fr|en"})
*/
public function indexAction($_locale = 'en')
{
...
}