russian 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.textile +2 -0
- data/Rakefile +1 -1
- data/lib/russian.rb +9 -4
- data/lib/russian/locale/pluralize.rb +11 -9
- data/spec/i18n/locale/pluralization_spec.rb +16 -13
- data/spec/russian_spec.rb +16 -1
- metadata +1 -1
data/README.textile
CHANGED
@@ -177,6 +177,8 @@ Russian.p(2, "вещь", "вещи", "вещей")
|
|
177
177
|
=> "вещи"
|
178
178
|
Russian.p(10, "вещь", "вещи", "вещей")
|
179
179
|
=> "вещей"
|
180
|
+
Russian.p(3.14, "вещь", "вещи", "вещей", "вещи") # последний вариант используется для дробных величин
|
181
|
+
=> "вещи"
|
180
182
|
</code></pre>
|
181
183
|
|
182
184
|
-- упрощенная (без использования хешей I18n) плюрализация для русского языка
|
data/Rakefile
CHANGED
data/lib/russian.rb
CHANGED
@@ -22,7 +22,7 @@ module Russian
|
|
22
22
|
module VERSION
|
23
23
|
MAJOR = 0
|
24
24
|
MINOR = 0
|
25
|
-
TINY =
|
25
|
+
TINY = 5
|
26
26
|
|
27
27
|
STRING = [MAJOR, MINOR, TINY].join('.')
|
28
28
|
end
|
@@ -46,7 +46,9 @@ module Russian
|
|
46
46
|
# Init Russian i18n: set custom backend, set default locale to Russian locale, load all translations
|
47
47
|
# shipped with library.
|
48
48
|
def init_i18n
|
49
|
+
loaded_translations = I18n.backend.__send__(:translations)
|
49
50
|
I18n.backend = Russian.i18n_backend_class.new
|
51
|
+
loaded_translations.each_pair { |locale, data| I18n.backend.store_translations(locale, data) }
|
50
52
|
I18n.default_locale = LOCALE
|
51
53
|
locale_files.each { |file| I18n.backend.load_translations(file) }
|
52
54
|
end
|
@@ -72,7 +74,11 @@ module Russian
|
|
72
74
|
#
|
73
75
|
# Usage:
|
74
76
|
# Russian.pluralize(1, "вещь", "вещи", "вещей")
|
77
|
+
# Russian.pluralize(3.14, "вещь", "вещи", "вещей", "вещи")
|
75
78
|
def pluralize(n, *variants)
|
79
|
+
raise ArgumentError, "Must have a Numeric as a first parameter" unless n.is_a?(Numeric)
|
80
|
+
raise ArgumentError, "Must have at least 3 variants for pluralization" if variants.size < 3
|
81
|
+
raise ArgumentError, "Must have at least 4 variants for pluralization" if (variants.size < 4 && n != n.round)
|
76
82
|
variants_hash = pluralization_variants_to_hash(*variants)
|
77
83
|
I18n.backend.send(:pluralize, LOCALE, variants_hash, n)
|
78
84
|
end
|
@@ -84,15 +90,14 @@ module Russian
|
|
84
90
|
Dir[File.join(File.dirname(__FILE__), "russian", "locale", "**/*")]
|
85
91
|
end
|
86
92
|
|
87
|
-
# Converts an array of pluralization variants
|
93
|
+
# Converts an array of pluralization variants to a Hash that can be used
|
88
94
|
# with I18n pluralization.
|
89
95
|
def pluralization_variants_to_hash(*variants)
|
90
|
-
raise ArgumentError, "Must have at least 3 variants for pluralization" if variants.size < 3
|
91
96
|
{
|
92
97
|
:one => variants[0],
|
93
98
|
:few => variants[1],
|
94
99
|
:many => variants[2],
|
95
|
-
:other => variants[
|
100
|
+
:other => variants[3]
|
96
101
|
}
|
97
102
|
end
|
98
103
|
end
|
@@ -11,18 +11,20 @@
|
|
11
11
|
# many -> n mod 10 is 0 or n mod 10 in 5..9 or n mod 100 in 11..14;
|
12
12
|
# other -> everything else
|
13
13
|
#
|
14
|
-
#
|
14
|
+
# Пример
|
15
15
|
#
|
16
|
-
# one = 1, 21, 31, 41, 51, 61...
|
17
|
-
# few = 2-4, 22-24, 32-34...
|
18
|
-
# many = 0, 5-20, 25-30, 35-40...
|
19
|
-
# other = 1.31, 2.31, 5.31...
|
20
|
-
|
16
|
+
# :one = 1, 21, 31, 41, 51, 61...
|
17
|
+
# :few = 2-4, 22-24, 32-34...
|
18
|
+
# :many = 0, 5-20, 25-30, 35-40...
|
19
|
+
# :other = 1.31, 2.31, 5.31...
|
20
|
+
modulo10 = n.modulo(10)
|
21
|
+
modulo100 = n.modulo(100)
|
22
|
+
|
23
|
+
if modulo10 == 1 && modulo100 != 11
|
21
24
|
:one
|
22
|
-
elsif (
|
25
|
+
elsif (modulo10 == 2 || modulo10 == 3 || modulo10 == 4) && !(modulo100 == 12 || modulo100 == 13 || modulo100 == 14)
|
23
26
|
:few
|
24
|
-
elsif
|
25
|
-
(n.modulo(100) >= 11 && n.modulo(100) <= 14)
|
27
|
+
elsif modulo10 == 0 || (modulo10 == 5 || modulo10 == 6 || modulo10 == 7 || modulo10 == 8 || modulo10 == 9) || (modulo100 == 11 || modulo100 == 12 || modulo100 == 13 || modulo100 == 14)
|
26
28
|
:many
|
27
29
|
else
|
28
30
|
:other
|
@@ -2,22 +2,25 @@ require File.dirname(__FILE__) + '/../../spec_helper'
|
|
2
2
|
|
3
3
|
describe I18n, "Russian pluralization" do
|
4
4
|
before(:each) do
|
5
|
-
@hash = {
|
5
|
+
@hash = {}
|
6
|
+
%w(one few many other).each do |key|
|
7
|
+
@hash[key.to_sym] = key
|
8
|
+
end
|
6
9
|
@backend = I18n.backend
|
7
10
|
end
|
8
11
|
|
9
12
|
it "should pluralize correctly" do
|
10
|
-
@backend.send(:pluralize, :'ru-RU', @hash, 1).should == '
|
11
|
-
@backend.send(:pluralize, :'ru-RU', @hash, 2).should == '
|
12
|
-
@backend.send(:pluralize, :'ru-RU', @hash, 3).should == '
|
13
|
-
@backend.send(:pluralize, :'ru-RU', @hash, 5).should == '
|
14
|
-
@backend.send(:pluralize, :'ru-RU', @hash, 10).should == '
|
15
|
-
@backend.send(:pluralize, :'ru-RU', @hash,
|
16
|
-
@backend.send(:pluralize, :'ru-RU', @hash,
|
17
|
-
@backend.send(:pluralize, :'ru-RU', @hash,
|
18
|
-
@backend.send(:pluralize, :'ru-RU', @hash, 131).should == '
|
19
|
-
@backend.send(:pluralize, :'ru-RU', @hash, 1.31).should == '
|
20
|
-
@backend.send(:pluralize, :'ru-RU', @hash, 2.31).should == '
|
21
|
-
@backend.send(:pluralize, :'ru-RU', @hash, 3.31).should == '
|
13
|
+
@backend.send(:pluralize, :'ru-RU', @hash, 1).should == 'one'
|
14
|
+
@backend.send(:pluralize, :'ru-RU', @hash, 2).should == 'few'
|
15
|
+
@backend.send(:pluralize, :'ru-RU', @hash, 3).should == 'few'
|
16
|
+
@backend.send(:pluralize, :'ru-RU', @hash, 5).should == 'many'
|
17
|
+
@backend.send(:pluralize, :'ru-RU', @hash, 10).should == 'many'
|
18
|
+
@backend.send(:pluralize, :'ru-RU', @hash, 11).should == 'many'
|
19
|
+
@backend.send(:pluralize, :'ru-RU', @hash, 21).should == 'one'
|
20
|
+
@backend.send(:pluralize, :'ru-RU', @hash, 29).should == 'many'
|
21
|
+
@backend.send(:pluralize, :'ru-RU', @hash, 131).should == 'one'
|
22
|
+
@backend.send(:pluralize, :'ru-RU', @hash, 1.31).should == 'other'
|
23
|
+
@backend.send(:pluralize, :'ru-RU', @hash, 2.31).should == 'other'
|
24
|
+
@backend.send(:pluralize, :'ru-RU', @hash, 3.31).should == 'other'
|
22
25
|
end
|
23
26
|
end
|
data/spec/russian_spec.rb
CHANGED
@@ -31,6 +31,12 @@ describe Russian do
|
|
31
31
|
I18n.backend.class.should == Russian.i18n_backend_class
|
32
32
|
end
|
33
33
|
|
34
|
+
it "should keep existing translations while switching backends" do
|
35
|
+
I18n.backend.store_translations(:'en-US', { :foo => "bar" })
|
36
|
+
Russian.init_i18n
|
37
|
+
I18n.t(:foo, :locale => :'en-US').should == "bar"
|
38
|
+
end
|
39
|
+
|
34
40
|
it "should set default locale to Russian locale" do
|
35
41
|
Russian.init_i18n
|
36
42
|
I18n.default_locale.should == Russian.locale
|
@@ -85,7 +91,7 @@ describe Russian do
|
|
85
91
|
describe "with pluralization" do
|
86
92
|
%w(p pluralize).each do |method|
|
87
93
|
it "'#{method}' should pluralize with variants given" do
|
88
|
-
variants = %w(вещь вещи вещей)
|
94
|
+
variants = %w(вещь вещи вещей вещи)
|
89
95
|
|
90
96
|
Russian.send(method, 1, *variants).should == "вещь"
|
91
97
|
Russian.send(method, 2, *variants).should == 'вещи'
|
@@ -96,12 +102,21 @@ describe Russian do
|
|
96
102
|
Russian.send(method, 29, *variants).should == 'вещей'
|
97
103
|
Russian.send(method, 129, *variants).should == 'вещей'
|
98
104
|
Russian.send(method, 131, *variants).should == 'вещь'
|
105
|
+
Russian.send(method, 3.14, *variants).should == 'вещи'
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should raise an exception when first parameter is not a number" do
|
109
|
+
lambda { Russian.send(method, nil, "вещь", "вещи", "вещей") }.should raise_error(ArgumentError)
|
110
|
+
lambda { Russian.send(method, "вещь", "вещь", "вещи", "вещей") }.should raise_error(ArgumentError)
|
99
111
|
end
|
100
112
|
|
101
113
|
it "should raise an exception when there are not enough variants" do
|
102
114
|
lambda { Russian.send(method, 1) }.should raise_error(ArgumentError)
|
103
115
|
lambda { Russian.send(method, 1, "вещь") }.should raise_error(ArgumentError)
|
104
116
|
lambda { Russian.send(method, 1, "вещь", "вещи") }.should raise_error(ArgumentError)
|
117
|
+
lambda { Russian.send(method, 1, "вещь", "вещи", "вещей") }.should_not raise_error(ArgumentError)
|
118
|
+
lambda { Russian.send(method, 3.14, "вещь", "вещи", "вещей") }.should raise_error(ArgumentError)
|
119
|
+
lambda { Russian.send(method, 3.14, "вещь", "вещи", "вещей", "вещи") }.should_not raise_error(ArgumentError)
|
105
120
|
end
|
106
121
|
end
|
107
122
|
end
|