air18n 0.1.16 → 0.1.17

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.
@@ -41,6 +41,19 @@ module Air18n
41
41
  self.full_locale = locale
42
42
  end
43
43
 
44
+ # Like ActionView::Helpers::TextHelper#pluralize, except requires that
45
+ # 'count' be a real number, not a string.
46
+ #
47
+ # ActionView::Helpers::TextHelper#pluralize is inherently English-centric,
48
+ # so this method is too, and assumes that the default locale's plural forms
49
+ # are like "%{smart_count} count-is-one-form;%{smart_count} everything-else-form".
50
+ def pluralize(count, singular, plural=nil)
51
+ raise TypeError if count.is_a?(String)
52
+ default_text = "%{smart_count} #{singular}#{SmartCount::DELIMITER}%{smart_count} #{plural || singular.pluralize}"
53
+ key = "shared.pluralize.#{singular}"
54
+ translate(key, :default => default_text, :smart_count => count)
55
+ end
56
+
44
57
  # For a locale (like :en, :en-GB, :pt-PT), returns the "language"
45
58
  # part (like :en or :pt).
46
59
  # If locale is invalid or nil, returns the default language.
@@ -1,3 +1,3 @@
1
1
  module Air18n
2
- VERSION = "0.1.16"
2
+ VERSION = "0.1.17"
3
3
  end
@@ -105,6 +105,46 @@ describe Air18n do
105
105
  end
106
106
  end
107
107
 
108
+ context 'pluralize' do
109
+ it 'should work in English' do
110
+ I18n.pluralize(0, 'Review').should == '0 Reviews'
111
+ I18n.pluralize(1, 'Review').should == '1 Review'
112
+ I18n.pluralize(2, 'Review').should == '2 Reviews'
113
+ I18n.pluralize(123.5, 'Review').should == '123.5 Reviews'
114
+ end
115
+
116
+ it 'should raise an error if count is a string' do
117
+ lambda { I18n.pluralize('Review', '3') }.should raise_error
118
+ end
119
+
120
+ it 'should work in French' do
121
+ I18n.backend.store_translations(:fr, { 'shared.pluralize.Review' => '%{smart_count} Commentaire;%{smart_count} Commentaires' })
122
+ old_locale = I18n.full_locale
123
+ I18n.full_locale = :fr
124
+ I18n.pluralize(0, 'Review').should == '0 Commentaire'
125
+ I18n.pluralize(1, 'Review').should == '1 Commentaire'
126
+ I18n.pluralize(2, 'Review').should == '2 Commentaires'
127
+ I18n.pluralize(123.5, 'Review').should == '123.5 Commentaires'
128
+ I18n.full_locale = old_locale
129
+ end
130
+
131
+ it 'should work with explicitly-specified plural' do
132
+ I18n.backend.store_translations(:fr, { 'shared.pluralize.feis' => '%{smart_count} french singular;%{smart_count} french plural' })
133
+
134
+ I18n.pluralize(0, 'feis', 'feiseanna').should == '0 feiseanna'
135
+ I18n.pluralize(1, 'feis', 'feiseanna').should == '1 feis'
136
+ I18n.pluralize(2, 'feis', 'feiseanna').should == '2 feiseanna'
137
+
138
+ I18n.full_locale = :fr
139
+ old_locale = I18n.full_locale
140
+ I18n.pluralize(0, 'feis', 'feiseanna').should == '0 french singular'
141
+ I18n.pluralize(1, 'feis', 'feiseanna').should == '1 french singular'
142
+ I18n.pluralize(2, 'feis', 'feiseanna').should == '2 french plural'
143
+ I18n.pluralize(123.5, 'feis', 'feiseanna').should == '123.5 french plural'
144
+ I18n.full_locale = old_locale
145
+ end
146
+ end
147
+
108
148
  context 'robustness' do
109
149
  it 'should not barf on input arrays' do
110
150
  # Just make sure that there is no exception.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: air18n
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.16
4
+ version: 0.1.17
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: