i18n-inflector 1.0.11 → 2.0.0

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.
@@ -0,0 +1,201 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Author:: Paweł Wilk (mailto:pw@gnu.org)
4
+ # Copyright:: (c) 2011 by Paweł Wilk
5
+ # License:: This program is licensed under the terms of {file:LGPL GNU Lesser General Public License} or {file:COPYING Ruby License}.
6
+ #
7
+ # This file contains a class used to set up some options,
8
+ # for engine.
9
+
10
+ module I18n
11
+ module Inflector
12
+
13
+ # This class contains structures for keeping parsed translation data
14
+ # and basic operations.
15
+ #
16
+ # All global options are available for current backend's inflector by
17
+ # calling:
18
+ # I18n.backend.inflector.options.<option_name>
19
+ # or just:
20
+ # I18n.inflector.options.<option_name>
21
+ # A global option may be overriden by passing a proper option to
22
+ # the translation method. Such option should have the name of a
23
+ # global option but prefixed with +inflector_+:
24
+ # translate('welcome', :inflector_<option_name> => value)
25
+ class InflectionOptions
26
+
27
+ # This is a switch that enables extended error reporting. When it's enabled then
28
+ # errors are raised in case of unknown or empty tokens present in a pattern
29
+ # or in options. This switch is by default set to +false+.
30
+ #
31
+ # @note Local option +:raises+ passed to the {I18n::Backend::Inflector#translate}
32
+ # overrides this setting.
33
+ #
34
+ # @api public
35
+ # @return [Boolean] state of the switch
36
+ # @param [Boolean] state +true+ enables, +false+ disables this switch
37
+ attr_accessor :raises
38
+
39
+ # This is a switch that enables you to use aliases in patterns. When it's enabled then
40
+ # aliases may be used in inflection patterns, not only true tokens. This operation
41
+ # may make your translation data a bit messy if you're not alert.
42
+ # That's why this switch is by default set to +false+.
43
+ #
44
+ # @note Local option +:aliased_patterns+ passed to the {I18n::Backend::Inflector#translate}
45
+ # overrides this setting.
46
+ #
47
+ # @api public
48
+ # @return [Boolean] state of the switch
49
+ # @param [Boolean] state +true+ enables, +false+ disables this switch
50
+ attr_accessor :aliased_patterns
51
+
52
+ # When this switch is set to +true+ then inflector falls back to the default
53
+ # token for a kind if an inflection option passed to the {I18n::Backend::Inflector#translate} is unknown
54
+ # or +nil+. Note that the value of the default token will be
55
+ # interpolated only when this token is present in a pattern. This switch
56
+ # is by default set to +true+.
57
+ #
58
+ # @note Local option +:unknown_defaults+ passed to the {I18n::Backend::Inflector#translate}
59
+ # overrides this setting.
60
+ #
61
+ # @api public
62
+ # @return [Boolean] state of the switch
63
+ # @param [Boolean] state +true+ enables, +false+ disables this switch
64
+ #
65
+ # @example YAML:
66
+ # en:
67
+ # i18n:
68
+ # inflections:
69
+ # gender:
70
+ # n: 'neuter'
71
+ # o: 'other'
72
+ # default: n
73
+ #
74
+ # welcome: "Dear @{n:You|o:Other}"
75
+ # welcome_free: "Dear @{n:You|o:Other|Free}"
76
+ #
77
+ # @example Example 1
78
+ #
79
+ # # :gender option is not present,
80
+ # # unknown tokens in options are falling back to default
81
+ #
82
+ # I18n.t('welcome')
83
+ # # => "Dear You"
84
+ #
85
+ # # :gender option is not present,
86
+ # # unknown tokens from options are not falling back to default
87
+ #
88
+ # I18n.t('welcome', :unknown_defaults => false)
89
+ # # => "Dear You"
90
+ #
91
+ # # other way of setting an option – globally
92
+ #
93
+ # I18n.inflector.options.unknown_defaults = false
94
+ # I18n.t('welcome')
95
+ # # => "Dear You"
96
+ #
97
+ # # :gender option is not present, free text is present,
98
+ # # unknown tokens from options are not falling back to default
99
+ #
100
+ # I18n.t('welcome_free', :unknown_defaults => false)
101
+ # # => "Dear You"
102
+ #
103
+ # @example Example 2
104
+ #
105
+ # # :gender option is nil,
106
+ # # unknown tokens from options are falling back to default token for a kind
107
+ #
108
+ # I18n.t('welcome', :gender => nil)
109
+ # # => "Dear You"
110
+ #
111
+ # # :gender option is nil
112
+ # # unknown tokens from options are not falling back to default token for a kind
113
+ #
114
+ # I18n.t('welcome', :gender => nil, :unknown_defaults => false)
115
+ # # => "Dear "
116
+ #
117
+ # # :gender option is nil, free text is present
118
+ # # unknown tokens from options are not falling back to default token for a kind
119
+ #
120
+ # I18n.t('welcome_free', :gender => nil, :unknown_defaults => false)
121
+ # # => "Dear Free"
122
+ #
123
+ # @example Example 3
124
+ #
125
+ # # :gender option is unknown,
126
+ # # unknown tokens from options are falling back to default token for a kind
127
+ #
128
+ # I18n.t('welcome', :gender => :unknown_blabla)
129
+ # # => "Dear You"
130
+ #
131
+ # # :gender option is unknown,
132
+ # # unknown tokens from options are not falling back to default token for a kind
133
+ #
134
+ # I18n.t('welcome', :gender => :unknown_blabla, :unknown_defaults => false)
135
+ # # => "Dear "
136
+ #
137
+ # # :gender option is unknown, free text is present
138
+ # # unknown tokens from options are not falling back to default token for a kind
139
+ #
140
+ # I18n.t('welcome_free', :gender => :unknown_blabla, :unknown_defaults => false)
141
+ # # => "Dear Free"
142
+ attr_accessor :unknown_defaults
143
+
144
+ # When this switch is set to +true+ then inflector falls back to the default
145
+ # token for a kind if the given inflection option is correct but doesn't exist in a pattern.
146
+ #
147
+ # There might happend that the inflection option
148
+ # given to {#translate} method will contain some proper token, but that token
149
+ # will not be present in a processed pattern. Normally an empty string will
150
+ # be generated from such a pattern or a free text (if a local fallback is present
151
+ # in a pattern). You can change that behavior and tell interpolating routine to
152
+ # use the default token for a processed kind in such cases.
153
+ #
154
+ # This switch is by default set to +false+.
155
+ #
156
+ # @note Local option +:excluded_defaults+ passed to the {I18n::Backend::Inflector#translate}
157
+ # overrides this setting.
158
+ #
159
+ # @api public
160
+ # @return [Boolean] state of the switch
161
+ # @param [Boolean] state +true+ enables, +false+ disables this switch
162
+ #
163
+ # @example YAML:
164
+ # en:
165
+ # i18n:
166
+ # inflections:
167
+ # gender:
168
+ # o: "other"
169
+ # m: "male"
170
+ # n: "neuter"
171
+ # default: n
172
+ #
173
+ # welcome: 'Dear @{n:You|m:Sir}'
174
+ # @example Usage of +:excluded_defaults+ option
175
+ # I18n.t('welcome', :gender => :o)
176
+ # # => "Dear "
177
+ #
178
+ # I18n.t('welcome', :gender => :o, :excluded_defaults => true)
179
+ # # => "Dear You"
180
+ attr_accessor :excluded_defaults
181
+
182
+ # This method initializes all internal structures.
183
+ def initialize
184
+ reset
185
+ end
186
+
187
+ # This method resets all options to their default values.
188
+ #
189
+ # @return [void]
190
+ def reset
191
+ @excluded_defaults = false
192
+ @unknown_defaults = true
193
+ @aliased_patterns = false
194
+ @raises = false
195
+ nil
196
+ end
197
+
198
+ end
199
+
200
+ end
201
+ end
@@ -0,0 +1,67 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Author:: Paweł Wilk (mailto:pw@gnu.org)
4
+ # Copyright:: (c) 2011 by Paweł Wilk
5
+ # License:: This program is licensed under the terms of {file:LGPL GNU Lesser General Public License} or {file:COPYING Ruby License}.
6
+ #
7
+ # This file contains utility methods,
8
+ # that are used by I18n::Inflector.
9
+
10
+ module I18n
11
+ module Inflector
12
+
13
+ # This module contains some methods that are helpful.
14
+ module Util
15
+
16
+ protected
17
+
18
+ # Processes +locale+ name and validates
19
+ # if it's correct (not empty and not +nil+).
20
+ #
21
+ # @note If the +locale+ is not correct, it
22
+ # tries to use locale from {I18n.locale} and validates it
23
+ # as well.
24
+ # @param [Symbol,String] locale the locale identifier
25
+ # @raise [I18n::InvalidLocale] if there is no proper locale name
26
+ # @return [Symbol] the given locale or the global locale
27
+ def prep_locale(locale=nil)
28
+ locale ||= I18n.locale
29
+ raise I18n::InvalidLocale.new(locale) if locale.to_s.empty?
30
+ locale.to_sym
31
+ end
32
+
33
+ # This method is the internal helper that prepares arguments
34
+ # containing +token+, +kind+ and +locale+.
35
+ #
36
+ # @note This method leaves +kind+ as is when it's +nil+ or empty. It sets
37
+ # +token+ to +nil+ when it's empty.
38
+ # @raise [I18n::InvalidLocale] if there is no proper locale name
39
+ # @raise [ArgumentError] if the count of arguments is invalid
40
+ # @return [Array<Symbol,Symbol,Symbol>] the array containing
41
+ # cleaned and validated +token+, +kind+ and +locale+
42
+ # @overload tkl_args(token, kind, locale)
43
+ # Prepares arguments containing +token+, +kind+ and +locale+.
44
+ # @param [String,Hash] token the token
45
+ # @param [String,Hash] kind the inflection kind
46
+ # @param [String,Hash] locale the locale identifier
47
+ # @return [Array<Symbol,Symbol,Symbol>] the array containing
48
+ # cleaned and validated +token+, +kind+ and +locale+
49
+ # @overload tkl_args(token, locale)
50
+ # Prepares arguments containing +token+ and +locale+.
51
+ # @param [String,Hash] token the token
52
+ # @param [String,Hash] locale the locale identifier
53
+ # @return [Array<Symbol,Symbol,Symbol>] the array containing
54
+ # cleaned and validated +token+, +kind+ and +locale+
55
+ def tkl_args(args)
56
+ token, kind, locale = case args.count
57
+ when 1 then [args[0], nil, nil]
58
+ when 2 then [args[0], nil, args[1]]
59
+ when 3 then args
60
+ else raise ArgumentError.new("wrong number of arguments: #{args.count} for (1..3)")
61
+ end
62
+ [token,kind,locale]
63
+ end
64
+
65
+ end
66
+ end
67
+ end
@@ -2,7 +2,7 @@
2
2
  #
