Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add pluralization based on locale setup for any language #899

Merged
merged 2 commits into from Jul 17, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion kaminari-core/lib/kaminari/helpers/helper_methods.rb
Expand Up @@ -99,7 +99,7 @@ def link_to_next_page(scope, name, **options)
# #-> 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
Expand Down
128 changes: 128 additions & 0 deletions kaminari-core/test/helpers/action_view_extension_test.rb
Expand Up @@ -5,6 +5,8 @@
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
Expand Down Expand Up @@ -266,6 +268,132 @@ def to_s
I18n.backend.reload!
end
end

sub_test_case 'with any other locale' do
sub_test_case ':de' do
Copy link
Member

@yuki24 yuki24 Jul 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this entire sub test case for :de is necessary when there is a test case for :fr. Are there any tests that fail without your change?

setup do
I18n.locale = :de
end

test 'with default entry name' do
users = User.page(1).per(50)
begin
I18n.backend.store_translations(:de,
helpers: {
page_entries_info: {
one_page: {
display_entries: {
one: "Displaying <b>1</b> %{entry_name}",
other: "Displaying <b>all %{count}</b> %{entry_name}"
}
}
}
}
)
assert_equal 'Displaying <b>all 50</b> Benutzer', view.page_entries_info(users, entry_name: 'Benutzer')
ensure
I18n.backend.reload!
end
end

test 'the last page with default entry name' do
User.max_pages 4
users = User.page(4).per(10)
begin
I18n.backend.store_translations(:de,
helpers: {
page_entries_info: {
more_pages: {
display_entries: "Displaying %{entry_name} <b>%{first}&nbsp;-&nbsp;%{last}</b> of <b>%{total}</b> in total"
}
}
}
)
assert_equal 'Displaying Benutzer <b>31&nbsp;-&nbsp;40</b> of <b>50</b> in total', view.page_entries_info(users, entry_name: 'Benutzer')
ensure
I18n.backend.reload!
end
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great 👍🏻

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)
begin
I18n.backend.store_translations(:fr,
helpers: {
page_entries_info: {
one_page: {
display_entries: {
one: "Displaying <b>1</b> %{entry_name}",
other: "Displaying <b>all %{count}</b> %{entry_name}"
}
}
}
}
)
assert_equal 'Displaying <b>1</b> utilisateur', view.page_entries_info(users, entry_name: 'utilisateur')
ensure
I18n.backend.reload!
Copy link
Member

@yuki24 yuki24 Jul 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using begin...ensure...end, I think it makes more sense to do this with setup do ... end and call a I18n.backend.reload! in teardown do ... end.

end
end
end

test 'having multiple entries with default entry name' do
users = User.page(1).per(50)
begin
I18n.backend.store_translations(:fr,
helpers: {
page_entries_info: {
one_page: {
display_entries: {
one: "Displaying <b>1</b> %{entry_name}",
other: "Displaying <b>all %{count}</b> %{entry_name}"
}
}
}
}
)
assert_equal 'Displaying <b>all 50</b> utilisateurs', view.page_entries_info(users, entry_name: 'utilisateur')
ensure
I18n.backend.reload!
end
end

test 'the last page with default entry name' do
User.max_pages 4
users = User.page(4).per(10)
begin
I18n.backend.store_translations(:fr,
helpers: {
page_entries_info: {
more_pages: {
display_entries: "Displaying %{entry_name} <b>%{first}&nbsp;-&nbsp;%{last}</b> of <b>%{total}</b> in total"
}
}
}
)
assert_equal 'Displaying utilisateurs <b>31&nbsp;-&nbsp;40</b> of <b>50</b> in total', view.page_entries_info(users, entry_name: 'utilisateur')
ensure
I18n.backend.reload!
end
end
end
end

sub_test_case 'on a model with namespace' do
Expand Down