JMSi18nRoutingBundle语言选择器 [英] JMSi18nRoutingBundle language selector

查看:69
本文介绍了JMSi18nRoutingBundle语言选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have implemented the following language switcher:

<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中具有参数的视图时,会出现以下错误:

I'm using JMSi18nRoutingBundle for translating the urls. It works fine but when it renders a view which has a parameter in the url, the following error appears:

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.


推荐答案

使用以下代码检查首页的条件

Use following condition to check for homepage


{%如果app.request.attributes.get('_ route')=='主页'%}

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

代替


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

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



说明:



访问需要参数的URL时,if语句 path(app.request.attributes中的代码。 get('_ route'))生成错误。由于您的首页路由被定义为 主页 ,因此仅选中 _route 属性足以确定您正在访问主页。

Explanation:

When you are accessing an URL which required parameter(s), your code in if statement path(app.request.attributes.get('_route')) generates the error. As your home page route is defined as homepage, only checking the _route attribute will be enough to determine that you are accessing the homepage.

祝您编程愉快!

这篇关于JMSi18nRoutingBundle语言选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