2017-06-14 36 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