【问题标题】:Give custom messages in page_entries_info of will_paginate in rails在 rails 的 will_paginate 的 page_entries_info 中给出自定义消息
【发布时间】:2012-07-30 06:13:30
【问题描述】:

我是 Rails 新手。我想为 page_entries_info 显示我的自定义消息。我已经通过以下链接,但不能理解太多。谁能详细解释一下。

how-do-i-specify-custom-wording-in-a-will-paginate-view-helper

【问题讨论】:

  • 您到底想定制什么?你能再解释一下吗?
  • 实际上对我来说默认消息是“总共显示 57 个主题中的 1 - 5 个”,我想将其设为“显示 57 个主题中的 1 - 5 个”。我已经浏览了链接,但不明白在哪里添加 Yaml 文件。它是如何工作的?

标签: ruby-on-rails ruby will-paginate


【解决方案1】:

另一种选择是您可以在 ApplicationHelper 中定义您的 page_entries_info() 方法 并像往常一样使用它。如果您知道不需要覆盖边缘情况(如我的情况),这将为您提供更大的灵活性,甚至可以更加清洁和高效。您可以参考原始方法定义here 并查看您需要实现的所有内容。以下代码将运行您的大部分问题!

def page_entries_info(collection, options = {})
  entry_name = options[:entry_name] || (collection.empty?? 'item' :
      collection.first.class.name.split('::').last.titleize)
  if collection.total_pages < 2
    case collection.size
    when 0; "No #{entry_name.pluralize} found"
    else; "Displaying all #{entry_name.pluralize}"
    end
  else
    %{Displaying %d - %d of %d #{entry_name.pluralize}} % [
      collection.offset + 1,
      collection.offset + collection.length,
      collection.total_entries
    ]
  end
end

【讨论】:

    【解决方案2】:

    这是默认加载的,取自project wiki

    en:
      will_paginate:
        page_entries_info:
          single_page:
            zero:  "No %{model} found"
            one:   "Displaying 1 %{model}"
            other: "Displaying all %{count} %{model}"
          single_page_html:
            zero:  "No %{model} found"
            one:   "Displaying <b>1</b> %{model}"
            other: "Displaying <b>all&nbsp;%{count}</b> %{model}"
    
          multi_page: "Displaying %{model} %{from} - %{to} of %{count} in total"
          multi_page_html: "Displaying %{model} <b>%{from}&nbsp;-&nbsp;%{to}</b> of <b>%{count}</b> in total"
    

    您需要更改 multi_page_htmlmulti_page,最后 2 个条目。

    在您的 en.yml 文件(或其他任何文件)中输入如下内容:

    en:
      will_paginate:
        line_item:
          page_entries_info:
            multi_page: "Displaying %{from} - %{to} of %{count} of %{model}"        
            multi_page_html: "Displaying <b>%{from}&nbsp;-&nbsp;%{to}</b> of <b>%{count}</b> of %{model}"
    

    如果您对 yml 文件有困难,rails i18n guide 有点高级,但提供了有关如何使用 yml 文件的好信息 - 只需向下滚动一点 :)。

    希望对你有帮助。

    【讨论】:

    • 除了使用国际化没有别的办法了吗??
    • 默认情况下您的软件已经“国际化”了。这不是“艰难的方式”。试试看吧..
    • 我正在使用 Rails 引擎,但在配置中我找不到语言环境目录。将为我创建一个目录吗
    • Rails::Engine documentation 会有所帮助。 i18n 是核心,无论您在 Rails 中查看什么位置。
    猜你喜欢
    • 1970-01-01
    • 2011-01-27
    • 1970-01-01
    • 1970-01-01
    • 2011-07-20
    • 2011-10-21
    • 1970-01-01
    • 1970-01-01
    • 2011-04-11
    相关资源
    最近更新 更多