Skip to content

Commit 392c65f

Browse files
committed
Add pluralization rules for non-English-like locales
1 parent 1c1e091 commit 392c65f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+1906
-95
lines changed

.rspec

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--colour

Rakefile

+8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ RSpec::Core::RakeTask.new(:spec) do |spec|
1818
spec.pattern = FileList['spec/**/*_spec.rb']
1919
end
2020

21+
RSpec::Core::RakeTask.new("spec:unit") do |spec|
22+
spec.pattern = 'spec/unit/**/*_spec.rb'
23+
end
24+
25+
RSpec::Core::RakeTask.new("spec:integration") do |spec|
26+
spec.pattern = 'spec/integration/**/*_spec.rb'
27+
end
28+
2129
RSpec::Core::RakeTask.new(:rcov) do |spec|
2230
spec.pattern = 'spec/**/*_spec.rb'
2331
spec.rcov = true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Originally was implemented by Yaroslav Markin in "russian" gem
2+
# (http://github.com/yaroslav/russian)
3+
#
4+
# Used for Belarusian, Bosnian, Croatian, Russian, Serbian, Serbo-Croatian, Ukrainian.
5+
6+
module RailsI18n
7+
module Pluralization
8+
module EastSlavic
9+
def self.rule
10+
lambda do |n|
11+
mod10 = n % 10
12+
mod100 = n % 100
13+
14+
if mod10 == 1 && mod100 != 11
15+
:one
16+
elsif [2, 3, 4].include?(mod10) && ![12, 13, 14].include?(mod100)
17+
:few
18+
elsif mod10 == 0 || (5..9).to_a.include?(mod10) || (11..14).to_a.include?(mod100)
19+
:many
20+
else
21+
:other
22+
end
23+
end
24+
end
25+
26+
def self.with_locale(locale)
27+
{ locale => {
28+
:'i18n' => {
29+
:plural => {
30+
:keys => [:one, :few, :many, :other],
31+
:rule => rule }}}}
32+
end
33+
end
34+
end
35+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Used for Cornish, Inari Sami, Inuktitut, Lule Sami, Nama, Northern Sami,
2+
# Sami Language, Skolt Sami, Southern Sami.
3+
4+
module RailsI18n
5+
module Pluralization
6+
module OneTwoOther
7+
def self.rule
8+
def self.rule
9+
lambda do |n|
10+
if n == 1
11+
:one
12+
elsif n == 2
13+
:two
14+
else
15+
:other
16+
end
17+
end
18+
end
19+
end
20+
21+
def self.with_locale(locale)
22+
{ locale => {
23+
:'i18n' => {
24+
:plural => {
25+
:keys => [:one, :two, :other],
26+
:rule => rule }}}}
27+
end
28+
end
29+
end
30+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Used for French, Fulah, Kabyle.
2+
3+
module RailsI18n
4+
module Pluralization
5+
module OneUptoTwoOther
6+
def self.rule
7+
lambda { |n| (0...2).cover?(n) ? :one : :other }
8+
end
9+
10+
def self.with_locale(locale)
11+
{ locale => {
12+
:'i18n' => {
13+
:plural => {
14+
:keys => [:one, :other],
15+
:rule => rule }}}}
16+
end
17+
end
18+
end
19+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Used in Akan, Amharic, Bihari, Filipino, guw, Hindi, Lingala, Malagasy,
2+
# Northen Sotho, Tachelhit, Tagalog, Tigrinya, Walloon.
3+
4+
module RailsI18n
5+
module Pluralization
6+
module OneWithZeroOther
7+
def self.rule
8+
lambda { |n| n == 0 || n == 1 ? :one : :other }
9+
end
10+
11+
def self.with_locale(locale)
12+
{ locale => {
13+
:'i18n' => {
14+
:plural => {
15+
:keys => [:one, :other],
16+
:rule => rule }}}}
17+
end
18+
end
19+
end
20+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module RailsI18n
2+
module Pluralization
3+
module Other
4+
def self.rule
5+
Proc.new { :other }
6+
end
7+
8+
def self.with_locale(locale)
9+
{ locale => {
10+
:'i18n' => {
11+
:plural => {
12+
:keys => [:other],
13+
:rule => rule }}}}
14+
end
15+
end
16+
end
17+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Used for Moldavian, Romanian.
2+
3+
module RailsI18n
4+
module Pluralization
5+
module Romanian
6+
def self.rule
7+
lambda do |n|
8+
if n == 1
9+
:one
10+
elsif n == 0 || (1..19).to_a.include?(n % 100)
11+
:few
12+
else
13+
:other
14+
end
15+
end
16+
end
17+
18+
def self.with_locale(locale)
19+
{ locale => {
20+
:'i18n' => {
21+
:plural => {
22+
:keys => [:one, :few, :other],
23+
:rule => rule }}}}
24+
end
25+
end
26+
end
27+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Used for Czech, Slovak.
2+
3+
module RailsI18n
4+
module Pluralization
5+
module WestSlavic
6+
def self.rule
7+
lambda do |n|
8+
if n == 1
9+
:one
10+
elsif [2, 3, 4].include?(n)
11+
:few
12+
else
13+
:other
14+
end
15+
end
16+
end
17+
18+
def self.with_locale(locale)
19+
{ locale => {
20+
:'i18n' => {
21+
:plural => {
22+
:keys => [:one, :few, :other],
23+
:rule => rule }}}}
24+
end
25+
end
26+
end
27+
end

