Skip to content

Commit

Permalink
Add pluralization rules for non-English-like locales
Browse files Browse the repository at this point in the history
  • Loading branch information
nashbridges committed Feb 25, 2012
1 parent 1c1e091 commit 392c65f
Show file tree
Hide file tree
Showing 113 changed files with 1,906 additions and 95 deletions.
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--colour
8 changes: 8 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ RSpec::Core::RakeTask.new(:spec) do |spec|
spec.pattern = FileList['spec/**/*_spec.rb']
end

RSpec::Core::RakeTask.new("spec:unit") do |spec|
spec.pattern = 'spec/unit/**/*_spec.rb'
end

RSpec::Core::RakeTask.new("spec:integration") do |spec|
spec.pattern = 'spec/integration/**/*_spec.rb'
end

RSpec::Core::RakeTask.new(:rcov) do |spec|
spec.pattern = 'spec/**/*_spec.rb'
spec.rcov = true
Expand Down
35 changes: 35 additions & 0 deletions lib/rails_i18n/common_pluralizations/east_slavic.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Originally was implemented by Yaroslav Markin in "russian" gem
# (http://github.com/yaroslav/russian)
#
# Used for Belarusian, Bosnian, Croatian, Russian, Serbian, Serbo-Croatian, Ukrainian.

module RailsI18n
module Pluralization
module EastSlavic
def self.rule
lambda do |n|
mod10 = n % 10
mod100 = n % 100

if mod10 == 1 && mod100 != 11
:one
elsif [2, 3, 4].include?(mod10) && ![12, 13, 14].include?(mod100)
:few
elsif mod10 == 0 || (5..9).to_a.include?(mod10) || (11..14).to_a.include?(mod100)
:many
else
:other
end
end
end

def self.with_locale(locale)
{ locale => {
:'i18n' => {
:plural => {
:keys => [:one, :few, :many, :other],
:rule => rule }}}}
end
end
end
end
30 changes: 30 additions & 0 deletions lib/rails_i18n/common_pluralizations/one_two_other.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Used for Cornish, Inari Sami, Inuktitut, Lule Sami, Nama, Northern Sami,
# Sami Language, Skolt Sami, Southern Sami.

module RailsI18n
module Pluralization
module OneTwoOther
def self.rule
def self.rule
lambda do |n|
if n == 1
:one
elsif n == 2
:two
else
:other
end
end
end
end

def self.with_locale(locale)
{ locale => {
:'i18n' => {
:plural => {
:keys => [:one, :two, :other],
:rule => rule }}}}
end
end
end
end
19 changes: 19 additions & 0 deletions lib/rails_i18n/common_pluralizations/one_upto_two_other.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Used for French, Fulah, Kabyle.

module RailsI18n
module Pluralization
module OneUptoTwoOther
def self.rule
lambda { |n| (0...2).cover?(n) ? :one : :other }
end

def self.with_locale(locale)
{ locale => {
:'i18n' => {
:plural => {
:keys => [:one, :other],
:rule => rule }}}}
end
end
end
end
20 changes: 20 additions & 0 deletions lib/rails_i18n/common_pluralizations/one_with_zero_other.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Used in Akan, Amharic, Bihari, Filipino, guw, Hindi, Lingala, Malagasy,
# Northen Sotho, Tachelhit, Tagalog, Tigrinya, Walloon.

module RailsI18n
module Pluralization
module OneWithZeroOther
def self.rule
lambda { |n| n == 0 || n == 1 ? :one : :other }
end

def self.with_locale(locale)
{ locale => {
:'i18n' => {
:plural => {
:keys => [:one, :other],
:rule => rule }}}}
end
end
end
end
17 changes: 17 additions & 0 deletions lib/rails_i18n/common_pluralizations/other.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module RailsI18n
module Pluralization
module Other
def self.rule
Proc.new { :other }
end

def self.with_locale(locale)
{ locale => {
:'i18n' => {
:plural => {
:keys => [:other],
:rule => rule }}}}
end
end
end
end
27 changes: 27 additions & 0 deletions lib/rails_i18n/common_pluralizations/romanian.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Used for Moldavian, Romanian.