3
3
  # Author:: Paweł Wilk (mailto:pw@gnu.org)
4
4
  # Copyright:: (c) 2011 by Paweł Wilk
5
- # License:: This program is licensed under the terms of {file:LGPL-LICENSE GNU Lesser General Public License} or {file:COPYING Ruby License}.
5
+ # License:: This program is licensed under the terms of {file:LGPL GNU Lesser General Public License} or {file:COPYING Ruby License}.
6
6
  #
7
7
  # This file contains version information.
8
8
 
@@ -14,7 +14,7 @@ module I18n
14
14
  # @private
15
15
  EMAIL = 'pw@gnu.org'
16
16
  # @private
17
- VERSION = '1.0.11'
17
+ VERSION = '2.0.0'
18
18
  # @private
19
19
  NAME = 'i18n-inflector'
20
20
  # @private
@@ -1,10 +1,14 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require 'i18n'
4
+
4
5
  require 'i18n-inflector/version'
5
6
  require 'i18n-inflector/errors'
7
+ require 'i18n-inflector/util'
8
+ require 'i18n-inflector/inflection_data'
9
+ require 'i18n-inflector/options'
10
+ require 'i18n-inflector/backend'
6
11
  require 'i18n-inflector/inflector'
7
12
 
8
13
  I18n::Backend::Simple.send(:include, I18n::Backend::Inflector)
