Skip to content

Commit

Permalink
plurals.rb: frozen variables; added explicit 'zero' for :zero_one_few…
Browse files Browse the repository at this point in the history
…_may_other, added :pl pluralization (#111); added README.md
  • Loading branch information
ddnexus committed Jan 14, 2019
1 parent 6cf86af commit b112f30
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
15 changes: 15 additions & 0 deletions lib/locales/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Pagy locales

### Please, submit your translation!

If you are using pagy with some language missing from the dictionary files, please, submit your translation!

The `pagy.info.single_page`, `pagy.info.item_name` and `pagy.items` keys of the yaml file are pluralized according to the plural rules of the language.

If you are going to submit a dictionary, please check wether the pluralization type of your language exists in the [plurals.rb](https://github.com/ddnexus/pagy/blob/master/lib/locales/plurals.rb) file. If it is missing, please, report it. Thanks.

### Useful Links

* [Pagy I18n Documentation](https://ddnexus.github.io/pagy/api/frontend#i18n)
* [i18n extra](https://ddnexus.github.io/pagy/extra/i18n)

31 changes: 22 additions & 9 deletions lib/locales/plurals.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,36 @@
# frozen_string_literal: true

# utility variables
zero_one = ['zero', 'one']
from2to4 = (2..4)
from5to9 = (5..9)
from11to14 = (11..14)
from12to14 = (12..14)
zero_one = ['zero', 'one'].freeze
from2to4 = (2..4).freeze
from5to9 = (5..9).freeze
from11to14 = (11..14).freeze
from12to14 = (12..14).freeze

# Plurals
# A plural proc returns a plural type string based on the passed count
# Each plural proc may apply to one or more languages below
plurals = {
zero_one_other: -> (count) {zero_one[count] || 'other'},
one_few_many_other: -> (count) do
zero_one_other: -> (count) {zero_one[count] || 'other'},

zero_one_few_many_other: -> (count) do
mod10, mod100 = count % 10, count % 100
if mod10 == 1 && mod100 != 11 ; 'one'
if count == 0 ; 'zero'
elsif mod10 == 1 && mod100 != 11 ; 'one'
elsif from2to4.cover?(mod10) && !from12to14.cover?(mod100) ; 'few'
elsif mod10 == 0 || from5to9.cover?(mod10) || from11to14.cover?(mod100) ; 'many'
else 'other'
end
end,

pl: -> (count) do
mod10, mod100 = count % 10, count % 100
if count == 0 ; 'zero'
elsif count == 1 ; 'one'
elsif from2to4.cover?(mod10) && !from12to14.cover?(mod100) ; 'few'
elsif [0, 1].include?(mod10) || from5to9.cover?(mod10) || from12to14.cover?(mod100) ; 'many'
else 'other'
end
end
}

Expand All @@ -30,7 +42,8 @@
# is the :zero_one_other plural (used for English)
Hash.new(plurals[:zero_one_other]).tap do |languages|
languages['en'] = plurals[:zero_one_other]
languages['ru'] = plurals[:one_few_many_other]
languages['ru'] = plurals[:zero_one_few_many_other]
languages['pl'] = plurals[:pl]
end

# PR for other languages and plurals are very welcome. Thanks!

0 comments on commit b112f30

Please sign in to comment.