module RailsI18n
module Pluralization
module Romanian
def self.rule
lambda do |n|
if n == 1
:one
elsif n == 0 || (1..19).to_a.include?(n % 100)
:few
else
:other
end
end
end

def self.with_locale(locale)
{ locale => {
:'i18n' => {
:plural => {
:keys => [:one, :few, :other],
:rule => rule }}}}
end
end
end
end
27 changes: 27 additions & 0 deletions lib/rails_i18n/common_pluralizations/west_slavic.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Used for Czech, Slovak.

module RailsI18n
module Pluralization
module WestSlavic
def self.rule
lambda do |n|
if n == 1
:one
elsif [2, 3, 4].include?(n)
:few
else
:other
end
end
end

def self.with_locale(locale)
{ locale => {
:'i18n' => {
:plural => {
:keys => [:one, :few, :other],
:rule => rule }}}}
end
end
end
end
25 changes: 20 additions & 5 deletions lib/rails_i18n/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,27 @@

module RailsI18n
class Railtie < ::Rails::Railtie #:nodoc:
initializer 'rails-i18n' do |app|
I18n.load_path << Dir[File.join(File.expand_path(File.dirname(__FILE__) + '/../../rails/locale'), '*.{rb,yml}')]
if defined? ::WillPaginate
I18n.load_path << Dir[File.join(File.expand_path(File.dirname(__FILE__) + '/../../will_paginate'), '*.{rb,yml}')]
initializer 'rails-i18n' do
RailsI18n::Railtie.instance_eval do
add('rails/locale/*.yml')
add('rails/pluralization/*.rb')
if defined? ::WillPaginate
add('will_paginate/*.yml')
end

init_pluralization_module
end
I18n.load_path.flatten!
end

protected

def self.add(pattern)
files = Dir[File.join(File.dirname(__FILE__), '../..', pattern)]
I18n.load_path.concat(files)
end

def self.init_pluralization_module
I18n.backend.class.send(:include, I18n::Backend::Pluralization)
end
end
end
5 changes: 4 additions & 1 deletion rails-i18n.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ Gem::Specification.new do |s|
s.summary = "Common locale data and translations for Rails i18n."
s.description = "A set of common locale data and translations to internationalize and/or localize your Rails applications."

s.files = Dir.glob("lib/**/*") + Dir.glob("rails/locale/*") + Dir.glob("will_paginate/*") + %w(README.md MIT-LICENSE.txt)
s.files = Dir.glob("lib/**/*") + Dir.glob("rails/locale/*") +
Dir.glob("rails/pluralization/*") + Dir.glob("rails/transliteration/*") +
Dir.glob("will_paginate/*") + %w(README.md MIT-LICENSE.txt)
s.platform = Gem::Platform::RUBY
s.require_path = 'lib'
s.rubyforge_project = '[none]'
Expand All @@ -20,4 +22,5 @@ Gem::Specification.new do |s|
s.add_development_dependency "rspec-rails", ">= 2.7.0"
s.add_development_dependency "i18n-spec", ">= 0.1.1"
s.add_development_dependency "will_paginate", ">= 3.0.0"
s.add_development_dependency "spork", "~> 1.0rc"
end
3 changes: 3 additions & 0 deletions rails/pluralization/ak.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require 'rails_i18n/common_pluralizations/one_with_zero_other'

::RailsI18n::Pluralization::OneWithZeroOther.with_locale(:ak)
3 changes: 3 additions & 0 deletions rails/pluralization/am.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require 'rails_i18n/common_pluralizations/one_with_zero_other'

::RailsI18n::Pluralization::OneWithZeroOther.with_locale(:am)
31 changes: 31 additions & 0 deletions rails/pluralization/ar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module RailsI18n
module Pluralization
module Arabic
def self.rule
lambda do |n|
mod100 = n % 100