9
14
 
10
- require 'i18n-inflector/shortcuts'
@@ -1,6 +1,6 @@
1
1
  require 'test_helper'
2
2
 
3
- class I18nBackendInflectionTest < Test::Unit::TestCase
3
+ class I18nInflectorTest < Test::Unit::TestCase
4
4
  class Backend < I18n::Backend::Simple
5
5
  include I18n::Backend::Inflector
6
6
  include I18n::Backend::Fallbacks
@@ -27,16 +27,18 @@ class I18nBackendInflectionTest < Test::Unit::TestCase
27
27
  store_translations(:xx, 'welcome' => 'Dear @{f:Lady|m:Sir|n:You|All}!')
28
28
  end
29
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?
30
+ test "backend inflector has methods to test its switches" do
31
+ assert_equal true, I18n.inflector.options.unknown_defaults = true
32
+ assert_equal false, I18n.inflector.options.excluded_defaults = false
33
+ assert_equal false, I18n.inflector.options.aliased_patterns = false
34
+ assert_equal false, I18n.inflector.options.raises = false
35
+ assert_equal false, I18n.backend.inflector.options.raises
36
+ assert_equal true, I18n.backend.inflector.options.unknown_defaults
37
+ assert_equal false, I18n.backend.inflector.options.excluded_defaults
38
+ assert_equal false, I18n.backend.inflector.options.aliased_patterns
37
39
  end
38
40
 
39
- test "inflector store_translations: regenerates inflection structures when translations are loaded" do
41
+ test "backend inflector store_translations: regenerates inflection structures when translations are loaded" do
40
42
  store_translations(:xx, :i18n => { :inflections => { :gender => { :o => 'other' }}})
41
43
  store_translations(:xx, 'hi' => 'Dear @{f:Lady|o:Others|n:You|All}!')
42
44
  assert_equal 'Dear Others!', I18n.t('hi', :gender => :o, :locale => :xx)
@@ -45,13 +47,13 @@ class I18nBackendInflectionTest < Test::Unit::TestCase
45
47
  assert_equal 'Dear All!', I18n.t('hi', :gender => :m, :locale => :xx)
46
48
  end
47
49
 
48
- test "inflector store_translations: raises I18n::DuplicatedInflectionToken when duplicated token is given" do
50
+ test "backend inflector store_translations: raises I18n::DuplicatedInflectionToken when duplicated token is given" do
49
51
  assert_raise I18n::DuplicatedInflectionToken do
50
52
  store_translations(:xx, :i18n => { :inflections => { :gender => { :o => 'other' }, :person => { :o => 'o' }}})
51
53
  end
52
54
  end
53
55
 
54
- test "inflector store_translations: raises I18n::BadInflectionAlias when bad alias is given" do
56
+ test "backend inflector store_translations: raises I18n::BadInflectionAlias when bad alias is given" do
55
57
  assert_raise I18n::BadInflectionAlias do
56
58
  store_translations(:xx, :i18n => { :inflections => { :gender => { :o => '@nonexistant' }}})
57
59
  end
@@ -60,109 +62,104 @@ class I18nBackendInflectionTest < Test::Unit::TestCase
60
62
  end
61
63
  end
62
64
 
63
- test "inflector store_translations: raises I18n::BadInflectionToken when duplicated token is given" do
65
+ test "backend inflector store_translations: raises I18n::BadInflectionToken when duplicated token is given" do
64
66
  assert_raise I18n::BadInflectionToken do
65
67
  store_translations(:xx, :i18n => { :inflections => { :gender => { :o => '@' }}})
66
68
  store_translations(:xx, :i18n => { :inflections => { :gender => { :tok => nil }}})
67
69
  end
68
70
  end
69
71
 
70
- test "inflector translate: allows pattern-only translation data" do
72
+ test "backend inflector translate: allows pattern-only translation data" do
71
73
  store_translations(:xx, 'clear_welcome' => '@{f:Lady|m:Sir|n:You|All}')
72
74
  assert_equal 'Lady', I18n.t('clear_welcome', :gender => 'f', :locale => :xx)
73
75
  end