lib/rails_i18n/railtie.rb

+20-5
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,27 @@
22

33
module RailsI18n
44
class Railtie < ::Rails::Railtie #:nodoc:
5-
initializer 'rails-i18n' do |app|
6-
I18n.load_path << Dir[File.join(File.expand_path(File.dirname(__FILE__) + '/../../rails/locale'), '*.{rb,yml}')]
7-
if defined? ::WillPaginate
8-
I18n.load_path << Dir[File.join(File.expand_path(File.dirname(__FILE__) + '/../../will_paginate'), '*.{rb,yml}')]
5+
initializer 'rails-i18n' do
6+
RailsI18n::Railtie.instance_eval do
7+
add('rails/locale/*.yml')
8+
add('rails/pluralization/*.rb')
9+
if defined? ::WillPaginate
10+
add('will_paginate/*.yml')
11+
end
12+
13+
init_pluralization_module
914
end
10-
I18n.load_path.flatten!
15+
end
16+
17+
protected
18+
19+
def self.add(pattern)
20+
files = Dir[File.join(File.dirname(__FILE__), '../..', pattern)]
21+
I18n.load_path.concat(files)
22+
end
23+
24+
def self.init_pluralization_module
25+
I18n.backend.class.send(:include, I18n::Backend::Pluralization)
1126
end
1227
end
1328
end

rails-i18n.gemspec

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ Gem::Specification.new do |s|
99
s.summary = "Common locale data and translations for Rails i18n."
1010
s.description = "A set of common locale data and translations to internationalize and/or localize your Rails applications."
1111

12-
s.files = Dir.glob("lib/**/*") + Dir.glob("rails/locale/*") + Dir.glob("will_paginate/*") + %w(README.md MIT-LICENSE.txt)
12+
s.files = Dir.glob("lib/**/*") + Dir.glob("rails/locale/*") +
13+
Dir.glob("rails/pluralization/*") + Dir.glob("rails/transliteration/*") +
14+
Dir.glob("will_paginate/*") + %w(README.md MIT-LICENSE.txt)
1315
s.platform = Gem::Platform::RUBY
1416
s.require_path = 'lib'
1517
s.rubyforge_project = '[none]'
@@ -20,4 +22,5 @@ Gem::Specification.new do |s|
2022
s.add_development_dependency "rspec-rails", ">= 2.7.0"
2123
s.add_development_dependency "i18n-spec", ">= 0.1.1"
2224
s.add_development_dependency "will_paginate", ">= 3.0.0"
25+
s.add_development_dependency "spork", "~> 1.0rc"
2326
end

rails/pluralization/ak.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require 'rails_i18n/common_pluralizations/one_with_zero_other'
2+
3+
::RailsI18n::Pluralization::OneWithZeroOther.with_locale(:ak)

rails/pluralization/am.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require 'rails_i18n/common_pluralizations/one_with_zero_other'
2+
3+
::RailsI18n::Pluralization::OneWithZeroOther.with_locale(:am)

