russian 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -5,7 +5,7 @@ require 'rubygems/specification'
5
5
  require 'date'
6
6
 
7
7
  GEM = "russian"
8
- GEM_VERSION = "0.0.4"
8
+ GEM_VERSION = "0.0.5"
9
9
  AUTHOR = "Yaroslav Markin"
10
10
  EMAIL = "yaroslav@markin.net"
11
11
  HOMEPAGE = "http://github.com/yaroslav/russian/"
@@ -22,7 +22,7 @@ module Russian
22
22
  module VERSION
23
23
  MAJOR = 0
24
24
  MINOR = 0
25
- TINY = 4
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 (3 entries) to a Hash that can be used
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[1]
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
- # Examples
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
- if n.modulo(10) == 1 && n.modulo(100) != 11
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 (n.modulo(10) >=2 && n.modulo(10) <= 4) && !(n.modulo(100) >=12 && n.modulo(100) <= 14)
25
+ elsif (modulo10 == 2 || modulo10 == 3 || modulo10 == 4) && !(modulo100 == 12 || modulo100 == 13 || modulo100 == 14)
23
26
  :few
24
- elsif n.modulo(10) == 0 || (n.modulo(10) >=5 && n.modulo(10) <= 9) ||
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 = {:one => 'вещь', :few => 'вещи', :many => 'вещей', :other => 'вещи'}
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, 21).should == 'вещь'
16
- @backend.send(:pluralize, :'ru-RU', @hash, 29).should == 'вещей'
17
- @backend.send(:pluralize, :'ru-RU', @hash, 129).should == 'вещей'
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
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: russian
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yaroslav Markin