从 KnpPaginatorBundle 获取错误的标签

Getting wrong labels from KnpPaginatorBundle

今天第一次在 symfony 3 项目上安装了 KnpPaginatorBundle,出现了奇怪的错误。我得到的不是 Previous 和 Next 按钮,而是“« label_previous”......和 ​​"label_next »"。我之前做过一些向 config.yml 添加语言的实验,但在今天恢复它们之后,我仍然没有得到正确的按钮标签。

Picture of the pages navigation

和我的config.yml

imports:
    - { resource: parameters.yml }
    - { resource: security.yml }
    - { resource: services.yml }

# Put parameters here that don't need to change on each machine where the app is deployed
# http://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
    locale: en

framework:
   #esi: ~
    #translator: { fallbacks: ['%locale%'] }
    secret: '%secret%'
    router:
        resource: '%kernel.root_dir%/config/routing.yml'
        strict_requirements: ~
    form: ~
    csrf_protection: ~
    validation: { enable_annotations: true }
    #serializer: { enable_annotations: true }
    templating:
        engines: ['twig']
    default_locale: '%locale%'
    trusted_hosts: ~
    trusted_proxies: ~
    session:
       # http://symfony.com/doc/current/reference/configuration/framework.html#handler-id
        handler_id: session.handler.native_file
        save_path:  "%kernel.root_dir%/../var/sessions/%kernel.environment%"
    fragments: ~
    http_method_override: true
    assets: ~
    php_errors:
        log: true

# Twig Configuration
twig:
    debug: '%kernel.debug%'
    strict_variables: '%kernel.debug%'
    form_themes:
       - bootstrap_3_layout.html.twig

    globals:
        brands: '@list_brands'

# Doctrine Configuration
doctrine:
    dbal:
        driver: pdo_mysql
        host: '%database_host%'
        port: '%database_port%'
        dbname: '%database_name%'
        user: '%database_user%'
        password: '%database_password%'
        charset: UTF8
        # if using pdo_sqlite as your database driver:
        #   1. add the path in parameters.yml
        #     e.g. database_path: "%kernel.root_dir%/../var/data/data.sqlite"
        #   2. Uncomment database_path in parameters.yml.dist
        #   3. Uncomment next line:
        #path: '%database_path%'

    orm:
        auto_generate_proxy_classes: '%kernel.debug%'
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true

# Swiftmailer Configuration
swiftmailer:
    transport: '%mailer_transport%'
    host: '%mailer_host%'
    username: '%mailer_user%'
    password: '%mailer_password%'
    spool: { type: memory }

#KNP
knp_paginator:
    page_range: 5                      # default page range used in pagination control
    default_options:
        page_name: page                # page query parameter name
        sort_field_name: sort          # sort field query parameter name
        sort_direction_name: direction # sort direction query parameter name
        distinct: true                 # ensure distinct results, useful when ORM queries are using GROUP BY statements
    template:
        pagination: 'KnpPaginatorBundle:Pagination:twitter_bootstrap_v3_pagination.html.twig'     # sliding pagination controls template
        sortable: 'KnpPaginatorBundle:Pagination:sortable_link.html.twig' # sort link template

twitter_bootstrap_v3_pagination.html.twig文件

