我已经实现了以下语言切换器:
<ul id="language-selector">
{% if path(app.request.attributes.get('_route')) == '/' %}
<li><a href="{{ path("homepage", {"_locale": "es"}) }}">ES</a></li>
<li><a href="{{ path("homepage", {"_locale": "en"}) }}">EN</a></li>
<li><a href="{{ path("homepage", {"_locale": "it"}) }}">IT</a></li>
{% else %}
<li><a href="{{ path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')|merge({'_locale': 'es'})) }}">ES</a></li>
<li><a href="{{ path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')|merge({'_locale': 'en'})) }}">EN</a></li>
<li><a href="{{ path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')|merge({'_locale': 'it'})) }}">IT</a></li>
{% endif %}
</ul>
我在用着JMSi18n路由包 https://github.com/schmittjoh/JMSI18nRoutingBundle用于翻译网址。它工作正常,但是当它渲染 url 中包含参数的视图时,会出现以下错误:
An exception has been thrown during the rendering of a template ("Some mandatory
parameters are missing ("id") to generate a URL for route
"es__RG__myapp_mybundle_myaction_edit".") in
MyAppMyBundle:Default:includes/myView.html.twig at line 21.
使用以下条件检查主页
{% if app.request.attributes.get('_route') == '主页' %}
代替
{% if path(app.request.attributes.get('_route')) == '/' %}
解释:
当您访问需要参数的 URL 时,if 语句中的代码path(app.request.attributes.get('_route'))
产生错误。由于您的主页路由定义为homepage
,仅检查_route
属性足以确定您正在访问主页。
快乐编码!!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)