Give custom messages in page_entries_info of will_paginate in rails
Asked Answered
S

2

7

I am new to rails. I want to display my custom message for page_entries_info. I have gone through following link but cannot understand much. Can anyone please explain in details.

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

Septuagesima answered 1/8, 2012 at 10:54 Comment(2)
what exactly you want to customize? can you explain a little more?Lashondra
actually for me default message coming is "Displaying Topic 1 - 5 of 57 in total", i want to make it as "Displaying 1 - 5 of 57 of Topic". I've gone through the link but can't understand where to add Yaml file. How it works?Septuagesima
Q
9

Another option is you can define your page_entries_info() method in your ApplicationHelper and use it as you normally would. This would give you more flexibility and can even be more cleaner and efficient if you know that you dont need to cover the edge cases (as in my case). You can refer the original method definition here and see what all you need to implement. Following code would run for most part of your problem!

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
Quirita answered 16/9, 2012 at 18:12 Comment(0)
S
8

This is what is loaded by default, taken from 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"

you need to change multi_page_html and multi_page, the last 2 entries.

in your en.yml file (or whatever it is) put something like this:

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}"

If you have difficulties about yml file rails i18n guide is a little advanced but gives nice information about how to use yml file - just scroll down a little :).

I hope it helps.

Steel answered 1/8, 2012 at 11:38 Comment(4)
is there no other way other than using internationalization??Septuagesima
your software is already "internationalized" by default. this is not the "hard way". just give it a try..Lashondra
I am using rails engines, and there in config i can't find locale directory. Will creating a directory work for meSeptuagesima
Rails::Engine documentation will help. i18n is at core, wherever you look in rails.Lashondra

© 2022 - 2024 — McMap. All rights reserved.