i18n 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +4 -0
  3. data/lib/i18n/backend.rb +1 -0
  4. data/lib/i18n/backend/base.rb +12 -1
  5. data/lib/i18n/backend/cache_file.rb +36 -0
  6. data/lib/i18n/backend/chain.rb +22 -0
  7. data/lib/i18n/backend/key_value.rb +24 -0
  8. data/lib/i18n/backend/simple.rb +1 -1
  9. data/lib/i18n/config.rb +16 -0
  10. data/lib/i18n/core_ext/hash.rb +8 -0
  11. data/lib/i18n/interpolate/ruby.rb +5 -3
  12. data/lib/i18n/version.rb +1 -1
  13. metadata +16 -52
  14. data/gemfiles/Gemfile.rails-3.2.x +0 -10
  15. data/gemfiles/Gemfile.rails-4.0.x +0 -10
  16. data/gemfiles/Gemfile.rails-4.1.x +0 -10
  17. data/gemfiles/Gemfile.rails-4.2.x +0 -10
  18. data/gemfiles/Gemfile.rails-5.0.x +0 -10
  19. data/gemfiles/Gemfile.rails-5.1.x +0 -10
  20. data/gemfiles/Gemfile.rails-master +0 -10
  21. data/test/api/all_features_test.rb +0 -58
  22. data/test/api/cascade_test.rb +0 -28
  23. data/test/api/chain_test.rb +0 -24
  24. data/test/api/fallbacks_test.rb +0 -30
  25. data/test/api/key_value_test.rb +0 -24
  26. data/test/api/memoize_test.rb +0 -56
  27. data/test/api/override_test.rb +0 -42
  28. data/test/api/pluralization_test.rb +0 -30
  29. data/test/api/simple_test.rb +0 -28
  30. data/test/backend/cache_test.rb +0 -109
  31. data/test/backend/cascade_test.rb +0 -86
  32. data/test/backend/chain_test.rb +0 -122
  33. data/test/backend/exceptions_test.rb +0 -36
  34. data/test/backend/fallbacks_test.rb +0 -211
  35. data/test/backend/interpolation_compiler_test.rb +0 -118
  36. data/test/backend/key_value_test.rb +0 -85
  37. data/test/backend/memoize_test.rb +0 -79
  38. data/test/backend/metadata_test.rb +0 -48
  39. data/test/backend/pluralization_test.rb +0 -45
  40. data/test/backend/simple_test.rb +0 -111
  41. data/test/backend/transliterator_test.rb +0 -84
  42. data/test/core_ext/hash_test.rb +0 -36
  43. data/test/gettext/api_test.rb +0 -214
  44. data/test/gettext/backend_test.rb +0 -92
  45. data/test/i18n/exceptions_test.rb +0 -117
  46. data/test/i18n/gettext_plural_keys_test.rb +0 -20
  47. data/test/i18n/interpolate_test.rb +0 -91
  48. data/test/i18n/load_path_test.rb +0 -34
  49. data/test/i18n/middleware_test.rb +0 -24
  50. data/test/i18n_test.rb +0 -462
  51. data/test/locale/fallbacks_test.rb +0 -133
  52. data/test/locale/tag/rfc4646_test.rb +0 -143
  53. data/test/locale/tag/simple_test.rb +0 -32
  54. data/test/run_all.rb +0 -20
  55. data/test/test_data/locales/de.po +0 -82
  56. data/test/test_data/locales/en.rb +0 -3
  57. data/test/test_data/locales/en.yaml +0 -3
  58. data/test/test_data/locales/en.yml +0 -3
  59. data/test/test_data/locales/invalid/empty.yml +0 -0
  60. data/test/test_data/locales/invalid/syntax.yml +0 -4
  61. data/test/test_data/locales/plurals.rb +0 -113
  62. data/test/test_helper.rb +0 -61