74
76
 
75
- test "inflector translate: allows patterns to be escaped using @@ or \\@" do
77
+ test "backend inflector translate: allows patterns to be escaped using @@ or \\@" do
76
78
  store_translations(:xx, 'escaped_welcome' => '@@{f:AAAAA|m:BBBBB}')
77
79
  assert_equal '@{f:AAAAA|m:BBBBB}', I18n.t('escaped_welcome', :gender => 'f', :locale => :xx)
78
80
  store_translations(:xx, 'escaped_welcome' => '\@{f:AAAAA|m:BBBBB}')
79
81
  assert_equal '@{f:AAAAA|m:BBBBB}', I18n.t('escaped_welcome', :gender => 'f', :locale => :xx)
80
82
  end
81
83
 
82
- test "inflector translate: picks Lady for :f gender option" do
84
+ test "backend inflector translate: picks Lady for :f gender option" do
83
85
  assert_equal 'Dear Lady!', I18n.t('welcome', :gender => :f, :locale => :xx)
84
86
  end
85
87
 
86
- test "inflector translate: picks Lady for f gender option" do
88
+ test "backend inflector translate: picks Lady for f gender option" do
87
89
  assert_equal 'Dear Lady!', I18n.t('welcome', :gender => 'f', :locale => :xx)
88
90
  end
89
91
 
90
- test "inflector translate: picks Sir for :m gender option" do
92
+ test "backend inflector translate: picks Sir for :m gender option" do
91
93
  assert_equal 'Dear Sir!', I18n.t('welcome', :gender => :m, :locale => :xx)
92
94
  end
93
95
 
94
- test "inflector translate: picks Sir for :masculine gender option" do
96
+ test "backend inflector translate: picks Sir for :masculine gender option" do
95
97
  assert_equal 'Dear Sir!', I18n.t('welcome', :gender => :masculine, :locale => :xx)
96
98
  end
97
99
 
98
- test "inflector translate: picks Sir for masculine gender option" do
100
+ test "backend inflector translate: picks Sir for masculine gender option" do
99
101
  assert_equal 'Dear Sir!', I18n.t('welcome', :gender => 'masculine', :locale => :xx)
100
102
  end
101
103
 
102
- test "inflector translate: picks an empty string when no default token is present and no free text is there" do
104
+ test "backend inflector translate: picks an empty string when no default token is present and no free text is there" do
103
105
  store_translations(:xx, 'none_welcome' => '@{n:You|f:Lady}')
104
106
  assert_equal '', I18n.t('none_welcome', :gender => 'masculine', :locale => :xx)
105
107
  end
106
108
 
107
- test "inflector translate: allows multiple patterns in the same data" do
109
+ test "backend inflector translate: allows multiple patterns in the same data" do
108
110
  store_translations(:xx, 'multiple_welcome' => '@@{f:AAAAA|m:BBBBB} @{f:Lady|m:Sir|n:You|All} @{f:Lady|All}@{m:Sir|All}@{n:You|All}')
109
111
  assert_equal '@{f:AAAAA|m:BBBBB} Sir AllSirAll', I18n.t('multiple_welcome', :gender => 'masculine', :locale => :xx)
110
112
  end
111
113
 
112
- test "inflector translate: falls back to default for the unknown gender option" do
114
+ test "backend inflector translate: falls back to default for the unknown gender option" do
113
115
  assert_equal 'Dear You!', I18n.t('welcome', :gender => :unknown, :locale => :xx)
114
116
  end
115
117
 
116
- test "inflector translate: falls back to default for a gender option set to nil" do
118
+ test "backend inflector translate: falls back to default for a gender option set to nil" do
117
119
  assert_equal 'Dear You!', I18n.t('welcome', :gender => nil, :locale => :xx)
118
120
  end
119
121
 
120
- test "inflector translate: falls back to default for no gender option" do
122
+ test "backend inflector translate: falls back to default for no gender option" do
121
123
  assert_equal 'Dear You!', I18n.t('welcome', :locale => :xx)
122
124
  end
123
125
 
124
- test "inflector translate: falls back to free text for the proper gender option but not present in pattern" do
126
+ test "backend inflector translate: falls back to free text for the proper gender option but not present in pattern" do
125
127
  assert_equal 'Dear All!', I18n.t('welcome', :gender => :s, :locale => :xx)
126
128
  end
127
129
 
128
- test "inflector translate: falls back to free text when :inflector_unknown_defaults is false" do
130
+ test "backend inflector translate: falls back to free text when :inflector_unknown_defaults is false" do
129
131
  assert_equal 'Dear All!', I18n.t('welcome', :gender => :unknown, :locale => :xx, :inflector_unknown_defaults => false)
130
132
  assert_equal 'Dear All!', I18n.t('welcome', :gender => :s, :locale => :xx, :inflector_unknown_defaults => false)
131
133
  assert_equal 'Dear All!', I18n.t('welcome', :gender => nil, :locale => :xx, :inflector_unknown_defaults => false)
132
134
  end
133
135
 
134
- test "inflector translate: falls back to default for no gender option when :inflector_unknown_defaults is false" do
136
+ test "backend inflector translate: falls back to default for no gender option when :inflector_unknown_defaults is false" do
135
137
  assert_equal 'Dear You!', I18n.t('welcome', :locale => :xx, :inflector_unknown_defaults => false)
136
138
  end
137
139
 
