Skip to content

Commit

Permalink
Add arabic locale, pluralization and tests; alpha ordered tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Jun 23, 2021
1 parent abb95a8 commit 1b95674
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 31 deletions.
26 changes: 26 additions & 0 deletions lib/locales/ar.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# :arabic pluralization (see https://github.com/ddnexus/pagy/blob/master/lib/locales/utils/p11n.rb)

ar:
pagy:

item_name:
zero:
one: "عنصر"
two:
few:
many:
other: "عناصر"

nav:
prev: "‹ السابق"
next: "التالي ›"
gap: "…"

info:
no_items: "لا يوجد %{item_name}"
single_page: "عرض <b>%{count}</b> %{item_name}"
multiple_pages: "عرض %{item_name} <b>%{from}-%{to}</b> من اجمالي <b>%{count}</b>"

combo_nav_js: "الصفحة %{page_input} من %{pages}"

items_selector_js: "عرض %{items_input} %{item_name} لكل صفحة"
32 changes: 24 additions & 8 deletions lib/locales/utils/p11n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@
p11n = {
one_other: -> (n){ n == 1 ? 'one' : 'other' }, # default

arabic: lambda do |n|
n ||= 0
mod100 = n % 100

case
when n == 0 then 'zero' # rubocop:disable Style/NumericPredicate
when n == 1 then 'one'
when n == 2 then 'two'
when (3..10).to_a.include?(mod100) then 'few'
when (11..99).to_a.include?(mod100) then 'many'
else 'other'
end
end,

east_slavic: lambda do |n|
n ||= 0
mod10 = n % 10
Expand All @@ -32,14 +46,6 @@
end
end,

west_slavic: lambda do |n|
case n
when 1 then 'one'
when 2, 3, 4 then 'few'
else 'other'
end
end,

one_two_other: lambda do |n|
case n
when 1 then 'one'
Expand All @@ -63,14 +69,24 @@
when (from0to1 + from5to9).include?(mod10) || from12to14.include?(mod100) then 'many'
else 'other'
end
end,

west_slavic: lambda do |n|
case n
when 1 then 'one'
when 2, 3, 4 then 'few'
else 'other'
end
end

}

# Hash of locale/pluralization pairs
# It contains all the entries for all the locales defined as dictionaries.
# The default pluralization for locales not explicitly listed here
# is the :one_other pluralization proc (used for English)
plurals = Hash.new(p11n[:one_other]).tap do |hash|
hash['ar'] = p11n[:arabic]
hash['bs'] = p11n[:east_slavic]
hash['cs'] = p11n[:west_slavic]
hash['id'] = p11n[:other]
Expand Down
1 change: 1 addition & 0 deletions pagy.manifest
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
lib/config/pagy.rb
lib/javascripts/pagy.js
lib/locales/ar.yml
lib/locales/bg.yml
lib/locales/bs.yml
lib/locales/ca.yml
Expand Down
74 changes: 51 additions & 23 deletions test/locales/utils/p11n_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,57 @@
describe 'locales/utils/p11n' do
let(:p11n) { eval(Pagy.root.join('locales', 'utils', 'p11n.rb').read)[1].freeze } #rubocop:disable Security/Eval

describe :arabic do
it "detects that 0 belongs to 'zero'" do
_(p11n[:arabic].call(0)).must_equal 'zero'
end
it "detects that 1 belongs to 'one'" do
_(p11n[:arabic].call(1)).must_equal 'one'
end
it "detects that 2 belongs to 'two'" do
_(p11n[:arabic].call(2)).must_equal 'two'
end
[3, 4, 103, 208, 210, 807].each do |count|
it "detects that #{count} belongs to 'few'" do
_(p11n[:arabic].call(count)).must_equal 'few'
end
end
[11, 112, 280, 344, 523, 699, 820, 25, 27, 936].each do |count|
it "detects that #{count} belongs to 'many'" do
_(p11n[:arabic].call(count)).must_equal 'many'
end
end
[101, 102, 801, 1.2, 3.7, 11.5, 20.8, 1004.3].each do |count|
it "detects that #{count} in ru is 'other'" do
_(p11n[:arabic].call(count)).must_equal 'other'
end
end
end

describe :east_slavic do
[1, 21, 51, 71, 101, 1031].each do |count|
it "detects that #{count} belongs to 'one'" do
_(p11n[:east_slavic].call(count)).must_equal 'one'
end
end
[2, 3, 4, 22, 23, 24, 92, 93, 94].each do |count|
it "detects that #{count} belongs to 'few'" do
_(p11n[:east_slavic].call(count)).must_equal 'few'
end
end
[0, 5, 8, 10, 11, 18, 20, 25, 27, 30, 35, 38, 40].each do |count|
it "detects that #{count} belongs to 'many'" do
_(p11n[:east_slavic].call(count)).must_equal 'many'
end
end
[1.2, 3.7, 11.5, 20.8, 1004.3].each do |count|
it "detects that #{count} in ru is 'other'" do
_(p11n[:east_slavic].call(count)).must_equal 'other'
end
end
end

# default
describe :one_other do
it "detects that 1 belongs to 'one'" do
_(p11n[:one_other].call(1)).must_equal 'one'
Expand Down Expand Up @@ -53,29 +104,6 @@
end
end

describe :east_slavic do
[1, 21, 51, 71, 101, 1031].each do |count|
it "detects that #{count} belongs to 'one'" do
_(p11n[:east_slavic].call(count)).must_equal 'one'
end
end
[2, 3, 4, 22, 23, 24, 92, 93, 94].each do |count|
it "detects that #{count} belongs to 'few'" do
_(p11n[:east_slavic].call(count)).must_equal 'few'
end
end
[0, 5, 8, 10, 11, 18, 20, 25, 27, 30, 35, 38, 40].each do |count|
it "detects that #{count} belongs to 'many'" do
_(p11n[:east_slavic].call(count)).must_equal 'many'
end
end
[1.2, 3.7, 11.5, 20.8, 1004.3].each do |count|
it "detects that #{count} in ru is 'other'" do
_(p11n[:east_slavic].call(count)).must_equal 'other'
end
end
end

describe :polish do
it "detects that 1 belongs to 'one'" do
_(p11n[:polish].call(1)).must_equal 'one'
Expand Down

0 comments on commit 1b95674

Please sign in to comment.