Sha256: 09046e5761162587546e5e085c83606b0fde4311a0e6c3d286a47b1f03bdab28

Contents?: true

Size: 863 Bytes

Versions: 7

Compression:

Stored size: 863 Bytes

Contents

module RailsI18n
  module Pluralization
    module OneFewOther
      FROM_2_TO_4   = (2..4).to_a.freeze
      FROM_12_TO_14 = (12..14).to_a.freeze

      def self.rule
        lambda do |n|
          return :other unless n.is_a?(Numeric)

          frac = (n.to_d % 1)

          if frac.nonzero?
            n = frac.to_s.split('.').last.to_i
          end

          mod10 = n % 10
          mod100 = n % 100

          if mod10 == 1 && mod100 != 11
            :one
          elsif FROM_2_TO_4.include?(mod10) && !FROM_12_TO_14.include?(mod100)
            :few
          else
            :other
          end
        end
      end

      def self.with_locale(locale)
          { locale => {
              :i18n => {
                :plural => {
                  :keys => [:one, :few, :other],
                  :rule => rule }}}}
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
rails-i18n-8.0.1 lib/rails_i18n/common_pluralizations/one_few_other.rb
rails-i18n-8.0.0 lib/rails_i18n/common_pluralizations/one_few_other.rb
rails-i18n-7.0.10 lib/rails_i18n/common_pluralizations/one_few_other.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rails-i18n-7.0.9/lib/rails_i18n/common_pluralizations/one_few_other.rb
rails-i18n-7.0.9 lib/rails_i18n/common_pluralizations/one_few_other.rb
rails-i18n-7.0.8 lib/rails_i18n/common_pluralizations/one_few_other.rb
rails-i18n-7.0.7 lib/rails_i18n/common_pluralizations/one_few_other.rb