symfony - JMSi18nRoutingBundle 语言选择器

标签 symfony routing translation

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

<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/

相关文章:

php - Symfony 自定义身份验证提供程序在请求重叠时注销

mongodb - 在 Golang 和 MongoDB 中将路由拆分为单独的包

Angular 6 - 翻译动态文本

javascript - css中的3D轮子旋转效果

iphone - 如何结合缩放和平移动画

symfony - 在 FOSElasticaBundle 中排序

php - 在 Symfony2 中使用 Doctrine DQL 时限制检索的记录数量

passwords - FOSUserBundle:自定义密码/从旧数据库结构迁移

html - Angular 2 - 如何路由到新页面而不在新页面的 app.component.html 中显示 html

angular - 路由按钮点击 Angular 5