JMSi18nRoutingBundle语言选择器

5 routing translation symfony

我已经实现了以下语言切换器:

<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>
Run Code Online (Sandbox Code Playgroud)

我正在使用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.
Run Code Online (Sandbox Code Playgroud)

xii*_*dea 3

使用以下条件检查主页

{% if app.request.attributes.get('_route') == '主页' %}

代替

{% if path(app.request.attributes.get('_route')) == '/' %}

解释:

当您访问需要参数的 URL 时,if 语句中的代码path(app.request.attributes.get('_route'))会生成错误。由于您的主页路由定义为homepage,因此只需检查该_route属性就足以确定您正在访问主页。

快乐编码!!