【问题标题】:Manually switch _locale in symfony 4在 symfony 4 中手动切换 _locale
【发布时间】:2019-01-06 22:12:55
【问题描述】:

我完全无法在 Symfony 4 中手动切换 _locale 变量的解决方案。

我关注了these steps,但现在我完全不知道如何在导航部分制作一个简单的开关按钮。我还查看了this 问题,但这似乎是较旧的 Symfony 版本..

谁能帮我走出这个黑洞,向我解释如何集成一个简单的_locale 开关按钮,或者至少为我指明正确的方向?

【问题讨论】:

  • 在您的切换按钮中使用$request->getSession()->set('_locale', 'en'); 对您有用吗?

标签: php symfony twig locale


【解决方案1】:

答案与 this 答案略有不同,后者不适用于 Symfony 4。首先编辑 config 目录中的 services.yaml 文件。

{# project/config/services.yaml}

# ...
parameters:
    # ...
    app_locales: [nl_NL, en_EN]

twig:
    # ...
    globals:
        locales: %app_locales%
        # ...

然后添加一个模板以将切换按钮集成到基本模板中的某处。

{# project/templates/_locale_switcher.html.twig #}

{% set route = app.request.attributes.get('_route') %}
{% set route_params = app.request.attributes.get('_route_params') %}
{% set params = route_params|merge(app.request.query.all) %}

{# You may want to not print a flag/link for current view, the "if" here let 
you handle it #}

{% for locale in locales if locale != app.request.locale %}

    <li>
        <a href="{{ path(route, params|merge({ _locale: locale })) }}">
            <img src="{{ asset('img/flags/' ~ locale ~ '.jpg') }}" alt="{{ 
locale }}">
        </a>
    </li>

{% endfor %}

最后将这个全新的模板集成到您的基础模板中。

{# project/templates/base.html.twig #}

{% include '_locale_switcher.html.twig' %}

为 Symfony 4.3.4+ 编辑

根据下面 Charles 的回答,services.yaml 文件中的语言环境值应该用引号插入以避免无效的 YAML 错误:

{# project/config/services.yaml}

# ...
parameters:
    # ...
    app_locales: [nl_NL, en_EN]

twig:
    # ...
    globals:
        locales: "%app_locales%"
        # ... 

【讨论】:

    【解决方案2】:

    这是最新的 symfony 4 版本对我有用的方法

    services.yaml:

    parameters:
        locale: 'en'
        app_locales: en|fr
    
    twig:
        globals:
            locales: '%app_locales%'
    

    语言环境模板:

    {# project/templates/_locale_switcher.html.twig #}
    
    {% set route = app.request.attributes.get('_route') %}
    {% set route_params = app.request.attributes.get('_route_params') %}
    {% set params = route_params|merge(app.request.query.all) %}
    
    {# You may want to not print a flag/link for current view, the "if" here let
    you handle it #}
    
    {% for locale in locales|split('|') %}
        {% if locale != app.request.locale %}
            <li>
                <a href="{{ path(route, params|merge({ _locale: locale })) }}">
                    <img src="{{ asset('img/flags/' ~ locale ~ '.jpg') }}" alt="{{ locale }}">
                </a>
            </li>
        {% endif %}
    {% endfor %}
    

    【讨论】:

      【解决方案3】:

      从最新的 Symfony (5.3.9) 开始,我强烈建议您遵循文档:

      https://symfony.com/doc/current/the-fast-track/en/28-intl.html

      最后,根据您可用的语言进行一些调整,以正确生成一个漂亮的下拉菜单:

      <ul class="navbar-nav me-auto mb-2 mb-md-0">
          <li class="nav-item dropdown">
              <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false" id="locales">
                  <i class="fa fa-globe" aria-hidden="true"></i>
                  {{ app.request.locale|locale_name(app.request.locale)|u.title }}
              </a>
              <ul class="dropdown-menu" aria-labelledby="locales">
                  {% for locale in locales|split('|') %}
                      <li>
                          <a {% if app.request.locale == locale %} 
                              class="dropdown-item active" 
                          {% else %} 
                              class="dropdown-item" 
                          {% endif %} 
                              href="{{ path(app.request.get('_route', 'app_index'), app.request.get('_route_params', [])|merge({_locale: locale})) }}">
                              {{ locale|locale_name(locale)|u.title }}
                          </a>
                      </li>
                  {% endfor %}
              </ul>
          </li>
      </ul>
      

      PS:以上使用的是Bootstrap 5。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-06
        • 2011-10-18
        • 2021-01-23
        • 2016-02-06
        • 1970-01-01
        相关资源
        最近更新 更多