@@ -1,122 +0,0 @@
1
- require 'test_helper'
2
-
3
- class I18nBackendChainTest < I18n::TestCase
4
- def setup
5
- super
6
- @first = backend(:en => {
7
- :foo => 'Foo', :formats => {
8
- :short => 'short',
9
- :subformats => {:short => 'short'},
10
- },
11
- :plural_1 => { :one => '%{count}' },
12
- :dates => {:a => "A"}
13
- })
14
- @second = backend(:en => {
15
- :bar => 'Bar', :formats => {
16
- :long => 'long',
17
- :subformats => {:long => 'long'},
18
- },
19
- :plural_2 => { :one => 'one' },
20
- :dates => {:a => "B", :b => "B"}
21
- })
22
- @chain = I18n.backend = I18n::Backend::Chain.new(@first, @second)
23
- end
24
-
25
- test "looks up translations from the first chained backend" do
26
- assert_equal 'Foo', @first.send(:translations)[:en][:foo]
27
- assert_equal 'Foo', I18n.t(:foo)
28
- end
29
-
30
- test "looks up translations from the second chained backend" do
31
- assert_equal 'Bar', @second.send(:translations)[:en][:bar]
32
- assert_equal 'Bar', I18n.t(:bar)
33
- end
34
-
35
- test "defaults only apply to lookups on the last backend in the chain" do
36
- assert_equal 'Foo', I18n.t(:foo, :default => 'Bah')
37
- assert_equal 'Bar', I18n.t(:bar, :default => 'Bah')
38
- assert_equal 'Bah', I18n.t(:bah, :default => 'Bah') # default kicks in only here
39
- end
40
-
41
- test "default" do
42
- assert_equal 'Fuh', I18n.t(:default => 'Fuh')
43
- assert_equal 'Zero', I18n.t(:default => { :zero => 'Zero' }, :count => 0)
44
- assert_equal({ :zero => 'Zero' }, I18n.t(:default => { :zero => 'Zero' }))
45
- assert_equal 'Foo', I18n.t(:default => :foo)
46
- end
47
-
48
- test 'default is returned if translation is missing' do
49
- assert_equal({}, I18n.t(:'i18n.transliterate.rule', :locale => 'en', :default => {}))
50
- end
51
-
52
- test "namespace lookup collects results from all backends and merges deep hashes" do
53
- assert_equal({:long=>"long", :subformats=>{:long=>"long", :short=>"short"}, :short=>"short"}, I18n.t(:formats))
54
- end
55
-
56
- test "namespace lookup collects results from all backends and lets leftmost backend take priority" do
57
- assert_equal({ :a => "A", :b => "B" }, I18n.t(:dates))
58
- end
59
-
60
- test "namespace lookup with only the first backend returning a result" do
61
- assert_equal({ :one => '%{count}' }, I18n.t(:plural_1))
62
- end
63
-
64
- test "pluralization still works" do
65
- assert_equal '1', I18n.t(:plural_1, :count => 1)
66
- assert_equal 'one', I18n.t(:plural_2, :count => 1)
67
- end
68
-
69
- test "bulk lookup collects results from all backends" do
70
- assert_equal ['Foo', 'Bar'], I18n.t([:foo, :bar])
71
- assert_equal ['Foo', 'Bar', 'Bah'], I18n.t([:foo, :bar, :bah], :default => 'Bah')
72
- assert_equal [{
73
- :long=>"long",
74
- :subformats=>{:long=>"long", :short=>"short"},
75
- :short=>"short"}, {:one=>"one"},
76
- "Bah"], I18n.t([:formats, :plural_2, :bah], :default => 'Bah')
77
- end
78
-
79
- test "store_translations options are not dropped while transfering to backend" do
80
- @first.expects(:store_translations).with(:foo, {:bar => :baz}, {:option => 'persists'})
81
- I18n.backend.store_translations :foo, {:bar => :baz}, {:option => 'persists'}
82
- end
83
-
84
- protected
85
-
86
- def backend(translations)
87
- backend = I18n::Backend::Simple.new
88
- translations.each { |locale, data| backend.store_translations(locale, data) }
89
- backend
90
- end
91
- end
92
-
93
- class I18nBackendChainWithKeyValueTest < I18n::TestCase
94
- def setup_backend!(subtrees = true)
95
- first = I18n::Backend::KeyValue.new({}, subtrees)
96
- first.store_translations(:en, :plural_1 => { :one => '%{count}' })
97
-
98
- second = I18n::Backend::Simple.new
99
- second.store_translations(:en, :plural_2 => { :one => 'one' })
100
- I18n.backend = I18n::Backend::Chain.new(first, second)
101
- end
102
-
103
- test "subtrees enabled: looks up pluralization translations from the first chained backend" do
104
- setup_backend!
105
- assert_equal '1', I18n.t(:plural_1, count: 1)
106
- end
107
-
108
- test "subtrees disabled: looks up pluralization translations from the first chained backend" do
109
- setup_backend!(false)
110
- assert_equal '1', I18n.t(:plural_1, count: 1)
111
- end
112
-
113
- test "subtrees enabled: looks up translations from the second chained backend" do
114
- setup_backend!
115
- assert_equal 'one', I18n.t(:plural_2, count: 1)
116
- end
117
-
118
- test "subtrees disabled: looks up translations from the second chained backend" do
119
- setup_backend!(false)
120
- assert_equal 'one', I18n.t(:plural_2, count: 1)
121
- end
122
- end if I18n::TestCase.key_value?
@@ -1,36 +0,0 @@
1
- require 'test_helper'
2
-
3
- class I18nBackendExceptionsTest < I18n::TestCase
4
- def setup
5
- super
6
- I18n.backend = I18n::Backend::Simple.new
7
- end
8
-
9
- test "throw message: MissingTranslation message from #translate includes the given scope and full key" do
10
- exception = catch(:exception) do
11
- I18n.t(:'baz.missing', :scope => :'foo.bar', :throw => true)
12
- end
13
- assert_equal "translation missing: en.foo.bar.baz.missing", exception.message
14
- end
15
-
16
- test "exceptions: MissingTranslationData message from #translate includes the given scope and full key" do
17
- begin
18
- I18n.t(:'baz.missing', :scope => :'foo.bar', :raise => true)
19
- rescue I18n::MissingTranslationData => exception
20
- end
21
- assert_equal "translation missing: en.foo.bar.baz.missing", exception.message
22
- end
23
-
24
- test "exceptions: MissingTranslationData message from #localize includes the given scope and full key" do
25
- begin
26
- I18n.l(Time.now, :format => :foo)
27
- rescue I18n::MissingTranslationData => exception
28
- end
29
- assert_equal "translation missing: en.time.formats.foo", exception.message
30
- end
31
-
32
- test "exceptions: MissingInterpolationArgument message includes missing key, provided keys and full string" do
33
- exception = I18n::MissingInterpolationArgument.new('key', {:this => 'was given'}, 'string')
34
- assert_equal 'missing interpolation argument "key" in "string" ({:this=>"was given"} given)', exception.message
35
- end
36
- end
@@ -1,211 +0,0 @@
1
- require 'test_helper'
2
-
3
- class I18nBackendFallbacksTranslateTest < I18n::TestCase
4
- class Backend < I18n::Backend::Simple
5
- include I18n::Backend::Fallbacks
6
- end
7
-
8
- def setup
9
- super
10
- I18n.backend = Backend.new
11
- store_translations(:en, :foo => 'Foo in :en', :bar => 'Bar in :en', :buz => 'Buz in :en', :interpolate => 'Interpolate %{value}')
12
- store_translations(:de, :bar => 'Bar in :de', :baz => 'Baz in :de')
13
- store_translations(:'de-DE', :baz => 'Baz in :de-DE')
14
- store_translations(:'pt-BR', :baz => 'Baz in :pt-BR')
15
- end
16
-
17
- test "still returns an existing translation as usual" do
18
- assert_equal 'Foo in :en', I18n.t(:foo, :locale => :en)
19
- assert_equal 'Bar in :de', I18n.t(:bar, :locale => :de)
20
- assert_equal 'Baz in :de-DE', I18n.t(:baz, :locale => :'de-DE')
21
- end
22
-
23
- test "returns interpolated value if no key provided" do
24
- assert_equal 'Interpolate %{value}', I18n.t(:interpolate)
25
- end
26
-
27
- test "returns the :de translation for a missing :'de-DE' translation" do
28
- assert_equal 'Bar in :de', I18n.t(:bar, :locale => :'de-DE')
29
- end
30
-
31
- test "returns the :de translation for a missing :'de-DE' when :default is a String" do
32
- assert_equal 'Bar in :de', I18n.t(:bar, :locale => :'de-DE', :default => "Default Bar")
33
- assert_equal "Default Bar", I18n.t(:missing_bar, :locale => :'de-DE', :default => "Default Bar")
34
- end
35
-
36
- test "returns the :de translation for a missing :'de-DE' when defaults is a Symbol (which exists in :en)" do
37
- assert_equal "Bar in :de", I18n.t(:bar, :locale => :'de-DE', :default => [:buz])
38
- end
39
-
40
- test "returns the :'de-DE' default :baz translation for a missing :'de-DE' (which exists in :de)" do
41
- assert_equal "Baz in :de-DE", I18n.t(:bar, :locale => :'de-DE', :default => [:baz])
42
- end
43
-
44
- test "returns the :de translation for a missing :'de-DE' when :default is a Proc" do
45
- assert_equal 'Bar in :de', I18n.t(:bar, :locale => :'de-DE', :default => Proc.new { "Default Bar" })
46
- assert_equal "Default Bar", I18n.t(:missing_bar, :locale => :'de-DE', :default => Proc.new { "Default Bar" })
47
- end
48
-
49
- test "returns the :de translation for a missing :'de-DE' when :default is a Hash" do
50
- assert_equal 'Bar in :de', I18n.t(:bar, :locale => :'de-DE', :default => {})
51
- assert_equal({}, I18n.t(:missing_bar, :locale => :'de-DE', :default => {}))
52
- end
53
-
54
- test "returns the :de translation for a missing :'de-DE' when :default is nil" do
55
- assert_equal 'Bar in :de', I18n.t(:bar, :locale => :'de-DE', :default => nil)
56
- assert_nil I18n.t(:missing_bar, :locale => :'de-DE', :default => nil)
57
- end
58
-
59
- test "returns the translation missing message if the default is also missing" do
60
- assert_equal 'translation missing: de-DE.missing_bar', I18n.t(:missing_bar, :locale => :'de-DE', :default => [:missing_baz])
61
- end
62
-
63
- test "returns the :'de-DE' default :baz translation for a missing :'de-DE' when defaults contains Symbol" do
64
- assert_equal 'Baz in :de-DE', I18n.t(:missing_foo, :locale => :'de-DE', :default => [:baz, "Default Bar"])
65
- end
66
-
67
- test "returns the defaults translation for a missing :'de-DE' when defaults contains a String or Proc before Symbol" do
68
- assert_equal "Default Bar", I18n.t(:missing_foo, :locale => :'de-DE', :default => [:missing_bar, "Default Bar", :baz])
69
- assert_equal "Default Bar", I18n.t(:missing_foo, :locale => :'de-DE', :default => [:missing_bar, Proc.new { "Default Bar" }, :baz])
70
- end
71
-
72
- test "returns the default translation for a missing :'de-DE' and existing :de when default is a Hash" do
73
- assert_equal 'Default 6 Bars', I18n.t(:missing_foo, :locale => :'de-DE', :default => [:missing_bar, {:other => "Default %{count} Bars"}, "Default Bar"], :count => 6)
74
- end
75
-
76
- test "returns the default translation for a missing :de translation even when default is a String when fallback is disabled" do
77
- assert_equal 'Default String', I18n.t(:foo, :locale => :de, :default => 'Default String', :fallback => false)
78
- end
79
-
80
- test "raises I18n::MissingTranslationData exception when fallback is disabled even when fallback translation exists" do
81
- assert_raise(I18n::MissingTranslationData) { I18n.t(:foo, :locale => :de, :fallback => false, :raise => true) }
82
- end
83
-
84
- test "raises I18n::MissingTranslationData exception when no translation was found" do
85
- assert_raise(I18n::MissingTranslationData) { I18n.t(:faa, :locale => :en, :raise => true) }
86
- assert_raise(I18n::MissingTranslationData) { I18n.t(:faa, :locale => :de, :raise => true) }
87
- end
88
-
89
- test "should ensure that default is not splitted on new line char" do
90
- assert_equal "Default \n Bar", I18n.t(:missing_bar, :default => "Default \n Bar")
91
- end
92
-
93
- test "should not raise error when enforce_available_locales is true, :'pt' is missing and default is a Symbol" do
94
- I18n.enforce_available_locales = true
95
- begin
96
- assert_equal 'Foo', I18n.t(:'model.attrs.foo', :locale => :'pt-BR', :default => [:'attrs.foo', "Foo"])
97
- ensure
98
- I18n.enforce_available_locales = false
99
- end
100
- end
101
-
102
- test "returns fallback default given missing pluralization data" do
103
- assert_equal 'default', I18n.t(:missing_bar, count: 1, default: 'default')
104
- assert_equal 'default', I18n.t(:missing_bar, count: 0, default: 'default')
105
- end
106
- end
107
-
108
- class I18nBackendFallbacksLocalizeTest < I18n::TestCase
109
- class Backend < I18n::Backend::Simple
110
- include I18n::Backend::Fallbacks
111
- end
112
-
113
- def setup
114
- super
115
- I18n.backend = Backend.new
116
- store_translations(:en, :date => { :formats => { :en => 'en' }, :day_names => %w(Sunday) })
117
- store_translations(:de, :date => { :formats => { :de => 'de' }, :day_names => %w(Sunday) })
118
- end
119
-
120
- test "still uses an existing format as usual" do
121
- assert_equal 'en', I18n.l(Date.today, :format => :en, :locale => :en)
122
- end
123
- test "looks up and uses a fallback locale's format for a key missing in the given locale" do
124
- assert_equal 'de', I18n.l(Date.today, :format => :de, :locale => :'de-DE')
125
- end
126
-
127
- test "still uses an existing day name translation as usual" do
128
- assert_equal 'Sunday', I18n.l(Date.new(2010, 1, 3), :format => '%A', :locale => :en)
129
- end
130
-
131
- test "uses a fallback locale's translation for a key missing in the given locale" do
132
- assert_equal 'Sunday', I18n.l(Date.new(2010, 1, 3), :format => '%A', :locale => :'de-DE')
133
- end
134
- end
135
-
136
- class I18nBackendFallbacksWithChainTest < I18n::TestCase
137
- class Backend < I18n::Backend::Simple
138
- include I18n::Backend::Fallbacks
139
- end
140
-
141
- class Chain < I18n::Backend::Chain
142
- include I18n::Backend::Fallbacks
143
- end
144
-
145
- def setup
146
- super
147
- backend = Backend.new
148
- backend.store_translations(:de, :foo => 'FOO')
149
- backend.store_translations(:'pt-BR', :foo => 'Baz in :pt-BR')
150
- I18n.backend = Chain.new(I18n::Backend::Simple.new, backend)
151
- end
152
-
153
- test "falls back from de-DE to de when there is no translation for de-DE available" do
154
- assert_equal 'FOO', I18n.t(:foo, :locale => :'de-DE')
155
- end
156
-
157
- test "falls back from de-DE to de when there is no translation for de-DE available when using arrays, too" do
158
- assert_equal ['FOO', 'FOO'], I18n.t([:foo, :foo], :locale => :'de-DE')
159
- end
160
-
161
- test "should not raise error when enforce_available_locales is true, :'pt' is missing and default is a Symbol" do
162
- I18n.enforce_available_locales = true
163
- begin
164
- assert_equal 'Foo', I18n.t(:'model.attrs.foo', :locale => :'pt-BR', :default => [:'attrs.foo', "Foo"])
165
- ensure
166
- I18n.enforce_available_locales = false
167
- end
168
- end
169
- end
170
-
171
- class I18nBackendFallbacksExistsTest < I18n::TestCase
172
- class Backend < I18n::Backend::Simple
173
- include I18n::Backend::Fallbacks
174
- end
175
-
176
- def setup
177
- super
178
- I18n.backend = Backend.new
179
- store_translations(:en, :foo => 'Foo in :en', :bar => 'Bar in :en')
180
- store_translations(:de, :bar => 'Bar in :de')
181
- store_translations(:'de-DE', :baz => 'Baz in :de-DE')
182
- end
183
-
184
- test "exists? given an existing key will return true" do
185
- assert_equal true, I18n.exists?(:foo)
186
- end
187
-
188
- test "exists? given a non-existing key will return false" do
189
- assert_equal false, I18n.exists?(:bogus)
190
- end
191
-
192
- test "exists? given an existing key and an existing locale will return true" do
193
- assert_equal true, I18n.exists?(:foo, :en)
194
- assert_equal true, I18n.exists?(:bar, :de)
195
- end
196
-
197
- test "exists? given a non-existing key and an existing locale will return false" do
198
- assert_equal false, I18n.exists?(:bogus, :en)
199
- assert_equal false, I18n.exists?(:bogus, :de)
200
- end
201
-
202
- test "exists? should return true given a key which is missing from the given locale and exists in a fallback locale" do
203
- assert_equal true, I18n.exists?(:bar, :de)
204
- assert_equal true, I18n.exists?(:bar, :'de-DE')
205
- end
206
-
207
- test "exists? should return false given a key which is missing from the given locale and all its fallback locales" do
208
- assert_equal false, I18n.exists?(:baz, :de)
209
- assert_equal false, I18n.exists?(:bogus, :'de-DE')
210
- end
211
- end
@@ -1,118 +0,0 @@
1
- require 'test_helper'
2
-
3
- class InterpolationCompilerTest < I18n::TestCase
4
- Compiler = I18n::Backend::InterpolationCompiler::Compiler
5
-
6
- def compile_and_interpolate(str, values = {})
7
- Compiler.compile_if_an_interpolation(str).i18n_interpolate(values)
8
- end
9
-
10
- def assert_escapes_interpolation_key(expected, malicious_str)
11
- assert_equal(expected, Compiler.send(:escape_key_sym, malicious_str))
12
- end
13
-
14
- def test_escape_key_properly_escapes
15
- assert_escapes_interpolation_key ':"\""', '"'
16
- assert_escapes_interpolation_key ':"\\\\"', '\\'
17
- assert_escapes_interpolation_key ':"\\\\\""', '\\"'
18
- assert_escapes_interpolation_key ':"\#{}"', '#{}'
19
- assert_escapes_interpolation_key ':"\\\\\#{}"', '\#{}'
20
- end
21
-
22
- def assert_escapes_plain_string(expected, plain_str)
23
- assert_equal expected, Compiler.send(:escape_plain_str, plain_str)
24
- end
25
-
26
- def test_escape_plain_string_properly_escapes
27
- assert_escapes_plain_string '\\"', '"'
28
- assert_escapes_plain_string '\'', '\''
29
- assert_escapes_plain_string '\\#', '#'
30
- assert_escapes_plain_string '\\#{}', '#{}'
31
- assert_escapes_plain_string '\\\\\\"','\\"'
32
- end
33
-
34
- def test_non_interpolated_strings_or_arrays_dont_get_compiled
35
- ['abc', '\\{a}}', '{a}}', []].each do |obj|
36
- Compiler.compile_if_an_interpolation(obj)
37
- assert_equal false, obj.respond_to?(:i18n_interpolate)
38
- end
39
- end
40
-
41
- def test_interpolated_string_gets_compiled
42
- assert_equal '-A-', compile_and_interpolate('-%{a}-', :a => 'A')
43
- end
44
-
45
- def assert_handles_key(str, key)
46
- assert_equal 'A', compile_and_interpolate(str, key => 'A')
47
- end
48
-
49
- def test_compiles_fancy_keys
50
- assert_handles_key('%{\}', :'\\' )
51
- assert_handles_key('%{#}', :'#' )
52
- assert_handles_key('%{#{}', :'#{' )
53
- assert_handles_key('%{#$SAFE}', :'#$SAFE')
54
- assert_handles_key('%{\000}', :'\000' )
55
- assert_handles_key('%{\'}', :'\'' )
56
- assert_handles_key('%{\'\'}', :'\'\'' )
57
- assert_handles_key('%{a.b}', :'a.b' )
58
- assert_handles_key('%{ }', :' ' )
59
- assert_handles_key('%{:}', :':' )
60
- assert_handles_key("%{:''}", :":''" )
61
- assert_handles_key('%{:"}', :':"' )
62
- end
63
-
64
- def test_str_containing_only_escaped_interpolation_is_handled_correctly
65
- assert_equal 'abc %{x}', compile_and_interpolate('abc %%{x}')
66
- end
67
-
68
- def test_handles_weird_strings
69
- assert_equal '#{} a', compile_and_interpolate('#{} %{a}', :a => 'a')
70
- assert_equal '"#{abc}"', compile_and_interpolate('"#{ab%{a}c}"', :a => '' )
71
- assert_equal 'a}', compile_and_interpolate('%{{a}}', :'{a' => 'a')
72
- assert_equal '"', compile_and_interpolate('"%{a}', :a => '' )
73
- assert_equal 'a%{a}', compile_and_interpolate('%{a}%%{a}', :a => 'a')
74
- assert_equal '%%{a}', compile_and_interpolate('%%%{a}')
75
- assert_equal '\";eval("a")', compile_and_interpolate('\";eval("%{a}")', :a => 'a')
76
- assert_equal '\";eval("a")', compile_and_interpolate('\";eval("a")%{a}', :a => '' )
77
- assert_equal "\na", compile_and_interpolate("\n%{a}", :a => 'a')
78
- end
79
-
80
- def test_raises_exception_when_argument_is_missing
81
- assert_raise(I18n::MissingInterpolationArgument) do
82
- compile_and_interpolate('%{first} %{last}', :first => 'first')
83
- end
84
- end
85
-
86
- def test_custom_missing_interpolation_argument_handler
87
- old_handler = I18n.config.missing_interpolation_argument_handler
88
- I18n.config.missing_interpolation_argument_handler = lambda do |key, values, string|
89
- "missing key is #{key}, values are #{values.inspect}, given string is '#{string}'"
90
- end
91
- assert_equal %|first missing key is last, values are {:first=>"first"}, given string is '%{first} %{last}'|,
92
- compile_and_interpolate('%{first} %{last}', :first => 'first')
93
- ensure
94
- I18n.config.missing_interpolation_argument_handler = old_handler
95
- end
96
- end
97
-
98
- class I18nBackendInterpolationCompilerTest < I18n::TestCase
99
- class Backend < I18n::Backend::Simple
100
- include I18n::Backend::InterpolationCompiler
101
- end
102
-
103
- include I18n::Tests::Interpolation
104
-
105
- def setup
106
- I18n.backend = Backend.new
107
- super
108
- end
109
-
110
- # pre-compile default strings to make sure we are testing I18n::Backend::InterpolationCompiler
111
- def interpolate(*args)
112
- options = args.last.kind_of?(Hash) ? args.last : {}
113
- if default_str = options[:default]
114
- I18n::Backend::InterpolationCompiler::Compiler.compile_if_an_interpolation(default_str)
115
- end
116
- super
117
- end
118
- end