{#
/**
 * @file
 * Twitter Bootstrap v3 Sliding pagination control implementation.
 *
 * View that can be used with the pagination module
 * from the Twitter Bootstrap CSS Toolkit
 * http://getbootstrap.com/components/#pagination
 *
 * @author Pablo Díez <pablodip@gmail.com>
 * @author Jan Sorgalla <jsorgalla@gmail.com>
 * @author Artem Ponomarenko <imenem@inbox.ru>
 * @author Artem Zabelin <artjomzabelin@gmail.com>
 */
#}

{% if pageCount > 1 %}
    <ul class="pagination">

    {% if previous is defined %}
        <li>
            <a rel="prev" href="{{ path(route, query|merge({(pageParameterName): previous})) }}">&laquo;&nbsp;{{ 'label_previous'|trans({}, 'KnpPaginatorBundle') }}</a>
        </li>
    {% else %}
        <li class="disabled">
            <span>&laquo;&nbsp;{{ 'label_previous'|trans({}, 'KnpPaginatorBundle') }}</span>
        </li>
    {% endif %}

    {% if startPage > 1 %}
        <li>
            <a href="{{ path(route, query|merge({(pageParameterName): 1})) }}">1</a>
        </li>
        {% if startPage == 3 %}
            <li>
                <a href="{{ path(route, query|merge({(pageParameterName): 2})) }}">2</a>
            </li>
        {% elseif startPage != 2 %}
        <li class="disabled">
            <span>&hellip;</span>
        </li>
        {% endif %}
    {% endif %}

    {% for page in pagesInRange %}
        {% if page != current %}
            <li>
                <a href="{{ path(route, query|merge({(pageParameterName): page})) }}">{{ page }}</a>
            </li>
        {% else %}
            <li class="active">
                <span>{{ page }}</span>
            </li>
        {% endif %}

    {% endfor %}

    {% if pageCount > endPage %}
        {% if pageCount > (endPage + 1) %}
            {% if pageCount > (endPage + 2) %}
                <li class="disabled">
                    <span>&hellip;</span>
                </li>
            {% else %}
                <li>
                    <a href="{{ path(route, query|merge({(pageParameterName): (pageCount - 1)})) }}">{{ pageCount -1 }}</a>
                </li>
            {% endif %}
        {% endif %}
        <li>
            <a href="{{ path(route, query|merge({(pageParameterName): pageCount})) }}">{{ pageCount }}</a>
        </li>
    {% endif %}

    {% if next is defined %}
        <li>
            <a rel="next" href="{{ path(route, query|merge({(pageParameterName): next})) }}">{{ 'label_next'|trans({}, 'KnpPaginatorBundle') }}&nbsp;&raquo;</a>
        </li>
    {% else %}
        <li class="disabled">
            <span>{{ 'label_next'|trans({}, 'KnpPaginatorBundle') }}&nbsp;&raquo;</span>
        </li>
    {% endif %}
    </ul>
{% endif %}

此模板文件 twitter_bootstrap_v3_pagination.html.twig 使用 trans twig 过滤器来获取标签的名称。例如,您可以在以下部分看到这一点:

<span>&laquo;&nbsp;{{ 'label_previous'|trans({}, 'KnpPaginatorBundle') }}</span>

trans 过滤器(http://symfony.com/doc/current/reference/twig_reference.html#trans) tries to locate an configuration file of translations in a specific context. Your context is the KnpPaginatorBundle, so, it will search for a translation file inside this bundle. Look at it here: https://github.com/KnpLabs/KnpPaginatorBundle/tree/master/Resources/translations

每个语言环境都有一个翻译文件,但如果您的应用程序默认语言环境不在此列表中,则不会翻译。

但是,如果您的语言环境是 en 但它仍然不起作用,您可以制作自己的模板并手动放置标签。

只需 3 个简单步骤即可完成。

1º:app/Resources/views 文件夹下创建一个名为 pagination.twig.html(或类似名称)的新文件。

2º: 复制文件 twitter_bootstrap_v3_pagination.html.twig 的代码并粘贴到您新创建的文件中。然后,更改引用 trans 过滤器的行。示例:

行: <span>&laquo;&nbsp;{{ 'label_previous'|trans({}, 'KnpPaginatorBundle') }}</span>

您应该编辑为: <span>&laquo;&nbsp; Previous</span>

3º: 在您的 app/config/config.yml 中更改 knp_paginator/template/pagination 键中的文件。它应该是这样的:

#KNP
knp_paginator:
    page_range: 5                      # default page range used in pagination control
    default_options:
        page_name: page                # page query parameter name
        sort_field_name: sort          # sort field query parameter name
        sort_direction_name: direction # sort direction query parameter name
        distinct: true                 # ensure distinct results, useful when ORM queries are using GROUP BY statements
    template:
        pagination: 'pagination.html.twig'
        sortable: 'KnpPaginatorBundle:Pagination:sortable_link.html.twig' # sort link template