rails/pluralization/ar.rb

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module RailsI18n
2+
module Pluralization
3+
module Arabic
4+
def self.rule
5+
lambda do |n|
6+
mod100 = n % 100
7+
8+
if n == 0
9+
:zero
10+
elsif n == 1
11+
:one
12+
elsif n == 2
13+
:two
14+
elsif (3..10).to_a.include?(mod100)
15+
:few
16+
elsif (11..99).to_a.include?(mod100)
17+
:many
18+
else
19+
:other
20+
end
21+
end
22+
end
23+
end
24+
end
25+
end
26+
27+
{ :ar => {
28+
:'i18n' => {
29+
:plural => {
30+
:keys => [:zero, :one, :two, :few, :many, :other],
31+
:rule => RailsI18n::Pluralization::Arabic.rule }}}}

rails/pluralization/az.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require 'rails_i18n/common_pluralizations/other'
2+
3+
::RailsI18n::Pluralization::Other.with_locale(:az)

rails/pluralization/be.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require 'rails_i18n/common_pluralizations/east_slavic'
2+
3+
::RailsI18n::Pluralization::EastSlavic.with_locale(:be)

rails/pluralization/bh.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require 'rails_i18n/common_pluralizations/one_with_zero_other'
2+
3+
::RailsI18n::Pluralization::OneWithZeroOther.with_locale(:bh)

rails/pluralization/bm.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require 'rails_i18n/common_pluralizations/other'
2+
3+
::RailsI18n::Pluralization::Other.with_locale(:bm)

rails/pluralization/bo.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require 'rails_i18n/common_pluralizations/other'
2+
3+
::RailsI18n::Pluralization::Other.with_locale(:bo)

rails/pluralization/br.rb

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module RailsI18n
2+
module Pluralization
3+
module Breton
4+
def self.rule
5+
lambda do |n|
6+
mod10 = n % 10
7+
mod100 = n % 100
8+
9+
if mod10 == 1 && ![11,71,91].include?(mod100)
10+
:one
11+
elsif mod10 == 2 && ![12,72,92].include?(mod100)
12+
:two
13+
elsif [3,4,9].include?(mod10) && !((10..19).to_a + (70..79).to_a + (90..99).to_a).include?(mod100)
14+
:few
15+
elsif n % 1000000 == 0 && n != 0
16+
:many
17+
else
18+
:other
19+
end
20+
end
21+
end
22+
end
23+
end
24+
end
25+
26+
{ :br => {
27+
:'i18n' => {
28+
:plural => {
29+
:keys => [:one, :two, :few, :many, :other],
30+
:rule => RailsI18n::Pluralization::Breton.rule }}}}

rails/pluralization/bs.rb

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
# Bosnia and Herzegovina (Bosnian) pluralization rule implementation for rails
2-
# Picked up from https://github.com/svenfuchs/i18n/blob/master/test/test_data/locales/plurals.rb
3-
#
4-
# In order for this to work, add folowing to application.rb
5-
# I18n::Backend::Simple.include(I18n::Backend::Pluralization)
1+
require 'rails_i18n/common_pluralizations/east_slavic'
62

7-
{ :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 } } } } }
3+
::RailsI18n::Pluralization::EastSlavic.with_locale(:bs)

rails/pluralization/cs.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require 'rails_i18n/common_pluralizations/west_slavic'
2+
3+
::RailsI18n::Pluralization::WestSlavic.with_locale(:cs)

rails/pluralization/cy.rb

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module RailsI18n
2+
module Pluralization
3+
module Welsh
4+
def self.rule
5+
lambda do |n|
6+
case n
7+
when 0 then :zero
8+
when 1 then :one
9+
when 2 then :two
10+
when 3 then :few
11+
when 6 then :many
12+
else :other
13+
end
14+
end
15+
end
16+
end
17+
end
18+
end
19+
20+
{ :cy => {
21+
:'i18n' => {
22+
:plural => {
23+
:keys => [:zero, :one, :two, :few, :many, :other],
24+
:rule => RailsI18n::Pluralization::Welsh.rule }}}}

rails/pluralization/dz.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require 'rails_i18n/common_pluralizations/other'
2+
3+
::RailsI18n::Pluralization::Other.with_locale(:dz)

0 commit comments

Comments
 (0)