138
- test "inflector translate: falls back to free text for the unknown gender option when global inflector_unknown_defaults is false" do
139
- I18n.backend.inflector_unknown_defaults = false
140
+ test "backend inflector translate: falls back to free text for the unknown gender option when global inflector_unknown_defaults is false" do
141
+ I18n.inflector.options.unknown_defaults = false
140
142
  assert_equal 'Dear All!', I18n.t('welcome', :gender => :unknown, :locale => :xx)
141
143
  end
142
144
 
143
- test "inflector translate: falls back to default for the unknown gender option when global inflector_unknown_defaults is overriden" do
144
- I18n.backend.inflector_unknown_defaults = false
145
+ test "backend inflector translate: falls back to default for the unknown gender option when global inflector_unknown_defaults is overriden" do
146
+ I18n.inflector.options.unknown_defaults = false
145
147
  assert_equal 'Dear You!', I18n.t('welcome', :gender => :unknown, :locale => :xx, :inflector_unknown_defaults => true)
146
148
  end
147
-
148
- test "inflector translate: falls back to default token for ommited gender option when :inflector_excluded_defaults is true" do
149
+
150
+ test "backend inflector translate: falls back to default token for ommited gender option when :inflector_excluded_defaults is true" do
149
151
  assert_equal 'Dear You!', I18n.t('welcome', :gender => :s, :locale => :xx, :inflector_excluded_defaults => true)
150
- I18n.backend.inflector_excluded_defaults = true
152
+ I18n.inflector.options.excluded_defaults = true
151
153
  assert_equal 'Dear You!', I18n.t('welcome', :gender => :s, :locale => :xx)
152
154
  end
153
155
 
154
- test "inflector translate: falls back to free text for ommited gender option when :inflector_excluded_defaults is false" do
156
+ test "backend inflector translate: falls back to free text for ommited gender option when :inflector_excluded_defaults is false" do
155
157
  assert_equal 'Dear All!', I18n.t('welcome', :gender => :s, :locale => :xx, :inflector_excluded_defaults => false)
156
- I18n.backend.inflector_excluded_defaults = false
158
+ I18n.inflector.options.excluded_defaults = false
157
159
  assert_equal 'Dear All!', I18n.t('welcome', :gender => :s, :locale => :xx)
158
160
  end
159
161
 
160
- test "inflector translate: works with %{} patterns" do
161
- store_translations(:xx, 'hi' => 'Dear @{f:Lady|m:%{test}}!')
162
- assert_equal 'Dear Dude!', I18n.t('hi', :gender => :m, :locale => :xx, :test => "Dude")
163
- end
164
-
165
- test "inflector translate: raises I18n::InvalidOptionForKind when bad kind is given and inflector_raises is true" do
162
+ test "backend inflector translate: raises I18n::InvalidOptionForKind when bad kind is given and inflector_raises is true" do
166
163
  assert_nothing_raised I18n::InvalidOptionForKind do
167
164
  I18n.t('welcome', :locale => :xx, :inflector_raises => true)
168
165
  end
@@ -173,128 +170,212 @@ class I18nBackendInflectionTest < Test::Unit::TestCase
173
170
  assert_raise(I18n::InvalidOptionForKind) { I18n.t('welcome', :locale => :xx, :gender => "", :inflector_raises => true) }
174
171
  assert_raise(I18n::InvalidOptionForKind) { I18n.t('welcome', :locale => :xx, :gender => nil, :inflector_raises => true) }
175
172
  assert_raise I18n::InvalidOptionForKind do
176
- I18n.backend.inflector_raises = true
173
+ I18n.inflector.options.raises = true
177
174
  I18n.t('welcome', :locale => :xx)
178
175
  end
179
176
  end
180
177
 
181
- test "inflector translate: raises I18n::InvalidInflectionToken when bad token is given and inflector_raises is true" do
178
+ test "backend inflector translate: raises I18n::InvalidInflectionToken when bad token is given and inflector_raises is true" do
182
179
  store_translations(:xx, 'hi' => 'Dear @{f:Lady|o:BAD_TOKEN|n:You|First}!')
183
180
  assert_raise(I18n::InvalidInflectionToken) { I18n.t('hi', :locale => :xx, :inflector_raises => true) }
184
181
  assert_raise I18n::InvalidInflectionToken do
185
- I18n.backend.inflector_raises = true
182
+ I18n.inflector.options.raises = true
186
183
  I18n.t('hi', :locale => :xx)
187
184
  end
188
185
  end
189
186
 
190
- test "inflector translate: raises I18n::MisplacedInflectionToken when bad token is given and inflector_raises is true" do
187
+ test "backend inflector translate: raises I18n::MisplacedInflectionToken when bad token is given and inflector_raises is true" do
191
188
  store_translations(:xx, 'hi' => 'Dear @{f:Lady|i:Me|n:You|First}!')
192
189
  assert_raise(I18n::MisplacedInflectionToken) { I18n.t('hi', :locale => :xx, :inflector_raises => true) }
193
190
  assert_raise I18n::MisplacedInflectionToken do
194
- I18n.backend.inflector_raises = true
191
+ I18n.inflector.options.raises = true
195
192
  I18n.t('hi', :locale => :xx)
196
193
  end
197
194
  end
198
195
 
