1
0
Fork 0
mirror of https://github.com/kaminari/kaminari.git synced 2022-11-09 13:44:37 -05:00

Use I18n to pluralize entries

This commit is contained in:
Linus Pettersson 2016-10-11 16:07:59 +02:00
parent 653143bbd8
commit 7f6e9a877d
5 changed files with 32 additions and 6 deletions

View file

@ -90,8 +90,11 @@ module Kaminari
# <%= page_entries_info @posts, :entry_name => 'item' %> # <%= page_entries_info @posts, :entry_name => 'item' %>
# #-> Displaying items 6 - 10 of 26 in total # #-> Displaying items 6 - 10 of 26 in total
def page_entries_info(collection, options = {}) def page_entries_info(collection, options = {})
entry_name = options[:entry_name] || collection.entry_name entry_name = if options[:entry_name]
entry_name = entry_name.pluralize unless collection.total_count == 1 options[:entry_name].pluralize(collection.size)
else
collection.entry_name(:count => collection.size).downcase
end
if collection.total_pages < 2 if collection.total_pages < 2
t('helpers.page_entries_info.one_page.display_entries', :entry_name => entry_name, :count => collection.total_count) t('helpers.page_entries_info.one_page.display_entries', :entry_name => entry_name, :count => collection.total_count)

View file

@ -1,7 +1,7 @@
module Kaminari module Kaminari
module ActiveRecordRelationMethods module ActiveRecordRelationMethods
def entry_name def entry_name(options = {})
model_name.human.downcase model_name.human(options.reverse_merge(:default => model_name.human.pluralize(options[:count])))
end end
def reset #:nodoc: def reset #:nodoc:

View file

@ -10,6 +10,10 @@ en:
truncate: "&hellip;" truncate: "&hellip;"
helpers: helpers:
page_entries_info: page_entries_info:
entry:
zero: "entries"
one: "entry"
other: "entries"
one_page: one_page:
display_entries: display_entries:
zero: "No %{entry_name} found" zero: "No %{entry_name} found"

View file

@ -4,6 +4,8 @@ module Kaminari
class PaginatableArray < Array class PaginatableArray < Array
include Kaminari::ConfigurationMethods::ClassMethods include Kaminari::ConfigurationMethods::ClassMethods
ENTRY = 'entry'.freeze
attr_internal_accessor :limit_value, :offset_value attr_internal_accessor :limit_value, :offset_value
# ==== Options # ==== Options
@ -28,8 +30,8 @@ module Kaminari
super(original_array || []) super(original_array || [])
end end
def entry_name def entry_name(options = {})
"entry" I18n.t('helpers.page_entries_info.entry', options.reverse_merge(:default => ENTRY.pluralize(options[:count])))
end end
# items at the specified "page" # items at the specified "page"

View file

@ -233,6 +233,23 @@ describe 'Kaminari::ActionViewExtension', :if => defined?(::Rails::Railtie) && d
end end
end end
end end
context 'I18n' do
before do
50.times {|i| User.create! :name => "user#{i}"}
@users = User.page(1).per(25)
I18n.backend.store_translations(:en, { User.i18n_scope => { models: { user: { one: "person", other: "people" } } } })
end
after do
I18n.backend.reload!
end
context 'page_entries_info translates entry' do
subject { helper.page_entries_info @users, :params => {:controller => 'users', :action => 'index'} }
it { should == 'Displaying people <b>1&nbsp;-&nbsp;25</b> of <b>50</b> in total' }
end
end
context 'on a model with namespace' do context 'on a model with namespace' do
before do before do
@addresses = User::Address.page(1).per(25) @addresses = User::Address.page(1).per(25)