Module: Pagy::I18n::P11n

Defined in:
lib/pagy/i18n.rb

Overview

Pluralization rules

Constant Summary collapse

RULE =

Store the proc defining each pluralization RULE Logic adapted from github.com/svenfuchs/rails-i18n

{
  arabic:
    lambda do |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 = 0|
      mod10  = n % 10
      mod100 = n % 100
      case
      when mod10 == 1 && mod100 != 11                                            then 'one'
      when from2to4.include?(mod10) && !from12to14.include?(mod100)              then 'few'
      when mod10 == 0 || from5to9.include?(mod10) || from11to14.include?(mod100) then 'many' # rubocop:disable Style/NumericPredicate
      else                                                                            'other'
      end
    end,

  one_other:
    ->(n) { n == 1 ? 'one' : 'other' }, # default RULE

  one_two_other:
    lambda do |n|
      case n
      when 1 then 'one'
      when 2 then 'two'
      else        'other'
      end
    end,

  one_upto_two_other:
    ->(n) { n && n >= 0 && n < 2 ? 'one' : 'other' },

  other:
    ->(*) { 'other' },

  polish:
    lambda do |n = 0|
      mod10  = n % 10
      mod100 = n % 100
      case
      when n == 1                                                               then 'one'
      when from2to4.include?(mod10) && !from12to14.include?(mod100)             then 'few'
      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 *from2to4 then 'few'
      else                'other'
      end
    end

}.freeze
LOCALE =

Store the RULE to apply to each LOCALE the :one_other RULE is the default for locales missing from this list

Hash.new(RULE[:one_other]).tap do |hash|
  hash['ar']    = RULE[:arabic]
  hash['bs']    = RULE[:east_slavic]
  hash['cs']    = RULE[:west_slavic]
  hash['id']    = RULE[:other]
  hash['fr']    = RULE[:one_upto_two_other]
  hash['hr']    = RULE[:east_slavic]
  hash['ja']    = RULE[:other]
  hash['km']    = RULE[:other]
  hash['ko']    = RULE[:other]
  hash['pl']    = RULE[:polish]
  hash['ru']    = RULE[:east_slavic]
  hash['sr']    = RULE[:east_slavic]
  hash['sv']    = RULE[:one_two_other]
  hash['sv-SE'] = RULE[:one_two_other]
  hash['tr']    = RULE[:other]
  hash['uk']    = RULE[:east_slavic]
  hash['zh-CN'] = RULE[:other]
  hash['zh-HK'] = RULE[:other]
  hash['zh-TW'] = RULE[:other]
end.freeze