196
+ test "backend inflector translate: works with %{} patterns" do
197
+ store_translations(:xx, 'hi' => 'Dear @{f:Lady|m:%{test}}!')
198
+ assert_equal 'Dear Dude!', I18n.t('hi', :gender => :m, :locale => :xx, :test => "Dude")
199
+ end
200
+
201
+ test "backend inflector translate: works with tokens separated by commas" do
202
+ store_translations(:xx, 'hi' => 'Dear @{f,m:Someone|n:You|All}!')
203
+ assert_equal 'Dear Someone!', I18n.t('hi', :gender => :m, :locale => :xx)
204
+ end
205
+
206
+ test "backend inflector translate: works with negative tokens" do
207
+ store_translations(:xx, 'hi' => 'Dear @{!m:Lady|m:Sir|n:You|All}!')
208
+ assert_equal 'Dear Lady!', I18n.t('hi', :gender => :n, :locale => :xx)
209
+ assert_equal 'Dear Sir!', I18n.t('hi', :gender => :m, :locale => :xx)
210
+ assert_equal 'Dear Lady!', I18n.t('hi', :locale => :xx)
211
+ assert_equal 'Dear Lady!', I18n.t('hi', :gender => :unknown, :locale => :xx)
212
+ store_translations(:xx, 'hi' => 'Hello @{!m:Ladies|n:You}')
213
+ assert_equal 'Hello Ladies', I18n.t('hi', :gender => :n, :locale => :xx)
214
+ assert_equal 'Hello Ladies', I18n.t('hi', :gender => :f, :locale => :xx)
215
+ assert_equal 'Hello ', I18n.t('hi', :gender => :m, :locale => :xx)
216
+ assert_equal 'Hello Ladies', I18n.t('hi', :locale => :xx)
217
+ store_translations(:xx, 'hi' => 'Hello @{!n:Ladies|m,f:You}')
218
+ assert_equal 'Hello ', I18n.t('hi', :locale => :xx, :inflector_raises => true)
219
+ end
220
+
221
+ test "backend inflector translate: works with tokens separated by commas and negative tokens" do
222
+ store_translations(:xx, 'hi' => 'Dear @{!f,!m:Someone|m:Sir}!')
223
+ assert_equal 'Dear Someone!', I18n.t('hi', :gender => :m, :locale => :xx)
224
+ assert_equal 'Dear Someone!', I18n.t('hi', :gender => :n, :locale => :xx)
225
+ store_translations(:xx, 'hi' => 'Dear @{!f,!m,n:Someone|m:Sir}!')
226
+ assert_equal 'Dear Someone!', I18n.t('hi', :gender => :m, :locale => :xx)
227
+ assert_equal 'Dear Someone!', I18n.t('hi', :gender => :n, :locale => :xx)
228
+ store_translations(:xx, 'hi' => 'Dear @{!f,n:Someone|m:Sir|f:Lady}!')
229
+ assert_equal 'Dear Someone!', I18n.t('hi', :gender => :m, :locale => :xx)
230
+ assert_equal 'Dear Lady!', I18n.t('hi', :gender => :f, :locale => :xx)
231
+ assert_equal 'Dear Someone!', I18n.t('hi', :locale => :xx)
232
+ end
233
+
234
+ test "backend inflector translate: works with aliased patterns" do
235
+ store_translations(:xx, 'hi' => 'Dear @{masculine:Sir|feminine:Lady|n:You|All}!')
236
+ assert_equal 'Dear Sir!', I18n.t('hi', :gender => :m, :locale => :xx, :inflector_aliased_patterns => true)
237
+ assert_equal 'Dear Sir!', I18n.t('hi', :gender => :masculine, :locale => :xx, :inflector_aliased_patterns => true)
238
+ assert_equal 'Dear Lady!', I18n.t('hi', :gender => :f, :locale => :xx, :inflector_aliased_patterns => true)
239
+ assert_equal 'Dear All!', I18n.t('hi', :gender => :s, :locale => :xx, :inflector_aliased_patterns => true)
240
+ assert_equal 'Dear You!', I18n.t('hi', :locale => :xx, :inflector_aliased_patterns => true)
241
+ I18n.inflector.options.aliased_patterns = true
242
+ assert_equal 'Dear Sir!', I18n.t('hi', :gender => :masculine, :locale => :xx)
243
+ end
244
+
199
245
  test "inflector inflected_locales: lists languages that support inflection" do
200
- assert_equal [:xx], I18n.backend.inflected_locales
201
- assert_equal [:xx], I18n.backend.inflected_locales(:gender)
246
+ assert_equal [:xx], I18n.inflector.inflected_locales
247
+ assert_equal [:xx], I18n.inflector.inflected_locales(:gender)
202
248
  end
203
249
 
204
- test "inflector inflected_locale?: checks if a language supports inflection" do
205
- assert_equal true, I18n.backend.inflected_locale?(:xx)
206
- assert_equal false, I18n.backend.inflected_locale?(:pl)
250
+ test "inflector locale_supported?: checks if a language supports inflection" do
251
+ assert_equal true, I18n.inflector.locale_supported?(:xx)
252
+ assert_equal false, I18n.inflector.locale_supported?(:pl)
253
+ assert_equal false, I18n.inflector.locale_supported?(nil)
254
+ assert_equal false, I18n.inflector.locale_supported?("")
207
255
  I18n.locale = :xx
208
- assert_equal true, I18n.backend.inflected_locale?
256
+ assert_equal true, I18n.inflector.locale_supported?
209
257
  I18n.locale = :pl
