i18n-inflector 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,249 @@
1
+ require 'test_helper'
2
+
3
+ class I18nBackendInflectionTest < Test::Unit::TestCase
4
+ class Backend < I18n::Backend::Simple
5
+ include I18n::Backend::Inflector
6
+ include I18n::Backend::Fallbacks
7
+ end
8
+
9
+ def setup
10
+ I18n.backend = Backend.new
11
+ store_translations(:xx, :i18n => { :inflections => {
12
+ :gender => {
13
+ :m => 'male',
14
+ :f => 'female',
15
+ :n => 'neuter',
16
+ :s => 'strange',
17
+ :masculine => '@m',
18
+ :feminine => '@f',
19
+ :neuter => '@n',
20
+ :neutral => '@neuter',
21
+ :default => 'neutral' },
22
+ :person => {
23
+ :i => 'I',
24
+ :you => 'You'}
25
+ } })
26
+
27
+ store_translations(:xx, 'welcome' => 'Dear @{f:Lady|m:Sir|n:You|All}!')
28
+ end
29
+
30
+ test "inflector has methods to test its switches" do
31
+ assert_equal true, I18n.backend.inflector_unknown_defaults = true
32
+ assert_equal false, I18n.backend.inflector_excluded_defaults = false
33
+ assert_equal false, I18n.backend.inflector_raises = false
34
+ assert_equal false, I18n.backend.inflector_raises?
35
+ assert_equal true, I18n.backend.inflector_unknown_defaults?
36
+ assert_equal false, I18n.backend.inflector_excluded_defaults?
37
+ end
38
+
39
+ test "inflector store_translations: regenerates inflection structures when translations are loaded" do
40
+ store_translations(:xx, :i18n => { :inflections => { :gender => { :o => 'other' }}})
41
+ store_translations(:xx, 'hi' => 'Dear @{f:Lady|o:Others|n:You|All}!')
42
+ assert_equal 'Dear Others!', I18n.t('hi', :gender => :o, :locale => :xx)
43
+ assert_equal 'Dear Lady!', I18n.t('hi', :gender => :f, :locale => :xx)
44
+ assert_equal 'Dear You!', I18n.t('hi', :gender => :unknown, :locale => :xx)
45
+ assert_equal 'Dear All!', I18n.t('hi', :gender => :m, :locale => :xx)
46
+ end
47
+
48
+ test "inflector store_translations: raises I18n::DuplicatedInflectionToken when duplicated token is given" do
49
+ assert_raise I18n::DuplicatedInflectionToken do
50
+ store_translations(:xx, :i18n => { :inflections => { :gender => { :o => 'other' }, :person => { :o => 'o' }}})
51
+ end
52
+ end
53
+
54
+ test "inflector store_translations: raises I18n::BadInflectionAlias when bad alias is given" do
55
+ assert_raise I18n::BadInflectionAlias do
56
+ store_translations(:xx, :i18n => { :inflections => { :gender => { :o => '@nonexistant' }}})
57
+ end
58
+ assert_raise I18n::BadInflectionAlias do
59
+ store_translations(:xx, :i18n => { :inflections => { :gender => { :default => '@nonexistant' }}})
60
+ end
61
+ end
62
+
63
+ test "inflector store_translations: raises I18n::BadInflectionToken when duplicated token is given" do
64
+ assert_raise I18n::BadInflectionToken do
65
+ store_translations(:xx, :i18n => { :inflections => { :gender => { :o => '@' }}})
66
+ store_translations(:xx, :i18n => { :inflections => { :gender => { :tok => nil }}})
67
+ end
68
+ end
69
+
70
+ test "inflector translate: picks Lady for :f gender option" do
71
+ assert_equal 'Dear Lady!', I18n.t('welcome', :gender => :f, :locale => :xx)
72
+ end
73
+
74
+ test "inflector translate: picks Lady for f gender option" do
75
+ assert_equal 'Dear Lady!', I18n.t('welcome', :gender => 'f', :locale => :xx)
76
+ end
77
+
78
+ test "inflector translate: picks Sir for :m gender option" do
79
+ assert_equal 'Dear Sir!', I18n.t('welcome', :gender => :m, :locale => :xx)
80
+ end
81
+
82
+ test "inflector translate: picks Sir for :masculine gender option" do
83
+ assert_equal 'Dear Sir!', I18n.t('welcome', :gender => :masculine, :locale => :xx)
84
+ end
85
+
86
+ test "inflector translate: picks Sir for masculine gender option" do
87
+ assert_equal 'Dear Sir!', I18n.t('welcome', :gender => 'masculine', :locale => :xx)
88
+ end
89
+
90
+ test "inflector translate: falls back to default for the unknown gender option" do
91
+ assert_equal 'Dear You!', I18n.t('welcome', :gender => :unknown, :locale => :xx)
92
+ end
93
+
94
+ test "inflector translate: falls back to default for a gender option set to nil" do
95
+ assert_equal 'Dear You!', I18n.t('welcome', :gender => nil, :locale => :xx)
96
+ end
97
+
98
+ test "inflector translate: falls back to default for no gender option" do
99
+ assert_equal 'Dear You!', I18n.t('welcome', :locale => :xx)
100
+ end
101
+
102
+ test "inflector translate: falls back to free text for the proper gender option but not present in pattern" do
103
+ assert_equal 'Dear All!', I18n.t('welcome', :gender => :s, :locale => :xx)
104
+ end
105
+
106
+ test "inflector translate: falls back to free text when :inflector_unknown_defaults is false" do
107
+ assert_equal 'Dear All!', I18n.t('welcome', :gender => :unknown, :locale => :xx, :inflector_unknown_defaults => false)
108
+ assert_equal 'Dear All!', I18n.t('welcome', :gender => :s, :locale => :xx, :inflector_unknown_defaults => false)
109
+ assert_equal 'Dear All!', I18n.t('welcome', :gender => nil, :locale => :xx, :inflector_unknown_defaults => false)
110
+ end
111
+
112
+ test "inflector translate: falls back to default for no gender option when :inflector_unknown_defaults is false" do
113
+ assert_equal 'Dear You!', I18n.t('welcome', :locale => :xx, :inflector_unknown_defaults => false)
114
+ end
115
+
116
+ test "inflector translate: falls back to free text for the unknown gender option when global inflector_unknown_defaults is false" do
117
+ I18n.backend.inflector_unknown_defaults = false
118
+ assert_equal 'Dear All!', I18n.t('welcome', :gender => :unknown, :locale => :xx)
119
+ end
120
+
121
+ test "inflector translate: falls back to default for the unknown gender option when global inflector_unknown_defaults is overriden" do
122
+ I18n.backend.inflector_unknown_defaults = false
123
+ assert_equal 'Dear You!', I18n.t('welcome', :gender => :unknown, :locale => :xx, :inflector_unknown_defaults => true)
124
+ end
125
+
126
+ test "inflector translate: falls back to default token for ommited gender option when :inflector_excluded_defaults is true" do
127
+ assert_equal 'Dear You!', I18n.t('welcome', :gender => :s, :locale => :xx, :inflector_excluded_defaults => true)
128
+ I18n.backend.inflector_excluded_defaults = true
129
+ assert_equal 'Dear You!', I18n.t('welcome', :gender => :s, :locale => :xx)
130
+ end
131
+
132
+ test "inflector translate: falls back to free text for ommited gender option when :inflector_excluded_defaults is false" do
133
+ assert_equal 'Dear All!', I18n.t('welcome', :gender => :s, :locale => :xx, :inflector_excluded_defaults => false)
134
+ I18n.backend.inflector_excluded_defaults = false
135
+ assert_equal 'Dear All!', I18n.t('welcome', :gender => :s, :locale => :xx)
136
+ end
137
+
138
+ test "inflector translate: works with %{} patterns" do
139
+ store_translations(:xx, 'hi' => 'Dear @{f:Lady|m:%{test}}!')
140
+ assert_equal 'Dear Dude!', I18n.t('hi', :gender => :m, :locale => :xx, :test => "Dude")
141
+ end
142
+
143
+ test "inflector translate: raises I18n::InvalidOptionForKind when bad kind is given and inflector_raises is true" do
144
+ assert_nothing_raised I18n::InvalidOptionForKind do
145
+ I18n.t('welcome', :locale => :xx, :inflector_raises => true)
146
+ end
147
+ tr = I18n.backend.send(:translations)
148
+ tr[:xx][:i18n][:inflections][:gender].delete(:default)
149
+ store_translations(:xx, :i18n => { :inflections => { :gender => { :o => 'other' }}})
150
+ assert_raise(I18n::InvalidOptionForKind) { I18n.t('welcome', :locale => :xx, :inflector_raises => true) }
151
+ assert_raise(I18n::InvalidOptionForKind) { I18n.t('welcome', :locale => :xx, :gender => "", :inflector_raises => true) }
152
+ assert_raise(I18n::InvalidOptionForKind) { I18n.t('welcome', :locale => :xx, :gender => nil, :inflector_raises => true) }
153
+ assert_raise I18n::InvalidOptionForKind do
154
+ I18n.backend.inflector_raises = true
155
+ I18n.t('welcome', :locale => :xx)
156
+ end
157
+ end
158
+
159
+ test "inflector translate: raises I18n::InvalidInflectionToken when bad token is given and inflector_raises is true" do
160
+ store_translations(:xx, 'hi' => 'Dear @{f:Lady|o:BAD_TOKEN|n:You|First}!')
161
+ assert_raise(I18n::InvalidInflectionToken) { I18n.t('hi', :locale => :xx, :inflector_raises => true) }
162
+ assert_raise I18n::InvalidInflectionToken do
163
+ I18n.backend.inflector_raises = true
164
+ I18n.t('hi', :locale => :xx)
165
+ end
166
+ end
167
+
168
+ test "inflector translate: raises I18n::MisplacedInflectionToken when bad token is given and inflector_raises is true" do
169
+ store_translations(:xx, 'hi' => 'Dear @{f:Lady|i:Me|n:You|First}!')
170
+ assert_raise(I18n::MisplacedInflectionToken) { I18n.t('hi', :locale => :xx, :inflector_raises => true) }
171
+ assert_raise I18n::MisplacedInflectionToken do
172
+ I18n.backend.inflector_raises = true
173
+ I18n.t('hi', :locale => :xx)
174
+ end
175
+ end
176
+
177
+ test "inflector inflected_locales: lists languages that support inflection" do
178
+ assert_equal [:xx], I18n.backend.inflected_locales
179
+ assert_equal [:xx], I18n.backend.inflected_locales(:gender)
180
+ end
181
+
182
+ test "inflector available_inflection_kinds: lists inflection kinds" do
183
+ assert_not_nil I18n.backend.available_inflection_kinds(:xx)
184
+ assert_equal [:gender,:person], I18n.backend.available_inflection_kinds(:xx).sort{|k,v| k.to_s<=>v.to_s}
185
+ I18n.locale = :xx
186
+ assert_equal [:gender,:person], I18n.backend.available_inflection_kinds.sort{|k,v| k.to_s<=>v.to_s}
187
+ end
188
+
189
+ test "inflector inflection_tokens: lists inflection tokens" do
190
+ h = {:m=>"male",:f=>"female",:n=>"neuter",:s=>"strange",
191
+ :masculine=>"male",:feminine=>"female",:neuter=>"neuter",
192
+ :neutral=>"neuter"}
193
+ ha = h.merge(:i=>'I', :you=>'You')
194
+ assert_equal h, I18n.backend.inflection_tokens(:gender, :xx)
195
+ I18n.locale = :xx
196
+ assert_equal h, I18n.backend.inflection_tokens(:gender)
197
+ assert_equal ha, I18n.backend.inflection_tokens
198
+ end
199
+
200
+ test "inflector inflection_true_tokens: lists true tokens" do
201
+ h = {:m=>"male",:f=>"female",:n=>"neuter",:s=>"strange"}
202
+ ha = h.merge(:i=>"I",:you=>"You")
203
+ assert_equal h, I18n.backend.inflection_true_tokens(:gender, :xx)
204
+ I18n.locale = :xx
205
+ assert_equal h, I18n.backend.inflection_true_tokens(:gender)
206
+ assert_equal ha, I18n.backend.inflection_true_tokens
207
+ end
208
+
209
+ test "inflector inflection_raw_tokens: lists tokens in a so called raw format" do
210
+ h = {:m=>"male",:f=>"female",:n=>"neuter",:s=>"strange",
211
+ :masculine=>:m,:feminine=>:f,:neuter=>:n,
212
+ :neutral=>:n}
213
+ ha = h.merge(:i=>'I',:you=>"You")
214
+ assert_equal h, I18n.backend.inflection_raw_tokens(:gender, :xx)
215
+ I18n.locale = :xx
216
+ assert_equal h, I18n.backend.inflection_raw_tokens(:gender)
217
+ assert_equal ha, I18n.backend.inflection_raw_tokens
218
+ end
219
+
220
+ test "inflector inflection_default_token: returns a default token for a kind" do
221
+ assert_equal :n, I18n.backend.inflection_default_token(:gender, :xx)
222
+ I18n.locale = :xx
223
+ assert_equal :n, I18n.backend.inflection_default_token(:gender)
224
+ end
225
+
226
+ test "inflector inflection_aliases: lists aliases" do
227
+ a = {:masculine=>:m, :feminine=>:f, :neuter=>:n, :neutral=>:n}
228
+ assert_equal a, I18n.backend.inflection_aliases(:gender, :xx)
229
+ I18n.locale = :xx
230
+ assert_equal a, I18n.backend.inflection_aliases(:gender)
231
+ assert_equal a, I18n.backend.inflection_aliases
232
+ end
233
+
234
+ test "inflector inflection_token_description: returns token's description" do
235
+ assert_equal "male", I18n.backend.inflection_token_description(:m, :xx)
236
+ I18n.locale = :xx
237
+ assert_equal "male", I18n.backend.inflection_token_description(:m)
238
+ assert_equal nil, I18n.backend.inflection_token_description(:nonexistent, :xx)
239
+ assert_equal "neuter", I18n.backend.inflection_token_description(:neutral, :xx)
240
+ end
241
+
242
+ test "inflector inflection_is_alias?: tests whether a token is an alias" do
243
+ assert_equal true, I18n.backend.inflection_is_alias?(:neutral, :xx)
244
+ assert_equal false, I18n.backend.inflection_is_alias?(:you, :xx)
245
+ I18n.locale = :xx
246
+ assert_equal true, I18n.backend.inflection_is_alias?(:neutral)
247
+ end
248
+
249
+ end
data/test/run_all.rb ADDED
@@ -0,0 +1,21 @@
1
+ def bundle_check
2
+ `bundle check` == "The Gemfile's dependencies are satisfied\n"
3
+ end
4
+
5
+ command = 'ruby -w -Ilib -Itest test/all.rb'
6
+ gemfiles = %w(ci/Gemfile)
7
+
8
+ results = gemfiles.map do |gemfile|
9
+ puts "BUNDLE_GEMFILE=#{gemfile}"
10
+ ENV['BUNDLE_GEMFILE'] = gemfile
11
+
12
+ unless bundle_check
13
+ puts "bundle install"
14
+ system('bundle install')
15
+ end
16
+
17
+ puts command
18
+ system('ruby -w -Ilib -Itest test/all.rb')
19
+ end
20
+
21
+ exit(results.inject(true) { |a, b| a && b })
@@ -0,0 +1,32 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'bundler/setup'
4
+ require 'i18n-inflector'
5
+ require 'test_declarative'
6
+
7
+ class Test::Unit::TestCase
8
+ def teardown
9
+ I18n.locale = nil
10
+ I18n.default_locale = :en
11
+ I18n.load_path = []
12
+ I18n.available_locales = nil
13
+ I18n.backend = nil
14
+ end
15
+
16
+ def translations
17
+ I18n.backend.instance_variable_get(:@translations)
18
+ end
19
+
20
+ def store_translations(*args)
21
+ data = args.pop
22
+ locale = args.pop || :en
23
+ I18n.backend.store_translations(locale, data)
24
+ end
25
+
26
+ end
27
+
28
+ Object.class_eval do
29
+ def meta_class
30
+ class << self; self; end
31
+ end
32
+ end unless Object.method_defined?(:meta_class)
data.tar.gz.sig ADDED
Binary file
metadata ADDED
@@ -0,0 +1,156 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: i18n-inflector
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
10
+ platform: ruby
11
+ authors:
12
+ - "Pawe\xC5\x82 Wilk"
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain:
16
+ - |
17
+ -----BEGIN CERTIFICATE-----
18
+ MIIDKjCCAhKgAwIBAgIBADANBgkqhkiG9w0BAQUFADA7MQ8wDQYDVQQDDAZzaWVm
19
+ Y2ExEzARBgoJkiaJk/IsZAEZFgNnbnUxEzARBgoJkiaJk/IsZAEZFgNvcmcwHhcN
20
+ MDkwNjA2MDkwODA5WhcNMTAwNjA2MDkwODA5WjA7MQ8wDQYDVQQDDAZzaWVmY2Ex
21
+ EzARBgoJkiaJk/IsZAEZFgNnbnUxEzARBgoJkiaJk/IsZAEZFgNvcmcwggEiMA0G
22
+ CSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCdk4+9ieSx2I2OPslPcj/LjajwtsrH
23
+ mev6Fs3xK9hdDIbbLuQM9AypBS7NeKP/2YToEOGxsvzcpFzL2Ah71cP6Yfn+Z2Yo
24
+ zvqpAx5/nl79PrJKvjlkdzVNOFBp/EOkLK67QK4Pv97ABnG2PkF4FokqOjuNHLM7
25
+ 47OkJPvFyfHyMBDZN7EFljBBNm3IuQRTiO48e5Jcp3L761PWOvCpnV8wiga0Wwt3
26
+ 98Gmy7c1nWzfbQc1wHwKLPICY/aidKU20KymSHG63BSW5pO2cXZecIeYjw5YNjGA
27
+ M1RZMiwT7QJ9W86VVP+8EqbJKJOS95xlmQTHjPK56yXv8GiuyLQHpPh5AgMBAAGj
28
+ OTA3MAkGA1UdEwQCMAAwHQYDVR0OBBYEFKOKspZONq4bt5D2DEexB+vsMB2GMAsG
29
+ A1UdDwQEAwIEsDANBgkqhkiG9w0BAQUFAAOCAQEAUh0LnB4o5XKpH3yOxavEyp9X
30
+ Nen2e854wsSjAr0waSVzEt3XxY1voyIE6WCGxZJU//40CR0Be7j5CcsJsDU2CZyZ
31
+ 8SXN1/mZjMqWvYyEMSfQP4XzkFSOuyDcoDAf43OGhOhdv5Jcs/Et/FH6DgWYwRxq
32
+ RtATRWON5R99ugPeRb7i1nIpnzGEBA9V32r6r959Bp3XjkVEXylbItYMqSARaZlY
33
+ qzKSsIUjh7vDyTNqta0DjSgCk26dhnOwc0hmzhvVZtBwfZritSVhfCLp5uFwqCqY
34
+ NK3TIZaPCh1S2/ES6wXNvjQ+5EnEEL9j/pSEop9DYEBPaM2WDVR5i0jJTAaRWw==
35
+ -----END CERTIFICATE-----
36
+
37
+ date: 2010-12-22 00:00:00 +01:00
38
+ default_executable:
39
+ dependencies:
40
+ - !ruby/object:Gem::Dependency
41
+ name: i18n
42
+ prerelease: false
43
+ requirement: &id001 !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ segments:
49
+ - 0
50
+ - 5
51
+ - 0
52
+ version: 0.5.0
53
+ type: :runtime
54
+ version_requirements: *id001
55
+ - !ruby/object:Gem::Dependency
56
+ name: test_declarative
57
+ prerelease: false
58
+ requirement: &id002 !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ segments:
64
+ - 0
65
+ - 0
66
+ - 4
67
+ version: 0.0.4
68
+ type: :development
69
+ version_requirements: *id002
70
+ - !ruby/object:Gem::Dependency
71
+ name: hoe
72
+ prerelease: false
73
+ requirement: &id003 !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ segments:
79
+ - 2
80
+ - 8
81
+ - 0
82
+ version: 2.8.0
83
+ type: :development
84
+ version_requirements: *id003
85
+ description: This backend module for I18n allows you to inflect translations by interpolating patterns.
86
+ email:
87
+ - pw@gnu.org
88
+ executables: []
89
+
90
+ extensions: []
91
+
92
+ extra_rdoc_files:
93
+ - Manifest.txt
94
+ - docs/README
95
+ - docs/LGPL-LICENSE
96
+ - docs/LEGAL
97
+ - docs/HISTORY
98
+ - docs/COPYING
99
+ files:
100
+ - ChangeLog
101
+ - Gemfile
102
+ - LGPL-LICENSE
103
+ - Manifest.txt
104
+ - README.textile
105
+ - Rakefile
106
+ - docs/COPYING
107
+ - docs/HISTORY
108
+ - docs/LEGAL
109
+ - docs/LGPL-LICENSE
110
+ - docs/README
111
+ - docs/rdoc.css
112
+ - lib/i18n-inflector.rb
113
+ - lib/i18n-inflector/inflector.rb
114
+ - lib/i18n-inflector/long_comments.rb
115
+ - lib/i18n-inflector/shortcuts.rb
116
+ - test/all.rb
117
+ - test/backend/inflection_test.rb
118
+ - test/run_all.rb
119
+ - test/test_helper.rb
120
+ has_rdoc: true
121
+ homepage: https://rubygems.org/gems/i18n-inflector/
122
+ licenses: []
123
+
124
+ post_install_message:
125
+ rdoc_options:
126
+ - --main
127
+ - docs/README
128
+ - --title
129
+ - Simple Inflector for I18n
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ none: false
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ segments:
138
+ - 0
139
+ version: "0"
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ none: false
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ segments:
146
+ - 0
147
+ version: "0"
148
+ requirements: []
149
+
150
+ rubyforge_project: i18n-inflector
151
+ rubygems_version: 1.3.7
152
+ signing_key:
153
+ specification_version: 3
154
+ summary: Simple Inflector backend module for I18n
155
+ test_files:
156
+ - test/test_helper.rb
metadata.gz.sig ADDED
@@ -0,0 +1,2 @@
1
+ !]�~��].c������>̅,�0^,@@Ɓ����d��:�Z��B&u���j�%`)2e� l�l ���81s�R��**�k<IJ�ܕ����(�cn�
2
+ ���4Y>:�`EN�r� He�ѓeY[۟��<��c��?�9: �x���,����1�>���ϙO �ꃥ3�8�o���t��x�$��vS���K���,���A\<��3���h�*nD�ա�>L