Skip to content

Commit

Permalink
Faster East Slavic pluralization
Browse files Browse the repository at this point in the history
  • Loading branch information
rwz committed Jan 3, 2014
1 parent 0b59c99 commit 5282098
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/rails_i18n/common_pluralizations/east_slavic.rb
Expand Up @@ -6,16 +6,22 @@
module RailsI18n module RailsI18n
module Pluralization module Pluralization
module EastSlavic module EastSlavic

FROM_2_TO_4 = (2..4).to_set
FROM_5_TO_9 = (5..9).to_set
FROM_11_TO_14 = (11..14).to_set
FROM_12_TO_14 = (12..14).to_set

def self.rule def self.rule
lambda do |n| lambda do |n|
mod10 = n % 10 mod10 = n % 10
mod100 = n % 100 mod100 = n % 100


if mod10 == 1 && mod100 != 11 if mod10 == 1 && mod100 != 11
:one :one
elsif [2, 3, 4].include?(mod10) && ![12, 13, 14].include?(mod100) elsif FROM_2_TO_4.include?(mod10) && !FROM_12_TO_14.include?(mod100)
:few :few
elsif mod10 == 0 || (5..9).to_a.include?(mod10) || (11..14).to_a.include?(mod100) elsif mod10 == 0 || FROM_5_TO_9.include?(mod10) || FROM_11_TO_14.include?(mod100)
:many :many
else else
:other :other
Expand All @@ -25,7 +31,7 @@ def self.rule


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

0 comments on commit 5282098

Please sign in to comment.