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:
parent
653143bbd8
commit
7f6e9a877d
5 changed files with 32 additions and 6 deletions
|
@ -90,8 +90,11 @@ module Kaminari
|
|||
# <%= page_entries_info @posts, :entry_name => 'item' %>
|
||||
# #-> Displaying items 6 - 10 of 26 in total
|
||||
def page_entries_info(collection, options = {})
|
||||
entry_name = options[:entry_name] || collection.entry_name
|
||||
entry_name = entry_name.pluralize unless collection.total_count == 1
|
||||
entry_name = if options[:entry_name]
|
||||
options[:entry_name].pluralize(collection.size)
|
||||
else
|
||||
collection.entry_name(:count => collection.size).downcase
|
||||
end
|
||||
|
||||
if collection.total_pages < 2
|
||||
t('helpers.page_entries_info.one_page.display_entries', :entry_name => entry_name, :count => collection.total_count)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
module Kaminari
|
||||
module ActiveRecordRelationMethods
|
||||
def entry_name
|
||||
model_name.human.downcase
|
||||
def entry_name(options = {})
|
||||
model_name.human(options.reverse_merge(:default => model_name.human.pluralize(options[:count])))
|
||||
end
|
||||
|
||||
def reset #:nodoc:
|
||||
|
|
|
@ -10,6 +10,10 @@ en:
|
|||
truncate: "…"
|
||||
helpers:
|
||||
page_entries_info:
|
||||
entry:
|
||||
zero: "entries"
|
||||
one: "entry"
|
||||
other: "entries"
|
||||
one_page:
|
||||
display_entries:
|
||||
zero: "No %{entry_name} found"
|
||||
|
|
|
@ -4,6 +4,8 @@ module Kaminari
|
|||
class PaginatableArray < Array
|
||||
include Kaminari::ConfigurationMethods::ClassMethods
|
||||
|
||||
ENTRY = 'entry'.freeze
|
||||
|
||||
attr_internal_accessor :limit_value, :offset_value
|
||||
|
||||
# ==== Options
|
||||
|
@ -28,8 +30,8 @@ module Kaminari
|
|||
super(original_array || [])
|
||||
end
|
||||
|
||||
def entry_name
|
||||
"entry"
|
||||
def entry_name(options = {})
|
||||
I18n.t('helpers.page_entries_info.entry', options.reverse_merge(:default => ENTRY.pluralize(options[:count])))
|
||||
end
|
||||
|
||||
# items at the specified "page"
|
||||
|
|
|
@ -233,6 +233,23 @@ describe 'Kaminari::ActionViewExtension', :if => defined?(::Rails::Railtie) && d
|
|||
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 - 25</b> of <b>50</b> in total' }
|
||||
end
|
||||
end
|
||||
context 'on a model with namespace' do
|
||||
before do
|
||||
@addresses = User::Address.page(1).per(25)
|
||||
|
|
Loading…
Reference in a new issue