如何在树枝模板中创建语言选择

问题描述 投票:-2回答:1

我目前正在使用symfony 3.2。现在我有一个像这样的链接:http://link.com?lang=en和config /参数我有allowed_locales -en,-ru

我怎么能在这样的树枝模板中创建一个语言切换器:

  <a href="#" class="locales">EN<img src="{{ asset('assets/images/arrow-down.svg') }}" alt="arrow" class="arrow-down" /></a>
                                <div class="locales-content" style="left:0;">
                                    <a href="#">Russian</a>
                                    <a href="#">English</a>
                                </div>
php symfony twig
1个回答
1
投票

这是我在我的应用程序中所做的,随意根据您的需求进行调整。它会创建一个下拉列表,其中包含两个重定向到同一页面的链接,但会更改_locale参数。如果请求中没有路由,则会创建两个重定向到主页的链接。

<li class="dropdown">
    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">{% if app.request.locale == 'ru' %}Russian{% else %}English{% endif %} <span class="caret"></span></a>
    <ul class="dropdown-menu">

        {# Check if there is a route and some parameters in the request #}
        {% if app.request.get('_route') is not empty and app.request.get('_route_params') is not null %}

            {# English #}
            <a href="{{ path(app.request.get('_route'), app.request.get('_route_params')|merge({'_locale': 'en'})) }}">English</a>
            {# Russian #}
            <a href="{{ path(app.request.get('_route'), app.request.get('_route_params')|merge({'_locale': 'ru'})) }}">Russian</a>

        {# If there is no route in the request, redirect to the homepage #}
        {% else %}

            {# English #}
            <a href="{{ path('homepage', {'_locale': 'en'}) }}">English</a>
            {# Russian #}
            <a href="{{ path('homepage', {'_locale': 'ru'}) }}">Russian</a>

        {% endif %}
    </ul>
</li>
© www.soinside.com 2019 - 2024. All rights reserved.