210
- assert_equal false, I18n.backend.inflected_locale?
258
+ assert_equal false, I18n.inflector.locale_supported?
259
+ I18n.locale = nil
260
+ assert_equal false, I18n.inflector.locale_supported?
261
+ I18n.locale = ""
262
+ assert_equal false, I18n.inflector.locale_supported?
263
+ end
264
+
265
+ test "inflector has_token?: checks if a token exists" do
266
+ assert_equal true, I18n.inflector.has_token?(:neuter, :gender, :xx)
267
+ assert_equal true, I18n.inflector.has_token?(:neuter, :xx)
268
+ assert_equal true, I18n.inflector.has_token?(:f, :xx)
269
+ assert_equal true, I18n.inflector.has_token?(:you, :xx)
270
+ I18n.locale = :xx
271
+ assert_equal true, I18n.inflector.has_token?(:f)
272
+ assert_equal true, I18n.inflector.has_token?(:you)
273
+ assert_equal false,I18n.inflector.has_token?(:faafaffafafa)
211
274
  end
212
275
 
213
- test "inflector inflection_kind: checks what is the inflection kind of the given token" do
214
- assert_equal :gender, I18n.backend.inflection_kind(:neuter, :xx)
215
- assert_equal :gender, I18n.backend.inflection_kind(:f, :xx)
216
- assert_equal :person, I18n.backend.inflection_kind(:you, :xx)
276
+ test "inflector kind: checks what is the inflection kind of the given token" do
277
+ assert_equal :gender, I18n.inflector.kind(:neuter, :xx)
278
+ assert_equal :gender, I18n.inflector.kind(:f, :xx)
279
+ assert_equal :person, I18n.inflector.kind(:you, :xx)
217
280
  I18n.locale = :xx
218
- assert_equal :gender, I18n.backend.inflection_kind(:neuter)
219
- assert_equal :gender, I18n.backend.inflection_kind(:f)
220
- assert_equal :person, I18n.backend.inflection_kind(:you)
221
- assert_equal nil, I18n.backend.inflection_kind(:faafaffafafa)
281
+ assert_equal :gender, I18n.inflector.kind(:neuter)
282
+ assert_equal :gender, I18n.inflector.kind(:f)
283
+ assert_equal :person, I18n.inflector.kind(:you)
284
+ assert_equal nil, I18n.inflector.kind(:faafaffafafa)
222
285
  end
223
286
 
224
- test "inflector inflection_true_token: gets true token for a given token name" do
225
- assert_equal :n, I18n.backend.inflection_true_token(:neuter, :xx)
226
- assert_equal :f, I18n.backend.inflection_true_token(:f, :xx)
287
+ test "inflector true_token: gets true token for a given token name" do
288
+ assert_equal :n, I18n.inflector.true_token(:neuter, :xx)
289
+ assert_equal :f, I18n.inflector.true_token(:f, :xx)
227
290
  I18n.locale = :xx
228
- assert_equal :n, I18n.backend.inflection_true_token(:neuter)
229
- assert_equal :f, I18n.backend.inflection_true_token(:f)
230
- assert_equal nil, I18n.backend.inflection_true_token(:faafaffafafa)
291
+ assert_equal :n, I18n.inflector.true_token(:neuter)
292
+ assert_equal :f, I18n.inflector.true_token(:f)
293
+ assert_equal :f, I18n.inflector.true_token(:f, :xx)
294
+ assert_equal nil, I18n.inflector.true_token(:f, :person, :xx)
295
+ assert_equal nil, I18n.inflector.true_token(:f, :nokind, :xx)
296
+ assert_equal nil, I18n.inflector.true_token(:faafaffafafa)
231
297
  end
232
298
 
233
- test "inflector available_inflection_kinds: lists inflection kinds" do
234
- assert_not_nil I18n.backend.available_inflection_kinds(:xx)
235
- assert_equal [:gender,:person], I18n.backend.available_inflection_kinds(:xx).sort{|k,v| k.to_s<=>v.to_s}
299
+ test "inflector has_true_token?: tests if true token exists for a given token name" do
300
+ assert_equal false, I18n.inflector.has_true_token?(:neuter, :xx)
301
+ assert_equal true, I18n.inflector.has_true_token?(:f, :xx)
236
302
  I18n.locale = :xx
237
- assert_equal [:gender,:person], I18n.backend.available_inflection_kinds.sort{|k,v| k.to_s<=>v.to_s}
303
+ assert_equal false, I18n.inflector.has_true_token?(:neuter)
304
+ assert_equal true, I18n.inflector.has_true_token?(:f)
305
+ assert_equal true, I18n.inflector.has_true_token?(:f, :xx)
306
+ assert_equal false, I18n.inflector.has_true_token?(:f, :person, :xx)
307
+ assert_equal false, I18n.inflector.has_true_token?(:f, :nokind, :xx)
308
+ assert_equal false, I18n.inflector.has_true_token?(:faafaffafafa)
238
309
  end
239
310
 
240
- test "inflector inflection_tokens: lists inflection tokens" do
311
+ test "inflector kinds: lists inflection kinds" do
312
+ assert_not_nil I18n.inflector.kinds(:xx)
313
+ assert_equal [:gender,:person], I18n.inflector.kinds(:xx).sort{|k,v| k.to_s<=>v.to_s}
314
+ I18n.locale = :xx
315
+ assert_equal [:gender,:person], I18n.inflector.kinds.sort{|k,v| k.to_s<=>v.to_s}
316
+ end
317
+
318
+ test "inflector tokens: lists all inflection tokens including aliases" do
241
319
  h = {:m=>"male",:f=>"female",:n=>"neuter",:s=>"strange",
242
320
  :masculine=>"male",:feminine=>"female",:neuter=>"neuter",
243
321
  :neutral=>"neuter"}
244
322
  ha = h.merge(:i=>'I', :you=>'You')
