2017-06-14 53 views
0

安装在今天第一次symfony的3项目KnpPaginatorBundle,并获得奇怪的错误。而不是上一个和下一个按钮,我会得到“«label_previous”......和“label_next»”。我做了一些实验,与之前添加语言到config.yml,但今天他们恢复之后,我还没有得到的按钮右边的标签。获取错误的标签从KnpPaginatorBundle

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 <[email protected]> 
* @author Jan Sorgalla <[email protected]> 
* @author Artem Ponomarenko <[email protected]> 
* @author Artem Zabelin <[email protected]> 
*/ 
#} 

{% 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 %} 
+0

如何KnpPaginatorBundle的'代码:分页:twitter_bootstrap_v3_pagination.html.twig' –

+0

@leo_ap我不明白你的问题。 – symfonypleb

+0

knp_paginator使用模板文件来渲染模板。看起来最后两行的config.yml的,你会看到2个文件,然后的一个是一个,我问你向我们展示的代码 –

回答

1

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

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

trans滤波器(http://symfony.com/doc/current/reference/twig_reference.html#trans)尝试定位翻译的一个配置文件中的特定上下文。您的上下文是KnpPaginatorBundle,因此它将搜索该捆绑包内的翻译文件。看看这里: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 /模板/分页密钥的文件。它应该是这样的:

#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 
+1

我只是取消注释这一行,在我的配置毕竟:翻译:{fallbacks:['%locale%']} – symfonypleb