i18n 0.5.3 → 0.5.4

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 (58) hide show
  1. checksums.yaml +15 -0
  2. data/CHANGELOG.textile +12 -3
  3. data/README.textile +4 -17
  4. data/lib/i18n.rb +5 -9
  5. data/lib/i18n/backend.rb +0 -1
  6. data/lib/i18n/backend/base.rb +4 -32
  7. data/lib/i18n/backend/gettext.rb +3 -3
  8. data/lib/i18n/backend/interpolation_compiler.rb +1 -1
  9. data/lib/i18n/backend/metadata.rb +1 -1
  10. data/lib/i18n/config.rb +1 -1
  11. data/lib/i18n/core_ext/kernel/surpress_warnings.rb +9 -0
  12. data/lib/i18n/core_ext/string/interpolate.rb +9 -0
  13. data/lib/i18n/exceptions.rb +1 -7
  14. data/lib/i18n/interpolate/ruby.rb +31 -0
  15. data/lib/i18n/locale/fallbacks.rb +3 -3
  16. data/lib/i18n/tests/localization/procs.rb +27 -26
  17. data/lib/i18n/version.rb +1 -1
  18. data/test/all.rb +8 -0
  19. data/test/api/all_features_test.rb +58 -0
  20. data/test/api/cascade_test.rb +28 -0
  21. data/test/api/chain_test.rb +24 -0
  22. data/test/api/fallbacks_test.rb +30 -0
  23. data/test/api/key_value_test.rb +28 -0
  24. data/test/api/memoize_test.rb +60 -0
  25. data/test/api/pluralization_test.rb +30 -0
  26. data/test/api/simple_test.rb +28 -0
  27. data/test/backend/cache_test.rb +83 -0
  28. data/test/backend/cascade_test.rb +85 -0
  29. data/test/backend/chain_test.rb +67 -0
  30. data/test/backend/exceptions_test.rb +23 -0
  31. data/test/backend/fallbacks_test.rb +116 -0
  32. data/test/backend/interpolation_compiler_test.rb +102 -0
  33. data/test/backend/key_value_test.rb +46 -0
  34. data/test/backend/memoize_test.rb +47 -0
  35. data/test/backend/metadata_test.rb +67 -0
  36. data/test/backend/pluralization_test.rb +44 -0
  37. data/test/backend/simple_test.rb +79 -0
  38. data/test/backend/transliterator_test.rb +81 -0
  39. data/test/core_ext/hash_test.rb +30 -0
  40. data/test/core_ext/string/interpolate_test.rb +99 -0
  41. data/test/gettext/api_test.rb +206 -0
  42. data/test/gettext/backend_test.rb +93 -0
  43. data/test/i18n/exceptions_test.rb +120 -0
  44. data/test/i18n/interpolate_test.rb +61 -0
  45. data/test/i18n/load_path_test.rb +26 -0
  46. data/test/i18n_test.rb +323 -0
  47. data/test/locale/fallbacks_test.rb +124 -0
  48. data/test/locale/tag/rfc4646_test.rb +142 -0
  49. data/test/locale/tag/simple_test.rb +32 -0
  50. data/test/run_all.rb +21 -0
  51. data/test/test_data/locales/de.po +72 -0
  52. data/test/test_data/locales/en.rb +3 -0
  53. data/test/test_data/locales/en.yml +3 -0
  54. data/test/test_data/locales/invalid/empty.yml +0 -0
  55. data/test/test_data/locales/plurals.rb +113 -0
  56. data/test/test_helper.rb +56 -0
  57. metadata +64 -48
  58. 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,120 @@
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 => exception
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
+ exception = I18n::MissingTranslationData.new(:de, :foo, :scope => :bar)
32
+ assert_equal '<span class="translation_missing" title="translation missing: de.bar.foo">Foo</span>', exception.html_message
33
+ end
34
+
35
+ test "MissingTranslationData html_message html escapes key names" do
36
+ exception = I18n::MissingTranslationData.new(:de, '<script>Evil</script>', :scope => '<iframe src="example.com" />')
37
+ assert_equal '<span class="translation_missing" title="translation missing: de.&lt;iframe src=&quot;example.com&quot; /&gt;.&lt;script&gt;Evil&lt;/script&gt;">&lt;Script&gt;Evil&lt;/Script&gt;</span>', exception.html_message
38
+ end
39
+
40
+ test "ExceptionHandler returns the html_message if :rescue_format => :html was given" do
41
+ message = force_missing_translation_data(:rescue_format => :html)
42
+ assert_equal '<span class="translation_missing" title="translation missing: de.bar.foo">Foo</span>', message
43
+ end
44
+
45
+ test "InvalidPluralizationData stores entry and count" do
46
+ force_invalid_pluralization_data do |exception|
47
+ assert_equal [:bar], exception.entry
48
+ assert_equal 1, exception.count
49
+ end
50
+ end
51
+
52
+ test "InvalidPluralizationData message contains count and data" do
53
+ force_invalid_pluralization_data do |exception|
54
+ assert_equal 'translation data [:bar] can not be used with :count => 1', exception.message
55
+ end
56
+ end
57
+
58
+ test "MissingInterpolationArgument stores key and string" do
59
+ assert_raise(I18n::MissingInterpolationArgument) { force_missing_interpolation_argument }
60
+ force_missing_interpolation_argument do |exception|
61
+ # assert_equal :bar, exception.key
62
+ assert_equal "%{bar}", exception.string
63
+ end
64
+ end
65
+
66
+ test "MissingInterpolationArgument message contains the missing and given arguments" do
67
+ force_missing_interpolation_argument do |exception|
68
+ assert_equal 'missing interpolation argument in "%{bar}" ({:baz=>"baz"} given)', exception.message
69
+ end
70
+ end
71
+
72
+ test "ReservedInterpolationKey stores key and string" do
73
+ force_reserved_interpolation_key do |exception|
74
+ assert_equal :scope, exception.key
75
+ assert_equal "%{scope}", exception.string
76
+ end
77
+ end
78
+
79
+ test "ReservedInterpolationKey message contains the reserved key" do
80
+ force_reserved_interpolation_key do |exception|
81
+ assert_equal 'reserved key :scope used in "%{scope}"', exception.message
82
+ end
83
+ end
84
+
85
+ private
86
+
87
+ def force_invalid_locale
88
+ I18n.translate(:foo, :locale => nil)
89
+ rescue I18n::ArgumentError => e
90
+ block_given? ? yield(e) : raise(e)
91
+ end
92
+
93
+ def force_missing_translation_data(options = {})
94
+ I18n.backend.store_translations('de', :bar => nil)
95
+ I18n.translate(:foo, options.merge(:scope => :bar, :locale => :de))
96
+ rescue I18n::ArgumentError => e
97
+ block_given? ? yield(e) : raise(e)
98
+ end
99
+
100
+ def force_invalid_pluralization_data
101
+ I18n.backend.store_translations('de', :foo => [:bar])
102
+ I18n.translate(:foo, :count => 1, :locale => :de)
103
+ rescue I18n::ArgumentError => e
104
+ block_given? ? yield(e) : raise(e)
105
+ end
106
+
107
+ def force_missing_interpolation_argument
108
+ I18n.backend.store_translations('de', :foo => "%{bar}")
109
+ I18n.translate(:foo, :baz => 'baz', :locale => :de)
110
+ rescue I18n::ArgumentError => e
111
+ block_given? ? yield(e) : raise(e)
112
+ end
113
+
114
+ def force_reserved_interpolation_key
115
+ I18n.backend.store_translations('de', :foo => "%{scope}")
116
+ I18n.translate(:foo, :baz => 'baz', :locale => :de)
117
+ rescue I18n::ArgumentError => e
118
+ block_given? ? yield(e) : raise(e)
119
+ end
120
+ end
@@ -0,0 +1,61 @@
1
+ require 'test_helper'
2
+ require 'i18n/core_ext/string/interpolate'
3
+
4
+ # thanks to Masao's String extensions, some tests taken from Masao's tests
5
+ # http://github.com/mutoh/gettext/blob/edbbe1fa8238fa12c7f26f2418403015f0270e47/test/test_string.rb
6
+
7
+ class I18nInterpolateTest < Test::Unit::TestCase
8
+ test "String interpolates a hash argument w/ named placeholders" do
9
+ assert_equal "Masao Mutoh", I18n.interpolate("%{first} %{last}", :first => 'Masao', :last => 'Mutoh' )
10
+ end
11
+
12
+ test "String interpolates a hash argument w/ named placeholders (reverse order)" do
13
+ assert_equal "Mutoh, Masao", I18n.interpolate("%{last}, %{first}", :first => 'Masao', :last => 'Mutoh' )
14
+ end
15
+
16
+ test "String interpolates named placeholders with sprintf syntax" do
17
+ assert_equal "10, 43.4", I18n.interpolate("%<integer>d, %<float>.1f", :integer => 10, :float => 43.4)
18
+ end
19
+
20
+ test "String interpolates named placeholders with sprintf syntax, does not recurse" do
21
+ assert_equal "%<not_translated>s", I18n.interpolate("%{msg}", :msg => '%<not_translated>s', :not_translated => 'should not happen' )
22
+ end
23
+
24
+ test "String interpolation does not replace anything when no placeholders are given" do
25
+ assert_equal "aaa", I18n.interpolate("aaa", :num => 1)
26
+ end
27
+
28
+ test "String interpolation sprintf behaviour equals Ruby 1.9 behaviour" do
29
+ assert_equal "1", I18n.interpolate("%<num>d", :num => 1)
30
+ assert_equal "0b1", I18n.interpolate("%<num>#b", :num => 1)
31
+ assert_equal "foo", I18n.interpolate("%<msg>s", :msg => "foo")
32
+ assert_equal "1.000000", I18n.interpolate("%<num>f", :num => 1.0)
33
+ assert_equal " 1", I18n.interpolate("%<num>3.0f", :num => 1.0)
34
+ assert_equal "100.00", I18n.interpolate("%<num>2.2f", :num => 100.0)
35
+ assert_equal "0x64", I18n.interpolate("%<num>#x", :num => 100.0)
36
+ assert_raise(ArgumentError) { I18n.interpolate("%<num>,d", :num => 100) }
37
+ assert_raise(ArgumentError) { I18n.interpolate("%<num>/d", :num => 100) }
38
+ end
39
+
40
+ test "String interpolation raises an I18n::MissingInterpolationArgument when the string has extra placeholders" do
41
+ assert_raise(I18n::MissingInterpolationArgument) do # Ruby 1.9 msg: "key not found"
42
+ I18n.interpolate("%{first} %{last}", :first => 'Masao')
43
+ end
44
+ end
45
+
46
+ test "String interpolation does not raise when extra values were passed" do
47
+ assert_nothing_raised do
48
+ assert_equal "Masao Mutoh", I18n.interpolate("%{first} %{last}", :first => 'Masao', :last => 'Mutoh', :salutation => 'Mr.' )
49
+ end
50
+ end
51
+
52
+ test "% acts as escape character in String interpolation" do
53
+ assert_equal "%{first}", I18n.interpolate("%%{first}", :first => 'Masao')
54
+ assert_equal "% 1", I18n.interpolate("%% %<num>d", :num => 1.0)
55
+ assert_equal "%{num} %<num>d", I18n.interpolate("%%{num} %%<num>d", :num => 1)
56
+ end
57
+
58
+ def test_sprintf_mix_unformatted_and_formatted_named_placeholders
59
+ assert_equal "foo 1.000000", I18n.interpolate("%{name} %<num>f", :name => "foo", :num => 1.0)
60
+ end
61
+ 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,323 @@
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 "translate given an unavailable locale rases an I18n::InvalidLocale" do
39
+ begin
40
+ I18n.config.enforce_available_locales = true
41
+ assert_raise(I18n::InvalidLocale) { I18n.t(:foo, :locale => 'klingon') }
42
+ ensure
43
+ I18n.config.enforce_available_locales = false
44
+ end
45
+ end
46
+
47
+ test "raises an I18n::InvalidLocale exception when setting an unavailable default locale" do
48
+ begin
49
+ I18n.config.enforce_available_locales = true
50
+ assert_raise(I18n::InvalidLocale) { I18n.default_locale = :klingon }
51
+ ensure
52
+ I18n.config.enforce_available_locales = false
53
+ end
54
+ end
55
+
56
+ test "uses the default locale as a locale by default" do
57
+ assert_equal I18n.default_locale, I18n.locale
58
+ end
59
+
60
+ test "sets the current locale to Thread.current" do
61
+ assert_nothing_raised { I18n.locale = 'de' }
62
+ assert_equal :de, I18n.locale
63
+ assert_equal :de, Thread.current[:i18n_config].locale
64
+ I18n.locale = :en
65
+ end
66
+
67
+ test "raises an I18n::InvalidLocale exception when setting an unavailable locale" do
68
+ begin
69
+ I18n.config.enforce_available_locales = true
70
+ assert_raise(I18n::InvalidLocale) { I18n.locale = :klingon }
71
+ ensure
72
+ I18n.config.enforce_available_locales = false
73
+ end
74
+ end
75
+
76
+ test "can set the configuration object" do
77
+ begin
78
+ I18n.config = self
79
+ assert_equal self, I18n.config
80
+ assert_equal self, Thread.current[:i18n_config]
81
+ ensure
82
+ I18n.config = ::I18n::Config.new
83
+ end
84
+ end
85
+
86
+ test "locale is not shared between configurations" do
87
+ a = I18n::Config.new
88
+ b = I18n::Config.new
89
+ a.locale = :fr
90
+ b.locale = :es
91
+ assert_equal :fr, a.locale
92
+ assert_equal :es, b.locale
93
+ assert_equal :en, I18n.locale
94
+ end
95
+
96
+ test "other options are shared between configurations" do
97
+ begin
98
+ a = I18n::Config.new
99
+ b = I18n::Config.new
100
+ a.default_locale = :fr
101
+ b.default_locale = :es
102
+ assert_equal :es, a.default_locale
103
+ assert_equal :es, b.default_locale
104
+ assert_equal :es, I18n.default_locale
105
+ ensure
106
+ I18n.default_locale = :en
107
+ end
108
+ end
109
+
110
+ test "uses a dot as a default_separator by default" do
111
+ assert_equal '.', I18n.default_separator
112
+ end
113
+
114
+ test "can set the default_separator" do
115
+ begin
116
+ assert_nothing_raised { I18n.default_separator = "\001" }
117
+ ensure
118
+ I18n.default_separator = '.'
119
+ end
120
+ end
121
+
122
+ test "normalize_keys normalizes given locale, keys and scope to an array of single-key symbols" do
123
+ assert_equal [:en, :foo, :bar], I18n.normalize_keys(:en, :bar, :foo)
124
+ assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, :'baz.buz', :'foo.bar')
125
+ assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, 'baz.buz', 'foo.bar')
126
+ assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, %w(baz buz), %w(foo bar))
127
+ assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, [:baz, :buz], [:foo, :bar])
128
+ end
129
+
130
+ test "normalize_keys discards empty keys" do
131
+ assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, :'baz..buz', :'foo..bar')
132
+ assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, :'baz......buz', :'foo......bar')
133
+ assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, ['baz', nil, '', 'buz'], ['foo', nil, '', 'bar'])
134
+ end
135
+
136
+ test "normalize_keys uses a given separator" do
137
+ assert_equal [:en, :foo, :bar, :baz, :buz], I18n.normalize_keys(:en, :'baz|buz', :'foo|bar', '|')
138
+ end
139
+
140
+ test "can set the exception_handler" do
141
+ begin
142
+ previous_exception_handler = I18n.exception_handler
143
+ assert_nothing_raised { I18n.exception_handler = :custom_exception_handler }
144
+ ensure
145
+ I18n.exception_handler = previous_exception_handler
146
+ end
147
+ end
148
+
149
+ test "uses a custom exception handler set to I18n.exception_handler" do
150
+ begin
151
+ previous_exception_handler = I18n.exception_handler
152
+ I18n.exception_handler = :custom_exception_handler
153
+ I18n.expects(:custom_exception_handler)
154
+ I18n.translate :bogus
155
+ ensure
156
+ I18n.exception_handler = previous_exception_handler
157
+ end
158
+ end
159
+
160
+ test "uses a custom exception handler passed as an option" do
161
+ I18n.expects(:custom_exception_handler)
162
+ I18n.translate(:bogus, :exception_handler => :custom_exception_handler)
163
+ end
164
+
165
+ test "delegates translate calls to the backend" do
166
+ I18n.backend.expects(:translate).with('de', :foo, {})
167
+ I18n.translate :foo, :locale => 'de'
168
+ end
169
+
170
+ test "delegates localize calls to the backend" do
171
+ I18n.backend.expects(:localize).with('de', :whatever, :default, {})
172
+ I18n.localize :whatever, :locale => 'de'
173
+ end
174
+
175
+ test "translate given no locale uses the current locale" do
176
+ I18n.backend.expects(:translate).with(:en, :foo, {})
177
+ I18n.translate :foo
178
+ end
179
+
180
+ test "translate works with nested symbol keys" do
181
+ assert_equal ".", I18n.t(:'currency.format.separator')
182
+ end
183
+
184
+ test "translate works with nested string keys" do
185
+ assert_equal ".", I18n.t('currency.format.separator')
186
+ end
187
+
188
+ test "translate with an array as a scope works" do
189
+ assert_equal ".", I18n.t(:separator, :scope => %w(currency format))
190
+ end
191
+
192
+ test "translate with an array containing dot separated strings as a scope works" do
193
+ assert_equal ".", I18n.t(:separator, :scope => ['currency.format'])
194
+ end
195
+
196
+ test "translate with an array of keys and a dot separated string as a scope works" do
197
+ assert_equal [".", ","], I18n.t(%w(separator delimiter), :scope => 'currency.format')
198
+ end
199
+
200
+ test "translate with an array of dot separated keys and a scope works" do
201
+ assert_equal [".", ","], I18n.t(%w(format.separator format.delimiter), :scope => 'currency')
202
+ end
203
+
204
+ # def test_translate_given_no_args_raises_missing_translation_data
205
+ # assert_equal "translation missing: en, no key", I18n.t
206
+ # end
207
+
208
+ test "translate given a bogus key returns an error message" do
209
+ assert_equal "translation missing: en.bogus", I18n.t(:bogus)
210
+ end
211
+
212
+ test "translate given an empty string as a key raises an I18n::ArgumentError" do
213
+ assert_raise(I18n::ArgumentError) { I18n.t("") }
214
+ end
215
+
216
+ test "localize given nil raises an I18n::ArgumentError" do
217
+ assert_raise(I18n::ArgumentError) { I18n.l nil }
218
+ end
219
+
220
+ test "localize givan an Object raises an I18n::ArgumentError" do
221
+ assert_raise(I18n::ArgumentError) { I18n.l Object.new }
222
+ end
223
+
224
+ test "localize given an unavailable locale rases an I18n::InvalidLocale" do
225
+ begin
226
+ I18n.config.enforce_available_locales = true
227
+ assert_raise(I18n::InvalidLocale) { I18n.l(Time.now, :locale => 'klingon') }
228
+ ensure
229
+ I18n.config.enforce_available_locales = false
230
+ end
231
+ end
232
+
233
+ test "can use a lambda as an exception handler" do
234
+ begin
235
+ previous_exception_handler = I18n.exception_handler
236
+ I18n.exception_handler = Proc.new { |exception, locale, key, options| exception }
237
+ assert_equal I18n::MissingTranslationData, I18n.translate(:test_proc_handler).class
238
+ ensure
239
+ I18n.exception_handler = previous_exception_handler
240
+ end
241
+ end
242
+
243
+ test "transliterate given an unavailable locale rases an I18n::InvalidLocale" do
244
+ begin
245
+ I18n.config.enforce_available_locales = true
246
+ assert_raise(I18n::InvalidLocale) { I18n.transliterate('string', :locale => 'klingon') }
247
+ ensure
248
+ I18n.config.enforce_available_locales = false
249
+ end
250
+ end
251
+
252
+ test "I18n.locale_available? returns true when the passed locale is available" do
253
+ I18n.available_locales = [:en, :de]
254
+ assert_equal true, I18n.locale_available?(:de)
255
+ end
256
+
257
+ test "I18n.locale_available? returns true when the passed locale is a string and is available" do
258
+ I18n.available_locales = [:en, :de]
259
+ assert_equal true, I18n.locale_available?('de')
260
+ end
261
+
262
+ test "I18n.locale_available? returns false when the passed locale is unavailable" do
263
+ assert_equal false, I18n.locale_available?(:klingon)
264
+ end
265
+
266
+ test "I18n.enforce_available_locales! raises an I18n::InvalidLocale when the passed locale is unavailable" do
267
+ begin
268
+ I18n.config.enforce_available_locales = true
269
+ assert_raise(I18n::InvalidLocale) { I18n.enforce_available_locales!(:klingon) }
270
+ ensure
271
+ I18n.config.enforce_available_locales = false
272
+ end
273
+ end
274
+
275
+ test "I18n.enforce_available_locales! does nothing when the passed locale is available" do
276
+ I18n.available_locales = [:en, :de]
277
+ begin
278
+ I18n.config.enforce_available_locales = true
279
+ assert_nothing_raised { I18n.enforce_available_locales!(:en) }
280
+ ensure
281
+ I18n.config.enforce_available_locales = false
282
+ end
283
+ end
284
+
285
+ test "can use an object responding to #call as an exception handler" do
286
+ begin
287
+ previous_exception_handler = I18n.exception_handler
288
+ I18n.exception_handler = Class.new do
289
+ def call(exception, locale, key, options); exception; end
290
+ end.new
291
+ assert_equal I18n::MissingTranslationData, I18n.translate(:test_proc_handler).class
292
+ ensure
293
+ I18n.exception_handler = previous_exception_handler
294
+ end
295
+ end
296
+
297
+ test "I18n.with_locale temporarily sets the given locale" do
298
+ store_translations(:en, :foo => 'Foo in :en')
299
+ store_translations(:de, :foo => 'Foo in :de')
300
+ store_translations(:pl, :foo => 'Foo in :pl')
301
+
302
+ I18n.with_locale { assert_equal [:en, 'Foo in :en'], [I18n.locale, I18n.t(:foo)] }
303
+ I18n.with_locale(:de) { assert_equal [:de, 'Foo in :de'], [I18n.locale, I18n.t(:foo)] }
304
+ I18n.with_locale(:pl) { assert_equal [:pl, 'Foo in :pl'], [I18n.locale, I18n.t(:foo)] }
305
+ I18n.with_locale(:en) { assert_equal [:en, 'Foo in :en'], [I18n.locale, I18n.t(:foo)] }
306
+
307
+ assert_equal I18n.default_locale, I18n.locale
308
+ end
309
+
310
+ test "I18n.with_locale resets the locale in case of errors" do
311
+ assert_raise(I18n::ArgumentError) { I18n.with_locale(:pl) { raise I18n::ArgumentError } }
312
+ assert_equal I18n.default_locale, I18n.locale
313
+ end
314
+
315
+ test "I18n.enforce_available_locales config can be set to false" do
316
+ begin
317
+ I18n.config.enforce_available_locales = false
318
+ assert_equal false, I18n.config.enforce_available_locales
319
+ ensure
320
+ I18n.config.enforce_available_locales = false
321
+ end
322
+ end
323
+ end