i18n 0.5.0beta1 → 0.5.0beta2

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of i18n might be problematic. Click here for more details.

Files changed (62) hide show
  1. data/README.textile +4 -17
  2. data/ci/Gemfile.no-rails +5 -0
  3. data/ci/Gemfile.no-rails.lock +17 -0
  4. data/ci/Gemfile.rails-2.3.x +9 -0
  5. data/ci/Gemfile.rails-2.3.x.lock +26 -0
  6. data/ci/Gemfile.rails-3.x +9 -0
  7. data/ci/Gemfile.rails-3.x.lock +26 -0
  8. data/lib/i18n.rb +4 -8
  9. data/lib/i18n/backend.rb +0 -1
  10. data/lib/i18n/backend/base.rb +4 -32
  11. data/lib/i18n/backend/gettext.rb +3 -3
  12. data/lib/i18n/backend/interpolation_compiler.rb +1 -1
  13. data/lib/i18n/backend/metadata.rb +1 -1
  14. data/lib/i18n/config.rb +1 -1
  15. data/lib/i18n/core_ext/kernel/surpress_warnings.rb +9 -0
  16. data/lib/i18n/core_ext/string/interpolate.rb +0 -96
  17. data/lib/i18n/exceptions.rb +0 -6
  18. data/lib/i18n/interpolate/ruby.rb +31 -0
  19. data/lib/i18n/locale/fallbacks.rb +3 -3
  20. data/lib/i18n/tests/localization/procs.rb +32 -19
  21. data/lib/i18n/tests/localization/time.rb +2 -2
  22. data/lib/i18n/version.rb +1 -1
  23. data/test/all.rb +8 -0
  24. data/test/api/all_features_test.rb +58 -0
  25. data/test/api/cascade_test.rb +28 -0
  26. data/test/api/chain_test.rb +24 -0
  27. data/test/api/fallbacks_test.rb +30 -0
  28. data/test/api/key_value_test.rb +28 -0
  29. data/test/api/memoize_test.rb +60 -0
  30. data/test/api/pluralization_test.rb +30 -0
  31. data/test/api/simple_test.rb +28 -0
  32. data/test/backend/cache_test.rb +83 -0
  33. data/test/backend/cascade_test.rb +85 -0
  34. data/test/backend/chain_test.rb +67 -0
  35. data/test/backend/exceptions_test.rb +23 -0
  36. data/test/backend/fallbacks_test.rb +116 -0
  37. data/test/backend/interpolation_compiler_test.rb +102 -0
  38. data/test/backend/key_value_test.rb +46 -0
  39. data/test/backend/memoize_test.rb +47 -0
  40. data/test/backend/metadata_test.rb +67 -0
  41. data/test/backend/pluralization_test.rb +44 -0
  42. data/test/backend/simple_test.rb +79 -0
  43. data/test/backend/transliterator_test.rb +81 -0
  44. data/test/core_ext/hash_test.rb +30 -0
  45. data/test/gettext/api_test.rb +206 -0
  46. data/test/gettext/backend_test.rb +93 -0
  47. data/test/i18n/exceptions_test.rb +116 -0
  48. data/test/i18n/interpolate_test.rb +60 -0
  49. data/test/i18n/load_path_test.rb +26 -0
  50. data/test/i18n_test.rb +236 -0
  51. data/test/locale/fallbacks_test.rb +124 -0
  52. data/test/locale/tag/rfc4646_test.rb +142 -0
  53. data/test/locale/tag/simple_test.rb +32 -0
  54. data/test/run_all.rb +21 -0
  55. data/test/test_data/locales/de.po +72 -0
  56. data/test/test_data/locales/en.rb +3 -0
  57. data/test/test_data/locales/en.yml +3 -0
  58. data/test/test_data/locales/invalid/empty.yml +1 -0
  59. data/test/test_data/locales/plurals.rb +113 -0
  60. data/test/test_helper.rb +56 -0
  61. metadata +116 -14
  62. data/lib/i18n/backend/cldr.rb +0 -99
