everythingbehind-i18n 0.1.1

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,53 @@
1
+ module I18n
2
+ class ArgumentError < ::ArgumentError; end
3
+
4
+ class InvalidLocale < ArgumentError
5
+ attr_reader :locale
6
+ def initialize(locale)
7
+ @locale = locale
8
+ super "#{locale.inspect} is not a valid locale"
9
+ end
10
+ end
11
+
12
+ class MissingTranslationData < ArgumentError
13
+ attr_reader :locale, :key, :options
14
+ def initialize(locale, key, options)
15
+ @key, @locale, @options = key, locale, options
16
+ keys = I18n.send(:normalize_translation_keys, locale, key, options[:scope])
17
+ keys << 'no key' if keys.size < 2
18
+ super "translation missing: #{keys.join(', ')}"
19
+ end
20
+ end
21
+
22
+ class InvalidPluralizationData < ArgumentError
23
+ attr_reader :entry, :count
24
+ def initialize(entry, count)
25
+ @entry, @count = entry, count
26
+ super "translation data #{entry.inspect} can not be used with :count => #{count}"
27
+ end
28
+ end
29
+
30
+ class MissingInterpolationArgument < ArgumentError
31
+ attr_reader :key, :string
32
+ def initialize(key, string)
33
+ @key, @string = key, string
34
+ super "interpolation argument #{key} missing in #{string.inspect}"
35
+ end
36
+ end
37
+
38
+ class ReservedInterpolationKey < ArgumentError
39
+ attr_reader :key, :string
40
+ def initialize(key, string)
41
+ @key, @string = key, string
42
+ super "reserved key #{key.inspect} used in #{string.inspect}"
43
+ end
44
+ end
45
+
46
+ class UnknownFileType < ArgumentError
47
+ attr_reader :type, :filename
48
+ def initialize(type, filename)
49
+ @type, @filename = type, filename
50
+ super "can not load translations from #{filename}, the file type #{type} is not known"
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,20 @@
1
+ module I18n
2
+ module TestHelper
3
+ def assert_all_locales_have_translations_available_to_the_default_locale(message = "All translations should be available in all locales")
4
+ default_locale = I18n.default_locale.to_sym
5
+ locales_to_check = I18n.locales - [default_locale]
6
+
7
+ required_translations = I18n.available_translations(default_locale)
8
+
9
+ locales_to_check.each do |target_locale|
10
+ defined_translations = I18n.available_translations(target_locale.to_sym)
11
+ missing_translations = required_translations - defined_translations
12
+
13
+ if missing_translations.any?
14
+ missing_translations_for_output = [*missing_translations.map{|a| " * #{a.join('.')}" }].join("\n")
15
+ raise Test::Unit::AssertionFailedError.new("#{message} - Missing translations for #{target_locale.inspect}:\n#{missing_translations_for_output}")
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
data/test/all.rb ADDED
@@ -0,0 +1,6 @@
1
+ dir = File.dirname(__FILE__)
2
+ require dir + '/i18n_test.rb'
3
+ require dir + '/simple_backend_test.rb'
4
+ require dir + '/i18n_exceptions_test.rb'
5
+ require dir + '/i18n_test_helper_test.rb'
6
+ # *require* dir + '/custom_backend_test.rb'
@@ -0,0 +1,100 @@
1
+ $:.unshift "lib"
2
+
3
+ require 'rubygems'
4
+ require 'test/unit'
5
+ require 'mocha'
6
+ require 'i18n'
7
+ require 'active_support'
8
+
9
+ class I18nExceptionsTest < Test::Unit::TestCase
10
+ def test_invalid_locale_stores_locale
11
+ force_invalid_locale
12
+ rescue I18n::ArgumentError => e
13
+ assert_nil e.locale
14
+ end
15
+
16
+ def test_invalid_locale_message
17
+ force_invalid_locale
18
+ rescue I18n::ArgumentError => e
19
+ assert_equal 'nil is not a valid locale', e.message
20
+ end
21
+
22
+ def test_missing_translation_data_stores_locale_key_and_options
23
+ force_missing_translation_data
24
+ rescue I18n::ArgumentError => e
25
+ options = {:scope => :bar}
26
+ assert_equal 'de', e.locale
27
+ assert_equal :foo, e.key
28
+ assert_equal options, e.options
29
+ end
30
+
31
+ def test_missing_translation_data_message
32
+ force_missing_translation_data
33
+ rescue I18n::ArgumentError => e
34
+ assert_equal 'translation missing: de, bar, foo', e.message
35
+ end
36
+
37
+ def test_invalid_pluralization_data_stores_entry_and_count
38
+ force_invalid_pluralization_data
39
+ rescue I18n::ArgumentError => e
40
+ assert_equal [:bar], e.entry
41
+ assert_equal 1, e.count
42
+ end
43
+
44
+ def test_invalid_pluralization_data_message
45
+ force_invalid_pluralization_data
46
+ rescue I18n::ArgumentError => e
47
+ assert_equal 'translation data [:bar] can not be used with :count => 1', e.message
48
+ end
49
+
50
+ def test_missing_interpolation_argument_stores_key_and_string
51
+ force_missing_interpolation_argument
52
+ rescue I18n::ArgumentError => e
53
+ assert_equal 'bar', e.key
54
+ assert_equal "{{bar}}", e.string
55
+ end
56
+
57
+ def test_missing_interpolation_argument_message
58
+ force_missing_interpolation_argument
59
+ rescue I18n::ArgumentError => e
60
+ assert_equal 'interpolation argument bar missing in "{{bar}}"', e.message
61
+ end
62
+
63
+ def test_reserved_interpolation_key_stores_key_and_string
64
+ force_reserved_interpolation_key
65
+ rescue I18n::ArgumentError => e
66
+ assert_equal 'scope', e.key
67
+ assert_equal "{{scope}}", e.string
68
+ end
69
+
70
+ def test_reserved_interpolation_key_message
71
+ force_reserved_interpolation_key
72
+ rescue I18n::ArgumentError => e
73
+ assert_equal 'reserved key "scope" used in "{{scope}}"', e.message
74
+ end
75
+
76
+ private
77
+ def force_invalid_locale
78
+ I18n.backend.translate nil, :foo
79
+ end
80
+
81
+ def force_missing_translation_data
82
+ I18n.backend.store_translations 'de', :bar => nil
83
+ I18n.backend.translate 'de', :foo, :scope => :bar
84
+ end
85
+
86
+ def force_invalid_pluralization_data
87
+ I18n.backend.store_translations 'de', :foo => [:bar]
88
+ I18n.backend.translate 'de', :foo, :count => 1
89
+ end
90
+
91
+ def force_missing_interpolation_argument
92
+ I18n.backend.store_translations 'de', :foo => "{{bar}}"
93
+ I18n.backend.translate 'de', :foo, :baz => 'baz'
94
+ end
95
+
96
+ def force_reserved_interpolation_key
97
+ I18n.backend.store_translations 'de', :foo => "{{scope}}"
98
+ I18n.backend.translate 'de', :foo, :baz => 'baz'
99
+ end
100
+ end
data/test/i18n_test.rb ADDED
@@ -0,0 +1,130 @@
1
+ $:.unshift "lib"
2
+
3
+ require 'rubygems'
4
+ require 'test/unit'
5
+ require 'mocha'
6
+ require 'i18n'
7
+ require 'active_support'
8
+
9
+ class I18nTest < Test::Unit::TestCase
10
+ def setup
11
+ I18n.backend.store_translations :'en', {
12
+ :currency => {
13
+ :format => {
14
+ :separator => '.',
15
+ :delimiter => ',',
16
+ }
17
+ }
18
+ }
19
+ end
20
+
21
+ def test_uses_simple_backend_set_by_default
22
+ assert I18n.backend.is_a?(I18n::Backend::Simple)
23
+ end
24
+
25
+ def test_can_set_backend
26
+ assert_nothing_raised{ I18n.backend = self }
27
+ assert_equal self, I18n.backend
28
+ I18n.backend = I18n::Backend::Simple.new
29
+ end
30
+
31
+ def test_uses_en_us_as_default_locale_by_default
32
+ assert_equal 'en', I18n.default_locale
33
+ end
34
+
35
+ def test_can_set_default_locale
36
+ assert_nothing_raised{ I18n.default_locale = 'de' }
37
+ assert_equal 'de', I18n.default_locale
38
+ I18n.default_locale = 'en'
39
+ end
40
+
41
+ def test_uses_default_locale_as_locale_by_default
42
+ assert_equal I18n.default_locale, I18n.locale
43
+ end
44
+
45
+ def test_can_set_locale_to_thread_current
46
+ assert_nothing_raised{ I18n.locale = 'de' }
47
+ assert_equal 'de', I18n.locale
48
+ assert_equal 'de', Thread.current[:locale]
49
+ I18n.locale = 'en'
50
+ end
51
+
52
+ def test_can_set_exception_handler
53
+ assert_nothing_raised{ I18n.exception_handler = :custom_exception_handler }
54
+ I18n.exception_handler = :default_exception_handler # revert it
55
+ end
56
+
57
+ def test_uses_custom_exception_handler
58
+ I18n.exception_handler = :custom_exception_handler
59
+ I18n.expects(:custom_exception_handler)
60
+ I18n.translate :bogus
61
+ I18n.exception_handler = :default_exception_handler # revert it
62
+ end
63
+
64
+ def test_delegates_locales_to_backend
65
+ I18n.backend.expects(:locales)
66
+ I18n.locales
67
+ end
68
+
69
+ def test_delegates_translate_to_backend
70
+ I18n.backend.expects(:translate).with 'de', :foo, {}
71
+ I18n.translate :foo, :locale => 'de'
72
+ end
73
+
74
+ def test_delegates_localize_to_backend
75
+ I18n.backend.expects(:localize).with 'de', :whatever, :default
76
+ I18n.localize :whatever, :locale => 'de'
77
+ end
78
+
79
+ def test_translate_given_no_locale_uses_i18n_locale
80
+ I18n.backend.expects(:translate).with 'en', :foo, {}
81
+ I18n.translate :foo
82
+ end
83
+
84
+ def test_translate_on_nested_symbol_keys_works
85
+ assert_equal ".", I18n.t(:'currency.format.separator')
86
+ end
87
+
88
+ def test_translate_with_nested_string_keys_works
89
+ assert_equal ".", I18n.t('currency.format.separator')
90
+ end
91
+
92
+ def test_translate_with_array_as_scope_works
93
+ assert_equal ".", I18n.t(:separator, :scope => ['currency.format'])
94
+ end
95
+
96
+ def test_translate_with_array_containing_dot_separated_strings_as_scope_works
97
+ assert_equal ".", I18n.t(:separator, :scope => ['currency.format'])
98
+ end
99
+
100
+ def test_translate_with_key_array_and_dot_separated_scope_works
101
+ assert_equal [".", ","], I18n.t(%w(separator delimiter), :scope => 'currency.format')
102
+ end
103
+
104
+ def test_translate_with_dot_separated_key_array_and_scope_works
105
+ assert_equal [".", ","], I18n.t(%w(format.separator format.delimiter), :scope => 'currency')
106
+ end
107
+
108
+ def test_translate_with_options_using_scope_works
109
+ I18n.backend.expects(:translate).with('de', :precision, :scope => :"currency.format")
110
+ I18n.with_options :locale => 'de', :scope => :'currency.format' do |locale|
111
+ locale.t :precision
112
+ end
113
+ end
114
+
115
+ # def test_translate_given_no_args_raises_missing_translation_data
116
+ # assert_equal "translation missing: en, no key", I18n.t
117
+ # end
118
+
119
+ def test_translate_given_a_bogus_key_raises_missing_translation_data
120
+ assert_equal "translation missing: en, bogus", I18n.t(:bogus)
121
+ end
122
+
123
+ def test_localize_nil_raises_argument_error
124
+ assert_raises(I18n::ArgumentError) { I18n.l nil }
125
+ end
126
+
127
+ def test_localize_object_raises_argument_error
128
+ assert_raises(I18n::ArgumentError) { I18n.l Object.new }
129
+ end
130
+ end
@@ -0,0 +1,56 @@
1
+
2
+ class EqualLocalesTestHelperTest < Test::Unit::TestCase
3
+ include I18n::TestHelper
4
+
5
+ def setup
6
+ I18n.reload!
7
+
8
+ I18n.backend.store_translations :'en', {
9
+ :foo => "Bar",
10
+ :nested => {
11
+ :translation => {
12
+ :should => "Work"
13
+ }
14
+ }
15
+ }
16
+
17
+ I18n.backend.store_translations :'pirate', {
18
+ :nested => {
19
+ :translation => {
20
+ :should => "Wark me hearties!"
21
+ }
22
+ },
23
+ :foo => "Barrrr!"
24
+ }
25
+ end
26
+
27
+ def test_should_not_raise_a_test_failure
28
+ assert_nothing_raised() { assert_all_locales_have_translations_available_to_the_default_locale }
29
+ end
30
+ end
31
+
32
+
33
+ class UnequalLocalesTestHelperTest < Test::Unit::TestCase
34
+ include I18n::TestHelper
35
+
36
+ def setup
37
+ I18n.reload!
38
+
39
+ I18n.backend.store_translations :'en', {
40
+ :bar => {
41
+ :foo => "Bar"
42
+ }
43
+ }
44
+
45
+ I18n.backend.store_translations :'pirate', {
46
+ :bar => {
47
+ }
48
+ }
49
+ end
50
+
51
+ def test_should_raise_a_test_failure_for_none_matching_nested_keys
52
+ e = assert_raise(Test::Unit::AssertionFailedError) { assert_all_locales_have_translations_available_to_the_default_locale }
53
+ assert_match %r/Missing translations for :pirate/, e.message
54
+ assert_match %r/ * bar.foo/, e.message
55
+ end
56
+ end
data/test/locale/en.rb ADDED
@@ -0,0 +1 @@
1
+ {:'en-Ruby' => {:foo => {:bar => "baz"}}}
@@ -0,0 +1,3 @@
1
+ en-Yaml:
2
+ foo:
3
+ bar: baz
@@ -0,0 +1,510 @@
1
+ # encoding: utf-8
2
+ $:.unshift "lib"
3
+
4
+ require 'rubygems'
5
+ require 'test/unit'
6
+ require 'mocha'
7
+ require 'i18n'
8
+ require 'time'
9
+ require 'yaml'
10
+
11
+ module I18nSimpleBackendTestSetup
12
+ def setup_backend
13
+ # backend_reset_translations!
14
+ @backend = I18n::Backend::Simple.new
15
+ @backend.store_translations 'en', :foo => {:bar => 'bar', :baz => 'baz'}
16
+ @locale_dir = File.dirname(__FILE__) + '/locale'
17
+ end
18
+ alias :setup :setup_backend
19
+
20
+ # def backend_reset_translations!
21
+ # I18n::Backend::Simple::ClassMethods.send :class_variable_set, :@@translations, {}
22
+ # end
23
+
24
+ def backend_get_translations
25
+ # I18n::Backend::Simple::ClassMethods.send :class_variable_get, :@@translations
26
+ @backend.instance_variable_get :@translations
27
+ end
28
+
29
+ def add_datetime_translations
30
+ @backend.store_translations :'de', {
31
+ :date => {
32
+ :formats => {
33
+ :default => "%d.%m.%Y",
34
+ :short => "%d. %b",
35
+ :long => "%d. %B %Y",
36
+ },
37
+ :day_names => %w(Sonntag Montag Dienstag Mittwoch Donnerstag Freitag Samstag),
38
+ :abbr_day_names => %w(So Mo Di Mi Do Fr Sa),
39
+ :month_names => %w(Januar Februar März April Mai Juni Juli August September Oktober November Dezember).unshift(nil),
40
+ :abbr_month_names => %w(Jan Feb Mar Apr Mai Jun Jul Aug Sep Okt Nov Dez).unshift(nil),
41
+ :order => [:day, :month, :year]
42
+ },
43
+ :time => {
44
+ :formats => {
45
+ :default => "%a, %d. %b %Y %H:%M:%S %z",
46
+ :short => "%d. %b %H:%M",
47
+ :long => "%d. %B %Y %H:%M",
48
+ },
49
+ :am => 'am',
50
+ :pm => 'pm'
51
+ },
52
+ :datetime => {
53
+ :distance_in_words => {
54
+ :half_a_minute => 'half a minute',
55
+ :less_than_x_seconds => {
56
+ :one => 'less than 1 second',
57
+ :other => 'less than {{count}} seconds'
58
+ },
59
+ :x_seconds => {
60
+ :one => '1 second',
61
+ :other => '{{count}} seconds'
62
+ },
63
+ :less_than_x_minutes => {
64
+ :one => 'less than a minute',
65
+ :other => 'less than {{count}} minutes'
66
+ },
67
+ :x_minutes => {
68
+ :one => '1 minute',
69
+ :other => '{{count}} minutes'
70
+ },
71
+ :about_x_hours => {
72
+ :one => 'about 1 hour',
73
+ :other => 'about {{count}} hours'
74
+ },
75
+ :x_days => {
76
+ :one => '1 day',
77
+ :other => '{{count}} days'
78
+ },
79
+ :about_x_months => {
80
+ :one => 'about 1 month',
81
+ :other => 'about {{count}} months'
82
+ },
83
+ :x_months => {
84
+ :one => '1 month',
85
+ :other => '{{count}} months'
86
+ },
87
+ :about_x_years => {
88
+ :one => 'about 1 year',
89
+ :other => 'about {{count}} year'
90
+ },
91
+ :over_x_years => {
92
+ :one => 'over 1 year',
93
+ :other => 'over {{count}} years'
94
+ }
95
+ }
96
+ }
97
+ }
98
+ end
99
+ end
100
+
101
+ class I18nSimpleBackendTranslationsTest < Test::Unit::TestCase
102
+ include I18nSimpleBackendTestSetup
103
+
104
+ def test_store_translations_adds_translations # no, really :-)
105
+ @backend.store_translations :'en', :foo => 'bar'
106
+ assert_equal Hash[:'en', {:foo => 'bar'}], backend_get_translations
107
+ end
108
+
109
+ def test_list_of_locales
110
+ @backend.store_translations :'en', :foo => {:bar => 'bar'}
111
+ @backend.store_translations :'fr', :foo => {:bar => 'baz'}
112
+ assert_equal 2, @backend.locales.length
113
+ assert @backend.locales.include?(:en)
114
+ assert @backend.locales.include?(:fr)
115
+ end
116
+
117
+ def test_store_translations_deep_merges_translations
118
+ @backend.store_translations :'en', :foo => {:bar => 'bar'}
119
+ @backend.store_translations :'en', :foo => {:baz => 'baz'}
120
+ assert_equal Hash[:'en', {:foo => {:bar => 'bar', :baz => 'baz'}}], backend_get_translations
121
+ end
122
+
123
+ def test_store_translations_forces_locale_to_sym
124
+ @backend.store_translations 'en', :foo => 'bar'
125
+ assert_equal Hash[:'en', {:foo => 'bar'}], backend_get_translations
126
+ end
127
+
128
+ def test_store_translations_converts_keys_to_symbols
129
+ # backend_reset_translations!
130
+ @backend.store_translations 'en', 'foo' => {'bar' => 'bar', 'baz' => 'baz'}
131
+ assert_equal Hash[:'en', {:foo => {:bar => 'bar', :baz => 'baz'}}], backend_get_translations
132
+ end
133
+ end
134
+
135
+ class I18nSimpleBackendTranslateTest < Test::Unit::TestCase
136
+ include I18nSimpleBackendTestSetup
137
+
138
+ def test_translate_calls_lookup_with_locale_given
139
+ @backend.expects(:lookup).with('de', :bar, [:foo]).returns 'bar'
140
+ @backend.translate 'de', :bar, :scope => [:foo]
141
+ end
142
+
143
+ def test_given_no_keys_it_returns_the_default
144
+ assert_equal 'default', @backend.translate('en', nil, :default => 'default')
145
+ end
146
+
147
+ def test_translate_given_a_symbol_as_a_default_translates_the_symbol
148
+ assert_equal 'bar', @backend.translate('en', nil, :scope => [:foo], :default => :bar)
149
+ end
150
+
151
+ def test_translate_given_an_array_as_default_uses_the_first_match
152
+ assert_equal 'bar', @backend.translate('en', :does_not_exist, :scope => [:foo], :default => [:does_not_exist_2, :bar])
153
+ end
154
+
155
+ def test_translate_given_an_array_of_inexistent_keys_it_raises_missing_translation_data
156
+ assert_raises I18n::MissingTranslationData do
157
+ @backend.translate('en', :does_not_exist, :scope => [:foo], :default => [:does_not_exist_2, :does_not_exist_3])
158
+ end
159
+ end
160
+
161
+ def test_translate_an_array_of_keys_translates_all_of_them
162
+ assert_equal %w(bar baz), @backend.translate('en', [:bar, :baz], :scope => [:foo])
163
+ end
164
+
165
+ def test_translate_calls_pluralize
166
+ @backend.expects(:pluralize).with 'en', 'bar', 1
167
+ @backend.translate 'en', :bar, :scope => [:foo], :count => 1
168
+ end
169
+
170
+ def test_translate_calls_interpolate
171
+ @backend.expects(:interpolate).with 'en', 'bar', {}
172
+ @backend.translate 'en', :bar, :scope => [:foo]
173
+ end
174
+
175
+ def test_translate_calls_interpolate_including_count_as_a_value
176
+ @backend.expects(:interpolate).with 'en', 'bar', {:count => 1}
177
+ @backend.translate 'en', :bar, :scope => [:foo], :count => 1
178
+ end
179
+
180
+ def test_translate_given_nil_as_a_locale_raises_an_argument_error
181
+ assert_raises(I18n::InvalidLocale){ @backend.translate nil, :bar }
182
+ end
183
+
184
+ def test_translate_with_a_bogus_key_and_no_default_raises_missing_translation_data
185
+ assert_raises(I18n::MissingTranslationData){ @backend.translate 'de', :bogus }
186
+ end
187
+ end
188
+
189
+ class I18nSimpleBackendLookupTest < Test::Unit::TestCase
190
+ include I18nSimpleBackendTestSetup
191
+
192
+ # useful because this way we can use the backend with no key for interpolation/pluralization
193
+ def test_lookup_given_nil_as_a_key_returns_nil
194
+ assert_nil @backend.send(:lookup, 'en', nil)
195
+ end
196
+
197
+ def test_lookup_given_nested_keys_looks_up_a_nested_hash_value
198
+ assert_equal 'bar', @backend.send(:lookup, 'en', :bar, [:foo])
199
+ end
200
+ end
201
+
202
+ class I18nSimpleBackendPluralizeTest < Test::Unit::TestCase
203
+ include I18nSimpleBackendTestSetup
204
+
205
+ def test_pluralize_given_nil_returns_the_given_entry
206
+ entry = {:one => 'bar', :other => 'bars'}
207
+ assert_equal entry, @backend.send(:pluralize, nil, entry, nil)
208
+ end
209
+
210
+ def test_pluralize_given_0_returns_zero_string_if_zero_key_given
211
+ assert_equal 'zero', @backend.send(:pluralize, nil, {:zero => 'zero', :one => 'bar', :other => 'bars'}, 0)
212
+ end
213
+
214
+ def test_pluralize_given_0_returns_plural_string_if_no_zero_key_given
215
+ assert_equal 'bars', @backend.send(:pluralize, nil, {:one => 'bar', :other => 'bars'}, 0)
216
+ end
217
+
218
+ def test_pluralize_given_1_returns_singular_string
219
+ assert_equal 'bar', @backend.send(:pluralize, nil, {:one => 'bar', :other => 'bars'}, 1)
220
+ end
221
+
222
+ def test_pluralize_given_2_returns_plural_string
223
+ assert_equal 'bars', @backend.send(:pluralize, nil, {:one => 'bar', :other => 'bars'}, 2)
224
+ end
225
+
226
+ def test_pluralize_given_3_returns_plural_string
227
+ assert_equal 'bars', @backend.send(:pluralize, nil, {:one => 'bar', :other => 'bars'}, 3)
228
+ end
229
+
230
+ def test_interpolate_given_incomplete_pluralization_data_raises_invalid_pluralization_data
231
+ assert_raises(I18n::InvalidPluralizationData){ @backend.send(:pluralize, nil, {:one => 'bar'}, 2) }
232
+ end
233
+
234
+ # def test_interpolate_given_a_string_raises_invalid_pluralization_data
235
+ # assert_raises(I18n::InvalidPluralizationData){ @backend.send(:pluralize, nil, 'bar', 2) }
236
+ # end
237
+ #
238
+ # def test_interpolate_given_an_array_raises_invalid_pluralization_data
239
+ # assert_raises(I18n::InvalidPluralizationData){ @backend.send(:pluralize, nil, ['bar'], 2) }
240
+ # end
241
+ end
242
+
243
+ class I18nSimpleBackendInterpolateTest < Test::Unit::TestCase
244
+ include I18nSimpleBackendTestSetup
245
+
246
+ def test_interpolate_given_a_value_hash_interpolates_the_values_to_the_string
247
+ assert_equal 'Hi David!', @backend.send(:interpolate, nil, 'Hi {{name}}!', :name => 'David')
248
+ end
249
+
250
+ def test_interpolate_given_a_value_hash_interpolates_into_unicode_string
251
+ assert_equal 'Häi David!', @backend.send(:interpolate, nil, 'Häi {{name}}!', :name => 'David')
252
+ end
253
+
254
+ def test_interpolate_given_nil_as_a_string_returns_nil
255
+ assert_nil @backend.send(:interpolate, nil, nil, :name => 'David')
256
+ end
257
+
258
+ def test_interpolate_given_an_non_string_as_a_string_returns_nil
259
+ assert_equal [], @backend.send(:interpolate, nil, [], :name => 'David')
260
+ end
261
+
262
+ def test_interpolate_given_a_values_hash_with_nil_values_interpolates_the_string
263
+ assert_equal 'Hi !', @backend.send(:interpolate, nil, 'Hi {{name}}!', {:name => nil})
264
+ end
265
+
266
+ def test_interpolate_given_an_empty_values_hash_raises_missing_interpolation_argument
267
+ assert_raises(I18n::MissingInterpolationArgument) { @backend.send(:interpolate, nil, 'Hi {{name}}!', {}) }
268
+ end
269
+
270
+ def test_interpolate_given_a_string_containing_a_reserved_key_raises_reserved_interpolation_key
271
+ assert_raises(I18n::ReservedInterpolationKey) { @backend.send(:interpolate, nil, '{{default}}', {:default => nil}) }
272
+ end
273
+ end
274
+
275
+ class I18nSimpleBackendLocalizeDateTest < Test::Unit::TestCase
276
+ include I18nSimpleBackendTestSetup
277
+
278
+ def setup
279
+ @backend = I18n::Backend::Simple.new
280
+ add_datetime_translations
281
+ @date = Date.new 2008, 1, 1
282
+ end
283
+
284
+ def test_translate_given_the_short_format_it_uses_it
285
+ assert_equal '01. Jan', @backend.localize('de', @date, :short)
286
+ end
287
+
288
+ def test_translate_given_the_long_format_it_uses_it
289
+ assert_equal '01. Januar 2008', @backend.localize('de', @date, :long)
290
+ end
291
+
292
+ def test_translate_given_the_default_format_it_uses_it
293
+ assert_equal '01.01.2008', @backend.localize('de', @date, :default)
294
+ end
295
+
296
+ def test_translate_given_a_day_name_format_it_returns_a_day_name
297
+ assert_equal 'Dienstag', @backend.localize('de', @date, '%A')
298
+ end
299
+
300
+ def test_translate_given_an_abbr_day_name_format_it_returns_an_abbrevated_day_name
301
+ assert_equal 'Di', @backend.localize('de', @date, '%a')
302
+ end
303
+
304
+ def test_translate_given_a_month_name_format_it_returns_a_month_name
305
+ assert_equal 'Januar', @backend.localize('de', @date, '%B')
306
+ end
307
+
308
+ def test_translate_given_an_abbr_month_name_format_it_returns_an_abbrevated_month_name
309
+ assert_equal 'Jan', @backend.localize('de', @date, '%b')
310
+ end
311
+
312
+ def test_translate_given_no_format_it_does_not_fail
313
+ assert_nothing_raised{ @backend.localize 'de', @date }
314
+ end
315
+
316
+ def test_translate_given_an_unknown_format_it_does_not_fail
317
+ assert_nothing_raised{ @backend.localize 'de', @date, '%x' }
318
+ end
319
+
320
+ def test_localize_nil_raises_argument_error
321
+ assert_raises(I18n::ArgumentError) { @backend.localize 'de', nil }
322
+ end
323
+
324
+ def test_localize_object_raises_argument_error
325
+ assert_raises(I18n::ArgumentError) { @backend.localize 'de', Object.new }
326
+ end
327
+ end
328
+
329
+ class I18nSimpleBackendLocalizeDateTimeTest < Test::Unit::TestCase
330
+ include I18nSimpleBackendTestSetup
331
+
332
+ def setup
333
+ @backend = I18n::Backend::Simple.new
334
+ add_datetime_translations
335
+ @morning = DateTime.new 2008, 1, 1, 6
336
+ @evening = DateTime.new 2008, 1, 1, 18
337
+ end
338
+
339
+ def test_translate_given_the_short_format_it_uses_it
340
+ assert_equal '01. Jan 06:00', @backend.localize('de', @morning, :short)
341
+ end
342
+
343
+ def test_translate_given_the_long_format_it_uses_it
344
+ assert_equal '01. Januar 2008 06:00', @backend.localize('de', @morning, :long)
345
+ end
346
+
347
+ def test_translate_given_the_default_format_it_uses_it
348
+ assert_equal 'Di, 01. Jan 2008 06:00:00 +0000', @backend.localize('de', @morning, :default)
349
+ end
350
+
351
+ def test_translate_given_a_day_name_format_it_returns_the_correct_day_name
352
+ assert_equal 'Dienstag', @backend.localize('de', @morning, '%A')
353
+ end
354
+
355
+ def test_translate_given_an_abbr_day_name_format_it_returns_the_correct_abbrevated_day_name
356
+ assert_equal 'Di', @backend.localize('de', @morning, '%a')
357
+ end
358
+
359
+ def test_translate_given_a_month_name_format_it_returns_the_correct_month_name
360
+ assert_equal 'Januar', @backend.localize('de', @morning, '%B')
361
+ end
362
+
363
+ def test_translate_given_an_abbr_month_name_format_it_returns_the_correct_abbrevated_month_name
364
+ assert_equal 'Jan', @backend.localize('de', @morning, '%b')
365
+ end
366
+
367
+ def test_translate_given_a_meridian_indicator_format_it_returns_the_correct_meridian_indicator
368
+ assert_equal 'am', @backend.localize('de', @morning, '%p')
369
+ assert_equal 'pm', @backend.localize('de', @evening, '%p')
370
+ end
371
+
372
+ def test_translate_given_no_format_it_does_not_fail
373
+ assert_nothing_raised{ @backend.localize 'de', @morning }
374
+ end
375
+
376
+ def test_translate_given_an_unknown_format_it_does_not_fail
377
+ assert_nothing_raised{ @backend.localize 'de', @morning, '%x' }
378
+ end
379
+ end
380
+
381
+ class I18nSimpleBackendLocalizeTimeTest < Test::Unit::TestCase
382
+ include I18nSimpleBackendTestSetup
383
+
384
+ def setup
385
+ @old_timezone, ENV['TZ'] = ENV['TZ'], 'UTC'
386
+ @backend = I18n::Backend::Simple.new
387
+ add_datetime_translations
388
+ @morning = Time.parse '2008-01-01 6:00 UTC'
389
+ @evening = Time.parse '2008-01-01 18:00 UTC'
390
+ end
391
+
392
+ def teardown
393
+ @old_timezone ? ENV['TZ'] = @old_timezone : ENV.delete('TZ')
394
+ end
395
+
396
+ def test_translate_given_the_short_format_it_uses_it
397
+ assert_equal '01. Jan 06:00', @backend.localize('de', @morning, :short)
398
+ end
399
+
400
+ def test_translate_given_the_long_format_it_uses_it
401
+ assert_equal '01. Januar 2008 06:00', @backend.localize('de', @morning, :long)
402
+ end
403
+
404
+ # TODO Seems to break on Windows because ENV['TZ'] is ignored. What's a better way to do this?
405
+ # def test_translate_given_the_default_format_it_uses_it
406
+ # assert_equal 'Di, 01. Jan 2008 06:00:00 +0000', @backend.localize('de', @morning, :default)
407
+ # end
408
+
409
+ def test_translate_given_a_day_name_format_it_returns_the_correct_day_name
410
+ assert_equal 'Dienstag', @backend.localize('de', @morning, '%A')
411
+ end
412
+
413
+ def test_translate_given_an_abbr_day_name_format_it_returns_the_correct_abbrevated_day_name
414
+ assert_equal 'Di', @backend.localize('de', @morning, '%a')
415
+ end
416
+
417
+ def test_translate_given_a_month_name_format_it_returns_the_correct_month_name
418
+ assert_equal 'Januar', @backend.localize('de', @morning, '%B')
419
+ end
420
+
421
+ def test_translate_given_an_abbr_month_name_format_it_returns_the_correct_abbrevated_month_name
422
+ assert_equal 'Jan', @backend.localize('de', @morning, '%b')
423
+ end
424
+
425
+ def test_translate_given_a_meridian_indicator_format_it_returns_the_correct_meridian_indicator
426
+ assert_equal 'am', @backend.localize('de', @morning, '%p')
427
+ assert_equal 'pm', @backend.localize('de', @evening, '%p')
428
+ end
429
+
430
+ def test_translate_given_no_format_it_does_not_fail
431
+ assert_nothing_raised{ @backend.localize 'de', @morning }
432
+ end
433
+
434
+ def test_translate_given_an_unknown_format_it_does_not_fail
435
+ assert_nothing_raised{ @backend.localize 'de', @morning, '%x' }
436
+ end
437
+ end
438
+
439
+ class I18nSimpleBackendHelperMethodsTest < Test::Unit::TestCase
440
+ def setup
441
+ @backend = I18n::Backend::Simple.new
442
+ end
443
+
444
+ def test_deep_symbolize_keys_works
445
+ result = @backend.send :deep_symbolize_keys, 'foo' => {'bar' => {'baz' => 'bar'}}
446
+ expected = {:foo => {:bar => {:baz => 'bar'}}}
447
+ assert_equal expected, result
448
+ end
449
+ end
450
+
451
+ class I18nSimpleBackendLoadTranslationsTest < Test::Unit::TestCase
452
+ include I18nSimpleBackendTestSetup
453
+
454
+ def test_load_translations_with_unknown_file_type_raises_exception
455
+ assert_raises(I18n::UnknownFileType) { @backend.load_translations "#{@locale_dir}/en.xml" }
456
+ end
457
+
458
+ def test_load_translations_with_ruby_file_type_does_not_raise_exception
459
+ assert_nothing_raised { @backend.load_translations "#{@locale_dir}/en.rb" }
460
+ end
461
+
462
+ def test_load_rb_loads_data_from_ruby_file
463
+ data = @backend.send :load_rb, "#{@locale_dir}/en.rb"
464
+ assert_equal({:'en-Ruby' => {:foo => {:bar => "baz"}}}, data)
465
+ end
466
+
467
+ def test_load_rb_loads_data_from_yaml_file
468
+ data = @backend.send :load_yml, "#{@locale_dir}/en.yml"
469
+ assert_equal({'en-Yaml' => {'foo' => {'bar' => 'baz'}}}, data)
470
+ end
471
+
472
+ def test_load_translations_loads_from_different_file_formats
473
+ @backend = I18n::Backend::Simple.new
474
+ @backend.load_translations "#{@locale_dir}/en.rb", "#{@locale_dir}/en.yml"
475
+ expected = {
476
+ :'en-Ruby' => {:foo => {:bar => "baz"}},
477
+ :'en-Yaml' => {:foo => {:bar => "baz"}}
478
+ }
479
+ assert_equal expected, backend_get_translations
480
+ end
481
+ end
482
+
483
+ class I18nSimpleBackendReloadTranslationsTest < Test::Unit::TestCase
484
+ include I18nSimpleBackendTestSetup
485
+
486
+ def setup
487
+ @backend = I18n::Backend::Simple.new
488
+ I18n.load_path = [File.dirname(__FILE__) + '/locale/en.yml']
489
+ assert_nil backend_get_translations
490
+ @backend.send :init_translations
491
+ end
492
+
493
+ def teardown
494
+ I18n.load_path = []
495
+ end
496
+
497
+ def test_setup
498
+ assert_not_nil backend_get_translations
499
+ end
500
+
501
+ def test_reload_translations_unloads_translations
502
+ @backend.reload!
503
+ assert_nil backend_get_translations
504
+ end
505
+
506
+ def test_reload_translations_uninitializes_translations
507
+ @backend.reload!
508
+ assert_equal @backend.initialized?, false
509
+ end
510
+ end