我已经实现了以下语言切换器:
<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>
我正在使用 JMSi18nRoutingBundle用于翻译网址。它工作正常,但是当它呈现在 url 中有参数的 View 时,会出现以下错误:
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') == 'homepage' %}
代替
{% if path(app.request.attributes.get('_route')) == '/' %}
说明:
当您访问需要参数的 URL 时,if 语句中的代码
path(app.request.attributes.get('_route'))
产生错误。由于您的主页路由定义为 homepage
,只检查 _route
属性足以确定您正在访问主页。快乐编码!!
关于symfony - JMSi18nRoutingBundle 语言选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23366077/