245
- assert_equal h, I18n.backend.inflection_tokens(:gender, :xx)
323
+ assert_equal h, I18n.inflector.tokens(:gender, :xx)
246
324
  I18n.locale = :xx
247
- assert_equal h, I18n.backend.inflection_tokens(:gender)
248
- assert_equal ha, I18n.backend.inflection_tokens
325
+ assert_equal h, I18n.inflector.tokens(:gender)
326
+ assert_equal ha, I18n.inflector.tokens
249
327
  end
250
328
 
251
- test "inflector inflection_true_tokens: lists true tokens" do
329
+ test "inflector true_tokens: lists true tokens" do
252
330
  h = {:m=>"male",:f=>"female",:n=>"neuter",:s=>"strange"}
253
331
  ha = h.merge(:i=>"I",:you=>"You")
254
- assert_equal h, I18n.backend.inflection_true_tokens(:gender, :xx)
332
+ assert_equal h, I18n.inflector.true_tokens(:gender, :xx)
255
333
  I18n.locale = :xx
256
- assert_equal h, I18n.backend.inflection_true_tokens(:gender)
257
- assert_equal ha, I18n.backend.inflection_true_tokens
334
+ assert_equal h, I18n.inflector.true_tokens(:gender)
335
+ assert_equal ha, I18n.inflector.true_tokens
258
336
  end
259
337
 
260
- test "inflector inflection_raw_tokens: lists tokens in a so called raw format" do
338
+ test "inflector raw_tokens: lists tokens in a so called raw format" do
261
339
  h = {:m=>"male",:f=>"female",:n=>"neuter",:s=>"strange",
262
340
  :masculine=>:m,:feminine=>:f,:neuter=>:n,
263
341
  :neutral=>:n}
264
342
  ha = h.merge(:i=>'I',:you=>"You")
265
- assert_equal h, I18n.backend.inflection_raw_tokens(:gender, :xx)
343
+ assert_equal h, I18n.inflector.raw_tokens(:gender, :xx)
266
344
  I18n.locale = :xx
267
- assert_equal h, I18n.backend.inflection_raw_tokens(:gender)
268
- assert_equal ha, I18n.backend.inflection_raw_tokens
345
+ assert_equal h, I18n.inflector.raw_tokens(:gender)
346
+ assert_equal ha, I18n.inflector.raw_tokens
269
347
  end
270
348
 
271
- test "inflector inflection_default_token: returns a default token for a kind" do
272
- assert_equal :n, I18n.backend.inflection_default_token(:gender, :xx)
349
+ test "inflector default_token: returns a default token for a kind" do
350
+ assert_equal :n, I18n.inflector.default_token(:gender, :xx)
273
351
  I18n.locale = :xx
274
- assert_equal :n, I18n.backend.inflection_default_token(:gender)
352
+ assert_equal :n, I18n.inflector.default_token(:gender)
275
353
  end
276
354
 
277
- test "inflector inflection_aliases: lists aliases" do
355
+ test "inflector aliases: lists aliases" do
278
356
  a = {:masculine=>:m, :feminine=>:f, :neuter=>:n, :neutral=>:n}
279
- assert_equal a, I18n.backend.inflection_aliases(:gender, :xx)
357
+ assert_equal a, I18n.inflector.aliases(:gender, :xx)
280
358
  I18n.locale = :xx
281
- assert_equal a, I18n.backend.inflection_aliases(:gender)
282
- assert_equal a, I18n.backend.inflection_aliases
359
+ assert_equal a, I18n.inflector.aliases(:gender)
360
+ assert_equal a, I18n.inflector.aliases
283
361
  end
284
362
 
285
- test "inflector inflection_token_description: returns token's description" do
286
- assert_equal "male", I18n.backend.inflection_token_description(:m, :xx)
363
+ test "inflector token_description: returns token's description" do
364
+ assert_equal "male", I18n.inflector.token_description(:m, :xx)
287
365
  I18n.locale = :xx
288
- assert_equal "male", I18n.backend.inflection_token_description(:m)
289
- assert_equal nil, I18n.backend.inflection_token_description(:nonexistent, :xx)
290
- assert_equal "neuter", I18n.backend.inflection_token_description(:neutral, :xx)
366
+ assert_equal "male", I18n.inflector.token_description(:m)
367
+ assert_equal nil, I18n.inflector.token_description(:nonexistent, :xx)
368
+ assert_equal "neuter", I18n.inflector.token_description(:neutral, :xx)
291
369
  end
292
370
 
293
- test "inflector inflection_is_alias?: tests whether a token is an alias" do
294
- assert_equal true, I18n.backend.inflection_is_alias?(:neutral, :xx)
295
- assert_equal false, I18n.backend.inflection_is_alias?(:you, :xx)
371
+ test "inflector has_alias?: tests whether a token is an alias" do
372
+ assert_equal true, I18n.inflector.has_alias?(:neutral, :xx)
373
+ assert_equal false, I18n.inflector.has_alias?(:you, :xx)
374
+ assert_equal true, I18n.inflector.has_alias?(:neutral, :gender, :xx)
375
+ assert_equal false, I18n.inflector.has_alias?(:you, :gender, :xx)
376
+ assert_equal false, I18n.inflector.has_alias?(:neutral, :nokind, :xx)
296
377
  I18n.locale = :xx
297
- assert_equal true, I18n.backend.inflection_is_alias?(:neutral)
378
+ assert_equal true, I18n.inflector.has_alias?(:neutral)
298
379
  end
299
-
380
+
300
381
  end