@@ -0,0 +1,93 @@
1
+ # encoding: utf-8
2
+
3
+ require 'test_helper'
4
+
5
+ # apparently Ruby 1.9.1p129 has encoding problems with the gettext po parser
6
+ unless RUBY_VERSION == '1.9.1' && RUBY_PATCHLEVEL <= 129
7
+
8
+ class I18nGettextBackendTest < Test::Unit::TestCase
9
+ include I18n::Gettext::Helpers
10
+
11
+ class Backend < I18n::Backend::Simple
12
+ include I18n::Backend::Gettext
13
+ end
14
+
15
+ def setup
16
+ I18n.backend = Backend.new
17
+ I18n.locale = :en
18
+ I18n.load_path = ["#{locales_dir}/de.po"]
19
+ @old_separator, I18n.default_separator = I18n.default_separator, '|'
20
+ end
21
+
22
+ def teardown
23
+ I18n.load_path = nil
24
+ I18n.backend = nil
25
+ I18n.default_separator = @old_separator
26
+ end
27
+
28
+ def test_backend_loads_po_file
29
+ I18n.backend.send(:init_translations)
30
+ assert I18n.backend.send(:translations)[:de][:"Axis"]
31
+ end
32
+
33
+ def test_looks_up_a_translation
34
+ I18n.locale = :de
35
+ assert_equal 'Auto', gettext('car')
36
+ end
37
+
38
+ def test_uses_default_translation
39
+ assert_equal 'car', gettext('car')
40
+ end
41
+
42
+ def test_looks_up_a_namespaced_translation
43
+ I18n.locale = :de
44
+ assert_equal 'Räderzahl', sgettext('Car|Wheels count')
45
+ assert_equal 'Räderzahl', pgettext('Car', 'Wheels count')
46
+ end
47
+
48
+ def test_uses_namespaced_default_translation
49
+ assert_equal 'Wheels count', sgettext('Car|Wheels count')
50
+ assert_equal 'Wheels count', pgettext('Car', 'Wheels count')
51
+ end
52
+
53
+ def test_pluralizes_entry
54
+ I18n.locale = :de
55
+ assert_equal 'Achse', ngettext('Axis', 'Axis', 1)
56
+ assert_equal 'Achsen', ngettext('Axis', 'Axis', 2)
57
+ end
58
+
59
+ def test_pluralizes_default_entry
60
+ assert_equal 'Axis', ngettext('Axis', 'Axis', 1)
61
+ assert_equal 'Axis', ngettext('Axis', 'Axis', 2)
62
+ end
63
+
64
+ def test_pluralizes_namespaced_entry
65
+ I18n.locale = :de
66
+ assert_equal 'Rad', nsgettext('Car|wheel', 'wheels', 1)
67
+ assert_equal 'Räder', nsgettext('Car|wheel', 'wheels', 2)
68
+ assert_equal 'Rad', npgettext('Car', 'wheel', 'wheels', 1)
69
+ assert_equal 'Räder', npgettext('Car', 'wheel', 'wheels', 2)
70
+ end
71
+
72
+ def test_pluralizes_namespaced_default_entry
73
+ assert_equal 'wheel', nsgettext('Car|wheel', 'wheels', 1)
74
+ assert_equal 'wheels', nsgettext('Car|wheel', 'wheels', 2)
75
+ assert_equal 'wheel', npgettext('Car', 'wheel', 'wheels', 1)
76
+ assert_equal 'wheels', npgettext('Car', 'wheel', 'wheels', 2)
77
+ end
78
+
79
+ def test_pluralizes_namespaced_entry_with_alternative_syntax
80
+ I18n.locale = :de
81
+ assert_equal 'Rad', nsgettext(['Car|wheel', 'wheels'], 1)
82
+ assert_equal 'Räder', nsgettext(['Car|wheel', 'wheels'], 2)
83
+ assert_equal 'Rad', npgettext('Car', ['wheel', 'wheels'], 1)
84
+ assert_equal 'Räder', npgettext('Car', ['wheel', 'wheels'], 2)
85
+ end
86
+
87
+ def test_ngettextpluralizes_entry_with_dots
88
+ I18n.locale = :de
89
+ assert_equal 'Auf 1 Achse.', n_("On %{count} wheel.", "On %{count} wheels.", 1)
90
+ assert_equal 'Auf 2 Achsen.', n_("On %{count} wheel.", "On %{count} wheels.", 2)
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,116 @@
1
+ require 'test_helper'
2
+
3
+ class I18nExceptionsTest < Test::Unit::TestCase
4
+ def test_invalid_locale_stores_locale
5
+ force_invalid_locale
6
+ rescue I18n::ArgumentError => e
7
+ assert_nil exception.locale
8
+ end
9
+
10
+ test "passing an invalid locale raises an InvalidLocale exception" do
11
+ force_invalid_locale do |exception|
12
+ assert_equal 'nil is not a valid locale', exception.message
13
+ end
14
+ end
15
+
16
+ test "MissingTranslationData exception stores locale, key and options" do
17
+ force_missing_translation_data do |exception|
18
+ assert_equal 'de', exception.locale
19
+ assert_equal :foo, exception.key
20
+ assert_equal({:scope => :bar}, exception.options)
21
+ end
22
+ end
23
+
24
+ test "MissingTranslationData message contains the locale and scoped key" do
25
+ force_missing_translation_data do |exception|
26
+ assert_equal 'translation missing: de.bar.foo', exception.message
27
+ end
28
+ end
29
+
30
+ test "MissingTranslationData html_message is a span with the titlelized last key token" do
31
+ force_missing_translation_data do |exception|
32
+ assert_equal '<span class="translation_missing" title="translation missing: de.bar.foo">Foo</span>', exception.html_message
33
+ end
34
+ end
35
+
36
+ test "ExceptionHandler returns the html_message if :rescue_format => :html was given" do
37
+ message = force_missing_translation_data(:rescue_format => :html)
38
+ assert_equal '<span class="translation_missing" title="translation missing: de.bar.foo">Foo</span>', message
39
+ end
40
+
41
+ test "InvalidPluralizationData stores entry and count" do
42
+ force_invalid_pluralization_data do |exception|
43
+ assert_equal [:bar], exception.entry
44
+ assert_equal 1, exception.count
45
+ end
46
+ end
47
+
48
+ test "InvalidPluralizationData message contains count and data" do
49
+ force_invalid_pluralization_data do |exception|
50
+ assert_equal 'translation data [:bar] can not be used with :count => 1', exception.message
51
+ end
52
+ end
53
+
54
+ test "MissingInterpolationArgument stores key and string" do
55
+ assert_raise(I18n::MissingInterpolationArgument) { force_missing_interpolation_argument }
56
+ force_missing_interpolation_argument do |exception|
57
+ # assert_equal :bar, exception.key
58
+ assert_equal "%{bar}", exception.string
59
+ end
60
+ end
61
+
62
+ test "MissingInterpolationArgument message contains the missing and given arguments" do
63
+ force_missing_interpolation_argument do |exception|
64
+ assert_equal 'missing interpolation argument in "%{bar}" ({:baz=>"baz"} given)', exception.message
65
+ end
66
+ end
67
+
68
+ test "ReservedInterpolationKey stores key and string" do
69
+ force_reserved_interpolation_key do |exception|
70
+ assert_equal :scope, exception.key
71
+ assert_equal "%{scope}", exception.string
72
+ end
73
+ end
74
+
75
+ test "ReservedInterpolationKey message contains the reserved key" do
76
+ force_reserved_interpolation_key do |exception|
77
+ assert_equal 'reserved key :scope used in "%{scope}"', exception.message
78
+ end
79
+ end
80
+
81
+ private
82
+
83
+ def force_invalid_locale
84
+ I18n.translate(:foo, :locale => nil)
85
+ rescue I18n::ArgumentError => e
86
+ block_given? ? yield(e) : raise(e)
87
+ end
88
+
89
+ def force_missing_translation_data(options = {})
90
+ I18n.backend.store_translations('de', :bar => nil)
91
+ I18n.translate(:foo, options.merge(:scope => :bar, :locale => :de))
92
+ rescue I18n::ArgumentError => e
93
+ block_given? ? yield(e) : raise(e)
94
+ end
95
+
96
+ def force_invalid_pluralization_data
97
+ I18n.backend.store_translations('de', :foo => [:bar])
98
+ I18n.translate(:foo, :count => 1, :locale => :de)
99
+ rescue I18n::ArgumentError => e
100
+ block_given? ? yield(e) : raise(e)
101
+ end
102
+
103
+ def force_missing_interpolation_argument
104
+ I18n.backend.store_translations('de', :foo => "%{bar}")
105
+ I18n.translate(:foo, :baz => 'baz', :locale => :de)
106
+ rescue I18n::ArgumentError => e
107
+ block_given? ? yield(e) : raise(e)
108
+ end
109
+
110
+ def force_reserved_interpolation_key
111
+ I18n.backend.store_translations('de', :foo => "%{scope}")
112
+ I18n.translate(:foo, :baz => 'baz', :locale => :de)
113
+ rescue I18n::ArgumentError => e
114
+ block_given? ? yield(e) : raise(e)
115
+ end
116
+ end
@@ -0,0 +1,60 @@
1
+ require 'test_helper'
2
+
3
+ # thanks to Masao's String extensions, some tests taken from Masao's tests
4
+ # http://github.com/mutoh/gettext/blob/edbbe1fa8238fa12c7f26f2418403015f0270e47/test/test_string.rb
5
+
6
+ class I18nInterpolateTest < Test::Unit::TestCase
7
+ test "String interpolates a hash argument w/ named placeholders" do
8
+ assert_equal "Masao Mutoh", I18n.interpolate("%{first} %{last}", :first => 'Masao', :last => 'Mutoh' )
9
+ end
10
+
11
+ test "String interpolates a hash argument w/ named placeholders (reverse order)" do
12
+ assert_equal "Mutoh, Masao", I18n.interpolate("%{last}, %{first}", :first => 'Masao', :last => 'Mutoh' )
13
+ end
14
+
15
+ test "String interpolates named placeholders with sprintf syntax" do
16
+ assert_equal "10, 43.4", I18n.interpolate("%<integer>d, %<float>.1f", :integer => 10, :float => 43.4)
17
+ end
18
+
19
+ test "String interpolates named placeholders with sprintf syntax, does not recurse" do
20
+ assert_equal "%<not_translated>s", I18n.interpolate("%{msg}", :msg => '%<not_translated>s', :not_translated => 'should not happen' )
21
+ end
22
+
23
+ test "String interpolation does not replace anything when no placeholders are given" do
24
+ assert_equal "aaa", I18n.interpolate("aaa", :num => 1)
25
+ end
26
+
27
+ test "String interpolation sprintf behaviour equals Ruby 1.9 behaviour" do
28
+ assert_equal "1", I18n.interpolate("%<num>d", :num => 1)
29
+ assert_equal "0b1", I18n.interpolate("%<num>#b", :num => 1)
30
+ assert_equal "foo", I18n.interpolate("%<msg>s", :msg => "foo")
31
+ assert_equal "1.000000", I18n.interpolate("%<num>f", :num => 1.0)
32
+ assert_equal " 1", I18n.interpolate("%<num>3.0f", :num => 1.0)
33
+ assert_equal "100.00", I18n.interpolate("%<num>2.2f", :num => 100.0)
34
+ assert_equal "0x64", I18n.interpolate("%<num>#x", :num => 100.0)
35
+ assert_raise(ArgumentError) { I18n.interpolate("%<num>,d", :num => 100) }
36
+ assert_raise(ArgumentError) { I18n.interpolate("%<num>/d", :num => 100) }
37
+ end
38
+
39
+ test "String interpolation raises an I18n::MissingInterpolationArgument when the string has extra placeholders" do
40
+ assert_raise(I18n::MissingInterpolationArgument) do # Ruby 1.9 msg: "key not found"
41
+ I18n.interpolate("%{first} %{last}", :first => 'Masao')
42
+ end
43
+ end
44
+
45
+ test "String interpolation does not raise when extra values were passed" do
46
+ assert_nothing_raised do
47
+ assert_equal "Masao Mutoh", I18n.interpolate("%{first} %{last}", :first => 'Masao', :last => 'Mutoh', :salutation => 'Mr.' )
48
+ end
49
+ end
50
+
51
+ test "% acts as escape character in String interpolation" do
52
+ assert_equal "%{first}", I18n.interpolate("%%{first}", :first => 'Masao')
53
+ assert_equal "% 1", I18n.interpolate("%% %<num>d", :num => 1.0)
54
+ assert_equal "%{num} %<num>d", I18n.interpolate("%%{num} %%<num>d", :num => 1)
55
+ end
56
+
57
+ def test_sprintf_mix_unformatted_and_formatted_named_placeholders
58
+ assert_equal "foo 1.000000", I18n.interpolate("%{name} %<num>f", :name => "foo", :num => 1.0)
59
+ end
60
+ end
@@ -0,0 +1,26 @@
1
+ require 'test_helper'
2
+
3
+ class I18nLoadPathTest < Test::Unit::TestCase
4
+ def setup
5
+ I18n.locale = :en
6
+ I18n.backend = I18n::Backend::Simple.new
7
+ store_translations(:en, :foo => {:bar => 'bar', :baz => 'baz'})
8
+ end
9
+
10
+ test "nested load paths do not break locale loading" do
11
+ I18n.load_path = [[locales_dir + '/en.yml']]
12
+ assert_equal "baz", I18n.t(:'foo.bar')
13
+ end
14
+
15
+ test "loading an empty yml file raises an InvalidLocaleData exception" do
16
+ assert_raise I18n::InvalidLocaleData do
17
+ I18n.load_path = [[locales_dir + '/invalid/empty.yml']]
18
+ I18n.t(:'foo.bar', :default => "baz")
19
+ end
20
+ end
21
+
22
+ test "adding arrays of filenames to the load path does not break locale loading" do
23
+ I18n.load_path << Dir[locales_dir + '/*.{rb,yml}']
24
+ assert_equal "baz", I18n.t(:'foo.bar')
25
+ end
26
+ end
@@ -0,0 +1,236 @@
1
+ require 'test_helper'
2
+
3
+ class I18nTest < Test::Unit::TestCase
4
+ def setup
5
+ I18n.backend.store_translations(:'en', :currency => { :format => { :separator => '.', :delimiter => ',', } })
6
+ end
7
+
8
+ test "exposes its VERSION constant" do
9
+ assert I18n::VERSION
10
+ end
11
+
12
+ test "uses the simple backend by default" do
13
+ assert I18n.backend.is_a?(I18n::Backend::Simple)
14
+ end
15
+
16
+ test "can set the backend" do
17
+ begin
18
+ assert_nothing_raised { I18n.backend = self }
19
+ assert_equal self, I18n.backend
20
+ ensure
21
+ I18n.backend = I18n::Backend::Simple.new
22
+ end
23
+ end
24
+
25
+ test "uses :en as a default_locale by default" do
26
+ assert_equal :en, I18n.default_locale
27
+ end
28
+
29
+ test "can set the default locale" do
30
+ begin
31
+ assert_nothing_raised { I18n.default_locale = 'de' }
32
+ assert_equal :de, I18n.default_locale
33
+ ensure
34
+ I18n.default_locale = :en
35
+ end
36
+ end
37
+
38
+ test "uses the default locale as a locale by default" do
39
+ assert_equal I18n.default_locale, I18n.locale
40
+ end
41
+
42
+ test "sets the current locale to Thread.current" do
43
+ assert_nothing_raised { I18n.locale = 'de' }
44
+ assert_equal :de, I18n.locale
45
+ assert_equal :de, Thread.current[:i18n_config].locale
46
+ I18n.locale = :en
47
+ end
48
+
49
+ test "can set the configuration object" do
50
+ begin
51
+ I18n.config = self
52
+ assert_equal self, I18n.config
53
+ assert_equal self, Thread.current[:i18n_config]
54
+ ensure
55
+ I18n.config = ::I18n::Config.new
56
+ end
57
+ end
58
+
59
+ test "locale is not shared between configurations" do
60
+ a = I18n::Config.new
61
+ b = I18n::Config.new
62
+ a.locale = :fr
63
+ b.locale = :es
64
+ assert_equal :fr, a.locale
65
+ assert_equal :es, b.locale
66
+ assert_equal :en, I18n.locale
67
+ end
68
+
69
+ test "other options are shared between configurations" do
70
+ begin
71
+ a = I18n::Config.new
72
+ b = I18n::Config.new
73
+ a.default_locale = :fr
74
+ b.default_locale = :es
75
+ assert_equal :es, a.default_locale
76
+ assert_equal :es, b.default_locale
77
+ assert_equal :es, I18n.default_locale
78
+ ensure
79
+ I18n.default_locale = :en
80
+ end
81
+ end
82
+
83
+ test "uses a dot as a default_separator by default" do
84
+ assert_equal '.', I18n.default_separator
85
+ end
86
+
87
+ test "can set the default_separator" do
88
+ begin
89
+ assert_nothing_raised { I18n.default_separator = "\001" }
90
+ ensure
91
+ I18n.default_separator = '.'
92
+ end
93
+ end
94
+
95
+ test "normalize_keys normalizes given locale, keys and scope to an array of single-key symbols" do
96
+ assert_equal [:en, :foo, :bar], I18n.normalize_keys(:en, :bar, :foo)
97
+ assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, :'baz.buz', :'foo.bar')
98
+ assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, 'baz.buz', 'foo.bar')
99
+ assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, %w(baz buz), %w(foo bar))
100
+ assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, [:baz, :buz], [:foo, :bar])
101
+ end
102
+
103
+ test "normalize_keys discards empty keys" do
104
+ assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, :'baz..buz', :'foo..bar')
105
+ assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, :'baz......buz', :'foo......bar')
106
+ assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, ['baz', nil, '', 'buz'], ['foo', nil, '', 'bar'])
107
+ end
108
+
109
+ test "normalize_keys uses a given separator" do
110
+ assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, :'baz|buz', :'foo|bar', '|')
111
+ end
112
+
113
+ test "can set the exception_handler" do
114
+ begin
115
+ previous_exception_handler = I18n.exception_handler
116
+ assert_nothing_raised { I18n.exception_handler = :custom_exception_handler }
117
+ ensure
118
+ I18n.exception_handler = previous_exception_handler
119
+ end
120
+ end
121
+
122
+ test "uses a custom exception handler set to I18n.exception_handler" do
123
+ begin
124
+ previous_exception_handler = I18n.exception_handler
125
+ I18n.exception_handler = :custom_exception_handler
126
+ I18n.expects(:custom_exception_handler)
127
+ I18n.translate :bogus
128
+ ensure
129
+ I18n.exception_handler = previous_exception_handler
130
+ end
131
+ end
132
+
133
+ test "uses a custom exception handler passed as an option" do
134
+ I18n.expects(:custom_exception_handler)
135
+ I18n.translate(:bogus, :exception_handler => :custom_exception_handler)
136
+ end
137
+
138
+ test "delegates translate calls to the backend" do
139
+ I18n.backend.expects(:translate).with('de', :foo, {})
140
+ I18n.translate :foo, :locale => 'de'
141
+ end
142
+
143
+ test "delegates localize calls to the backend" do
144
+ I18n.backend.expects(:localize).with('de', :whatever, :default, {})
145
+ I18n.localize :whatever, :locale => 'de'
146
+ end
147
+
148
+ test "translate given no locale uses the current locale" do
149
+ I18n.backend.expects(:translate).with(:en, :foo, {})
150
+ I18n.translate :foo
151
+ end
152
+
153
+ test "translate works with nested symbol keys" do
154
+ assert_equal ".", I18n.t(:'currency.format.separator')
155
+ end
156
+
157
+ test "translate works with nested string keys" do
158
+ assert_equal ".", I18n.t('currency.format.separator')
159
+ end
160
+
161
+ test "translate with an array as a scope works" do
162
+ assert_equal ".", I18n.t(:separator, :scope => %w(currency format))
163
+ end
164
+
165
+ test "translate with an array containing dot separated strings as a scope works" do
166
+ assert_equal ".", I18n.t(:separator, :scope => ['currency.format'])
167
+ end
168
+
169
+ test "translate with an array of keys and a dot separated string as a scope works" do
170
+ assert_equal [".", ","], I18n.t(%w(separator delimiter), :scope => 'currency.format')
171
+ end
172
+
173
+ test "translate with an array of dot separated keys and a scope works" do
174
+ assert_equal [".", ","], I18n.t(%w(format.separator format.delimiter), :scope => 'currency')
175
+ end
176
+
177
+ # def test_translate_given_no_args_raises_missing_translation_data
178
+ # assert_equal "translation missing: en, no key", I18n.t
179
+ # end
180
+
181
+ test "translate given a bogus key returns an error message" do
182
+ assert_equal "translation missing: en.bogus", I18n.t(:bogus)
183
+ end
184
+
185
+ test "translate given an empty string as a key raises an I18n::ArgumentError" do
186
+ assert_raise(I18n::ArgumentError) { I18n.t("") }
187
+ end
188
+
189
+ test "localize given nil raises an I18n::ArgumentError" do
190
+ assert_raise(I18n::ArgumentError) { I18n.l nil }
191
+ end
192
+
193
+ test "localize givan an Object raises an I18n::ArgumentError" do
194
+ assert_raise(I18n::ArgumentError) { I18n.l Object.new }
195
+ end
196
+
197
+ test "can use a lambda as an exception handler" do
198
+ begin
199
+ previous_exception_handler = I18n.exception_handler
200
+ I18n.exception_handler = Proc.new { |exception, locale, key, options| exception }
201
+ assert_equal I18n::MissingTranslationData, I18n.translate(:test_proc_handler).class
202
+ ensure
203
+ I18n.exception_handler = previous_exception_handler
204
+ end
205
+ end
206
+
207
+ test "can use an object responding to #call as an exception handler" do
208
+ begin
209
+ previous_exception_handler = I18n.exception_handler
210
+ I18n.exception_handler = Class.new do
211
+ def call(exception, locale, key, options); exception; end
212
+ end.new
213
+ assert_equal I18n::MissingTranslationData, I18n.translate(:test_proc_handler).class
214
+ ensure
215
+ I18n.exception_handler = previous_exception_handler
216
+ end
217
+ end
218
+
219
+ test "I18n.with_locale temporarily sets the given locale" do
220
+ store_translations(:en, :foo => 'Foo in :en')
221
+ store_translations(:de, :foo => 'Foo in :de')
222
+ store_translations(:pl, :foo => 'Foo in :pl')
223
+
224
+ I18n.with_locale { assert_equal [:en, 'Foo in :en'], [I18n.locale, I18n.t(:foo)] }
225
+ I18n.with_locale(:de) { assert_equal [:de, 'Foo in :de'], [I18n.locale, I18n.t(:foo)] }
226
+ I18n.with_locale(:pl) { assert_equal [:pl, 'Foo in :pl'], [I18n.locale, I18n.t(:foo)] }
227
+ I18n.with_locale(:en) { assert_equal [:en, 'Foo in :en'], [I18n.locale, I18n.t(:foo)] }
228
+
229
+ assert_equal I18n.default_locale, I18n.locale
230
+ end
231
+
232
+ test "I18n.with_locale resets the locale in case of errors" do
233
+ assert_raise(I18n::ArgumentError) { I18n.with_locale(:pl) { raise I18n::ArgumentError } }
234
+ assert_equal I18n.default_locale, I18n.locale
235
+ end
236
+ end