diff --git a/kaminari-core/lib/kaminari/helpers/helper_methods.rb b/kaminari-core/lib/kaminari/helpers/helper_methods.rb index 7d39355..1ed6bcb 100644 --- a/kaminari-core/lib/kaminari/helpers/helper_methods.rb +++ b/kaminari-core/lib/kaminari/helpers/helper_methods.rb @@ -99,7 +99,7 @@ module Kaminari # #-> Displaying items 6 - 10 of 26 in total def page_entries_info(collection, entry_name: nil) entry_name = if entry_name - entry_name.pluralize(collection.size) + entry_name.pluralize(collection.size, I18n.locale) else collection.entry_name(count: collection.size).downcase end diff --git a/kaminari-core/test/helpers/action_view_extension_test.rb b/kaminari-core/test/helpers/action_view_extension_test.rb index 08afaab..20a889c 100644 --- a/kaminari-core/test/helpers/action_view_extension_test.rb +++ b/kaminari-core/test/helpers/action_view_extension_test.rb @@ -5,6 +5,8 @@ if defined?(::Rails::Railtie) && defined?(::ActionView) class ActionViewExtensionTest < ActionView::TestCase setup do self.output_buffer = ::ActionView::OutputBuffer.new + I18n.available_locales = [:en, :de, :fr] + I18n.locale = :en end teardown do User.delete_all @@ -266,6 +268,92 @@ if defined?(::Rails::Railtie) && defined?(::ActionView) I18n.backend.reload! end end + + sub_test_case 'with any other locale' do + teardown do + I18n.backend.reload! + end + + sub_test_case ':de' do + setup do + I18n.locale = :de + I18n.backend.store_translations(:de, + helpers: { + page_entries_info: { + one_page: { + display_entries: { + one: "Displaying 1 %{entry_name}", + other: "Displaying all %{count} %{entry_name}" + } + }, + more_pages: { + display_entries: "Displaying %{entry_name} %{first} - %{last} of %{total} in total" + } + } + } + ) + end + + test 'with default entry name' do + users = User.page(1).per(50) + assert_equal 'Displaying all 50 Benutzer', view.page_entries_info(users, entry_name: 'Benutzer') + end + + test 'the last page with default entry name' do + User.max_pages 4 + users = User.page(4).per(10) + assert_equal 'Displaying Benutzer 31 - 40 of 50 in total', view.page_entries_info(users, entry_name: 'Benutzer') + end + end + end + + sub_test_case ':fr' do + setup do + I18n.locale = :fr + ActiveSupport::Inflector.inflections(:fr) do |inflect| + inflect.plural(/$/, 's') + inflect.singular(/s$/, '') + end + I18n.backend.store_translations(:fr, + helpers: { + page_entries_info: { + one_page: { + display_entries: { + one: "Displaying 1 %{entry_name}", + other: "Displaying all %{count} %{entry_name}" + } + }, + more_pages: { + display_entries: "Displaying %{entry_name} %{first} - %{last} of %{total} in total" + } + } + } + ) + end + + sub_test_case 'having 1 entry' do + setup do + User.delete_all + User.create! name: 'user1' + end + + test 'with default entry name' do + users = User.page(1).per(25) + assert_equal 'Displaying 1 utilisateur', view.page_entries_info(users, entry_name: 'utilisateur') + end + end + + test 'having multiple entries with default entry name' do + users = User.page(1).per(50) + assert_equal 'Displaying all 50 utilisateurs', view.page_entries_info(users, entry_name: 'utilisateur') + end + + test 'the last page with default entry name' do + User.max_pages 4 + users = User.page(4).per(10) + assert_equal 'Displaying utilisateurs 31 - 40 of 50 in total', view.page_entries_info(users, entry_name: 'utilisateur') + end + end end sub_test_case 'on a model with namespace' do