if n == 0
:zero
elsif n == 1
:one
elsif n == 2
:two
elsif (3..10).to_a.include?(mod100)
:few
elsif (11..99).to_a.include?(mod100)
:many
else
:other
end
end
end
end
end
end

{ :ar => {
:'i18n' => {
:plural => {
:keys => [:zero, :one, :two, :few, :many, :other],
:rule => RailsI18n::Pluralization::Arabic.rule }}}}
3 changes: 3 additions & 0 deletions rails/pluralization/az.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require 'rails_i18n/common_pluralizations/other'

::RailsI18n::Pluralization::Other.with_locale(:az)
3 changes: 3 additions & 0 deletions rails/pluralization/be.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require 'rails_i18n/common_pluralizations/east_slavic'

::RailsI18n::Pluralization::EastSlavic.with_locale(:be)
3 changes: 3 additions & 0 deletions rails/pluralization/bh.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require 'rails_i18n/common_pluralizations/one_with_zero_other'

::RailsI18n::Pluralization::OneWithZeroOther.with_locale(:bh)
3 changes: 3 additions & 0 deletions rails/pluralization/bm.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require 'rails_i18n/common_pluralizations/other'

::RailsI18n::Pluralization::Other.with_locale(:bm)
3 changes: 3 additions & 0 deletions rails/pluralization/bo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require 'rails_i18n/common_pluralizations/other'

::RailsI18n::Pluralization::Other.with_locale(:bo)
30 changes: 30 additions & 0 deletions rails/pluralization/br.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module RailsI18n
module Pluralization
module Breton
def self.rule
lambda do |n|
mod10 = n % 10
mod100 = n % 100

if mod10 == 1 && ![11,71,91].include?(mod100)
:one
elsif mod10 == 2 && ![12,72,92].include?(mod100)
:two
elsif [3,4,9].include?(mod10) && !((10..19).to_a + (70..79).to_a + (90..99).to_a).include?(mod100)
:few
elsif n % 1000000 == 0 && n != 0
:many
else
:other
end
end
end
end
end
end

{ :br => {
:'i18n' => {
:plural => {
:keys => [:one, :two, :few, :many, :other],
:rule => RailsI18n::Pluralization::Breton.rule }}}}
8 changes: 2 additions & 6 deletions rails/pluralization/bs.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
# Bosnia and Herzegovina (Bosnian) pluralization rule implementation for rails
# Picked up from https://github.com/svenfuchs/i18n/blob/master/test/test_data/locales/plurals.rb
#
# In order for this to work, add folowing to application.rb
# I18n::Backend::Simple.include(I18n::Backend::Pluralization)
require 'rails_i18n/common_pluralizations/east_slavic'

{ :bs => { :i18n => { :plural => { :rule => lambda { |n| n % 10 == 1 && n % 100 != 11 ? :one : [2, 3, 4].include?(n % 10) && ![12, 13, 14].include?(n % 100) ? :few : n % 10 == 0 || [5, 6, 7, 8, 9].include?(n % 10) || [11, 12, 13, 14].include?(n % 100) ? :many : :other } } } } }
::RailsI18n::Pluralization::EastSlavic.with_locale(:bs)
3 changes: 3 additions & 0 deletions rails/pluralization/cs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require 'rails_i18n/common_pluralizations/west_slavic'

::RailsI18n::Pluralization::WestSlavic.with_locale(:cs)
24 changes: 24 additions & 0 deletions rails/pluralization/cy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module RailsI18n
module Pluralization
module Welsh
def self.rule
lambda do |n|
case n
when 0 then :zero
when 1 then :one
when 2 then :two
when 3 then :few
when 6 then :many
else :other
end
end
end
end
end
end

{ :cy => {
:'i18n' => {
:plural => {
:keys => [:zero, :one, :two, :few, :many, :other],
:rule => RailsI18n::Pluralization::Welsh.rule }}}}
3 changes: 3 additions & 0 deletions rails/pluralization/dz.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require 'rails_i18n/common_pluralizations/other'

::RailsI18n::Pluralization::Other.with_locale(:dz)
Loading

0 comments on commit 392c65f

Please sign in to comment.