11 module I18nSimpleBackendTestSetup
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'
18 alias :setup :setup_backend
20 # def backend_reset_translations!
21 # I18n::Backend::Simple::ClassMethods.send :class_variable_set, :@@translations, {}
24 def backend_get_translations
25 # I18n::Backend::Simple::ClassMethods.send :class_variable_get, :@@translations
26 @backend.instance_variable_get
:@translations
29 def add_datetime_translations
30 @backend.store_translations
:'de', {
33 :default => "%d.%m.%Y",
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]
45 :default => "%a, %d. %b %Y %H:%M:%S %z",
46 :short => "%d. %b %H:%M",
47 :long => "%d. %B %Y %H:%M",
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'
61 :other => '{{count}} seconds'
63 :less_than_x_minutes => {
64 :one => 'less than a minute',
65 :other => 'less than {{count}} minutes'
69 :other => '{{count}} minutes'
72 :one => 'about 1 hour',
73 :other => 'about {{count}} hours'
77 :other => '{{count}} days'
80 :one => 'about 1 month',
81 :other => 'about {{count}} months'
85 :other => '{{count}} months'
88 :one => 'about 1 year',
89 :other => 'about {{count}} year'
92 :one => 'over 1 year',
93 :other => 'over {{count}} years'
101 class I18nSimpleBackendTranslationsTest
< Test
::Unit::TestCase
102 include I18nSimpleBackendTestSetup
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
109 def test_store_translations_deep_merges_translations
110 @backend.store_translations
:'en', :foo => {:bar => 'bar'}
111 @backend.store_translations
:'en', :foo => {:baz => 'baz'}
112 assert_equal Hash
[:'en', {:foo => {:bar => 'bar', :baz => 'baz'}}], backend_get_translations
115 def test_store_translations_forces_locale_to_sym
116 @backend.store_translations
'en', :foo => 'bar'
117 assert_equal Hash
[:'en', {:foo => 'bar'}], backend_get_translations
120 def test_store_translations_converts_keys_to_symbols
121 # backend_reset_translations!
122 @backend.store_translations
'en', 'foo' => {'bar' => 'bar', 'baz' => 'baz'}
123 assert_equal Hash
[:'en', {:foo => {:bar => 'bar', :baz => 'baz'}}], backend_get_translations
127 class I18nSimpleBackendAvailableLocalesTest
< Test
::Unit::TestCase
128 def test_available_locales
129 @backend = I18n
::Backend::Simple.new
130 @backend.store_translations
'de', :foo => 'bar'
131 @backend.store_translations
'en', :foo => 'foo'
133 assert_equal
['de', 'en'], @backend.available_locales
.map
{|locale
| locale
.to_s
}.sort
137 class I18nSimpleBackendTranslateTest
< Test
::Unit::TestCase
138 include I18nSimpleBackendTestSetup
140 def test_translate_calls_lookup_with_locale_given
141 @backend.expects(:lookup).with('de', :bar, [:foo]).returns
'bar'
142 @backend.translate
'de', :bar, :scope => [:foo]
145 def test_given_no_keys_it_returns_the_default
146 assert_equal
'default', @backend.translate('en', nil, :default => 'default')
149 def test_translate_given_a_symbol_as_a_default_translates_the_symbol
150 assert_equal
'bar', @backend.translate('en', nil, :scope => [:foo], :default => :bar)
153 def test_translate_given_an_array_as_default_uses_the_first_match
154 assert_equal
'bar', @backend.translate('en', :does_not_exist, :scope => [:foo], :default => [:does_not_exist_2, :bar])
157 def test_translate_given_an_array_of_inexistent_keys_it_raises_missing_translation_data
158 assert_raise I18n
::MissingTranslationData do
159 @backend.translate('en', :does_not_exist, :scope => [:foo], :default => [:does_not_exist_2, :does_not_exist_3])
163 def test_translate_an_array_of_keys_translates_all_of_them
164 assert_equal
%w(bar baz
), @backend.translate('en', [:bar, :baz], :scope => [:foo])
167 def test_translate_calls_pluralize
168 @backend.expects(:pluralize).with
'en', 'bar', 1
169 @backend.translate
'en', :bar, :scope => [:foo], :count => 1
172 def test_translate_calls_interpolate
173 @backend.expects(:interpolate).with
'en', 'bar', {}
174 @backend.translate
'en', :bar, :scope => [:foo]
177 def test_translate_calls_interpolate_including_count_as_a_value
178 @backend.expects(:interpolate).with
'en', 'bar', {:count => 1}
179 @backend.translate
'en', :bar, :scope => [:foo], :count => 1
182 def test_translate_given_nil_as_a_locale_raises_an_argument_error
183 assert_raise(I18n
::InvalidLocale){ @backend.translate
nil, :bar }
186 def test_translate_with_a_bogus_key_and_no_default_raises_missing_translation_data
187 assert_raise(I18n
::MissingTranslationData){ @backend.translate
'de', :bogus }
191 class I18nSimpleBackendLookupTest
< Test
::Unit::TestCase
192 include I18nSimpleBackendTestSetup
194 # useful because this way we can use the backend with no key for interpolation/pluralization
195 def test_lookup_given_nil_as_a_key_returns_nil
196 assert_nil
@backend.send(:lookup, 'en', nil)
199 def test_lookup_given_nested_keys_looks_up_a_nested_hash_value
200 assert_equal
'bar', @backend.send(:lookup, 'en', :bar, [:foo])
204 class I18nSimpleBackendPluralizeTest
< Test
::Unit::TestCase
205 include I18nSimpleBackendTestSetup
207 def test_pluralize_given_nil_returns_the_given_entry
208 entry
= {:one => 'bar', :other => 'bars'}
209 assert_equal entry
, @backend.send(:pluralize, nil, entry
, nil)
212 def test_pluralize_given_0_returns_zero_string_if_zero_key_given
213 assert_equal
'zero', @backend.send(:pluralize, nil, {:zero => 'zero', :one => 'bar', :other => 'bars'}, 0)
216 def test_pluralize_given_0_returns_plural_string_if_no_zero_key_given
217 assert_equal
'bars', @backend.send(:pluralize, nil, {:one => 'bar', :other => 'bars'}, 0)
220 def test_pluralize_given_1_returns_singular_string
221 assert_equal
'bar', @backend.send(:pluralize, nil, {:one => 'bar', :other => 'bars'}, 1)
224 def test_pluralize_given_2_returns_plural_string
225 assert_equal
'bars', @backend.send(:pluralize, nil, {:one => 'bar', :other => 'bars'}, 2)
228 def test_pluralize_given_3_returns_plural_string
229 assert_equal
'bars', @backend.send(:pluralize, nil, {:one => 'bar', :other => 'bars'}, 3)
232 def test_interpolate_given_incomplete_pluralization_data_raises_invalid_pluralization_data
233 assert_raise(I18n
::InvalidPluralizationData){ @backend.send(:pluralize, nil, {:one => 'bar'}, 2) }
236 # def test_interpolate_given_a_string_raises_invalid_pluralization_data
237 # assert_raise(I18n::InvalidPluralizationData){ @backend.send(:pluralize, nil, 'bar', 2) }
240 # def test_interpolate_given_an_array_raises_invalid_pluralization_data
241 # assert_raise(I18n::InvalidPluralizationData){ @backend.send(:pluralize, nil, ['bar'], 2) }
245 class I18nSimpleBackendInterpolateTest
< Test
::Unit::TestCase
246 include I18nSimpleBackendTestSetup
248 def test_interpolate_given_a_value_hash_interpolates_the_values_to_the_string
249 assert_equal
'Hi David!', @backend.send(:interpolate, nil, 'Hi {{name}}!', :name => 'David')
252 def test_interpolate_given_a_value_hash_interpolates_into_unicode_string
253 assert_equal
'Häi David!', @backend.send(:interpolate, nil, 'Häi {{name}}!', :name => 'David')
256 def test_interpolate_given_an_unicode_value_hash_interpolates_to_the_string
257 assert_equal
'Hi ゆきひろ!', @backend.send(:interpolate, nil, 'Hi {{name}}!', :name => 'ゆきひろ')
260 def test_interpolate_given_an_unicode_value_hash_interpolates_into_unicode_string
261 assert_equal
'こんにちは、ゆきひろさん!', @backend.send(:interpolate, nil, 'こんにちは、{{name}}さん!', :name => 'ゆきひろ')
264 if Kernel
.const_defined
?(:Encoding)
265 def test_interpolate_given_a_non_unicode_multibyte_value_hash_interpolates_into_a_string_with_the_same_encoding
266 assert_equal
euc_jp('Hi ゆきひろ!'), @backend.send(:interpolate, nil, 'Hi {{name}}!', :name => euc_jp('ゆきひろ'))
269 def test_interpolate_given_an_unicode_value_hash_into_a_non_unicode_multibyte_string_raises_encoding_compatibility_error
270 assert_raise(Encoding
::CompatibilityError) do
271 @backend.send(:interpolate, nil, euc_jp('こんにちは、{{name}}さん!'), :name => 'ゆきひろ')
275 def test_interpolate_given_a_non_unicode_multibyte_value_hash_into_an_unicode_string_raises_encoding_compatibility_error
276 assert_raise(Encoding
::CompatibilityError) do
277 @backend.send(:interpolate, nil, 'こんにちは、{{name}}さん!', :name => euc_jp('ゆきひろ'))
282 def test_interpolate_given_nil_as_a_string_returns_nil
283 assert_nil
@backend.send(:interpolate, nil, nil, :name => 'David')
286 def test_interpolate_given_an_non_string_as_a_string_returns_nil
287 assert_equal
[], @backend.send(:interpolate, nil, [], :name => 'David')
290 def test_interpolate_given_a_values_hash_with_nil_values_interpolates_the_string
291 assert_equal
'Hi !', @backend.send(:interpolate, nil, 'Hi {{name}}!', {:name => nil})
294 def test_interpolate_given_an_empty_values_hash_raises_missing_interpolation_argument
295 assert_raise(I18n
::MissingInterpolationArgument) { @backend.send(:interpolate, nil, 'Hi {{name}}!', {}) }
298 def test_interpolate_given_a_string_containing_a_reserved_key_raises_reserved_interpolation_key
299 assert_raise(I18n
::ReservedInterpolationKey) { @backend.send(:interpolate, nil, '{{default}}', {:default => nil}) }
305 string
.encode
!(Encoding
::EUC_JP)
309 class I18nSimpleBackendLocalizeDateTest
< Test
::Unit::TestCase
310 include I18nSimpleBackendTestSetup
313 @backend = I18n
::Backend::Simple.new
314 add_datetime_translations
315 @date = Date
.new
2008, 1, 1
318 def test_translate_given_the_short_format_it_uses_it
319 assert_equal
'01. Jan', @backend.localize('de', @date, :short)
322 def test_translate_given_the_long_format_it_uses_it
323 assert_equal
'01. Januar 2008', @backend.localize('de', @date, :long)
326 def test_translate_given_the_default_format_it_uses_it
327 assert_equal
'01.01.2008', @backend.localize('de', @date, :default)
330 def test_translate_given_a_day_name_format_it_returns_a_day_name
331 assert_equal
'Dienstag', @backend.localize('de', @date, '%A')
334 def test_translate_given_an_abbr_day_name_format_it_returns_an_abbrevated_day_name
335 assert_equal
'Di', @backend.localize('de', @date, '%a')
338 def test_translate_given_a_month_name_format_it_returns_a_month_name
339 assert_equal
'Januar', @backend.localize('de', @date, '%B')
342 def test_translate_given_an_abbr_month_name_format_it_returns_an_abbrevated_month_name
343 assert_equal
'Jan', @backend.localize('de', @date, '%b')
346 def test_translate_given_no_format_it_does_not_fail
347 assert_nothing_raised
{ @backend.localize
'de', @date }
350 def test_translate_given_an_unknown_format_it_does_not_fail
351 assert_nothing_raised
{ @backend.localize
'de', @date, '%x' }
354 def test_localize_nil_raises_argument_error
355 assert_raise(I18n
::ArgumentError) { @backend.localize
'de', nil }
358 def test_localize_object_raises_argument_error
359 assert_raise(I18n
::ArgumentError) { @backend.localize
'de', Object
.new
}
363 class I18nSimpleBackendLocalizeDateTimeTest
< Test
::Unit::TestCase
364 include I18nSimpleBackendTestSetup
367 @backend = I18n
::Backend::Simple.new
368 add_datetime_translations
369 @morning = DateTime
.new
2008, 1, 1, 6
370 @evening = DateTime
.new
2008, 1, 1, 18
373 def test_translate_given_the_short_format_it_uses_it
374 assert_equal
'01. Jan 06:00', @backend.localize('de', @morning, :short)
377 def test_translate_given_the_long_format_it_uses_it
378 assert_equal
'01. Januar 2008 06:00', @backend.localize('de', @morning, :long)
381 def test_translate_given_the_default_format_it_uses_it
382 assert_equal
'Di, 01. Jan 2008 06:00:00 +0000', @backend.localize('de', @morning, :default)
385 def test_translate_given_a_day_name_format_it_returns_the_correct_day_name
386 assert_equal
'Dienstag', @backend.localize('de', @morning, '%A')
389 def test_translate_given_an_abbr_day_name_format_it_returns_the_correct_abbrevated_day_name
390 assert_equal
'Di', @backend.localize('de', @morning, '%a')
393 def test_translate_given_a_month_name_format_it_returns_the_correct_month_name
394 assert_equal
'Januar', @backend.localize('de', @morning, '%B')
397 def test_translate_given_an_abbr_month_name_format_it_returns_the_correct_abbrevated_month_name
398 assert_equal
'Jan', @backend.localize('de', @morning, '%b')
401 def test_translate_given_a_meridian_indicator_format_it_returns_the_correct_meridian_indicator
402 assert_equal
'am', @backend.localize('de', @morning, '%p')
403 assert_equal
'pm', @backend.localize('de', @evening, '%p')
406 def test_translate_given_no_format_it_does_not_fail
407 assert_nothing_raised
{ @backend.localize
'de', @morning }
410 def test_translate_given_an_unknown_format_it_does_not_fail
411 assert_nothing_raised
{ @backend.localize
'de', @morning, '%x' }
415 class I18nSimpleBackendLocalizeTimeTest
< Test
::Unit::TestCase
416 include I18nSimpleBackendTestSetup
419 @old_timezone, ENV['TZ'] = ENV['TZ'], 'UTC'
420 @backend = I18n
::Backend::Simple.new
421 add_datetime_translations
422 @morning = Time
.parse
'2008-01-01 6:00 UTC'
423 @evening = Time
.parse
'2008-01-01 18:00 UTC'
427 @old_timezone ? ENV['TZ'] = @old_timezone : ENV.delete('TZ')
430 def test_translate_given_the_short_format_it_uses_it
431 assert_equal
'01. Jan 06:00', @backend.localize('de', @morning, :short)
434 def test_translate_given_the_long_format_it_uses_it
435 assert_equal
'01. Januar 2008 06:00', @backend.localize('de', @morning, :long)
438 # TODO Seems to break on Windows because ENV['TZ'] is ignored. What's a better way to do this?
439 # def test_translate_given_the_default_format_it_uses_it
440 # assert_equal 'Di, 01. Jan 2008 06:00:00 +0000', @backend.localize('de', @morning, :default)
443 def test_translate_given_a_day_name_format_it_returns_the_correct_day_name
444 assert_equal
'Dienstag', @backend.localize('de', @morning, '%A')
447 def test_translate_given_an_abbr_day_name_format_it_returns_the_correct_abbrevated_day_name
448 assert_equal
'Di', @backend.localize('de', @morning, '%a')
451 def test_translate_given_a_month_name_format_it_returns_the_correct_month_name
452 assert_equal
'Januar', @backend.localize('de', @morning, '%B')
455 def test_translate_given_an_abbr_month_name_format_it_returns_the_correct_abbrevated_month_name
456 assert_equal
'Jan', @backend.localize('de', @morning, '%b')
459 def test_translate_given_a_meridian_indicator_format_it_returns_the_correct_meridian_indicator
460 assert_equal
'am', @backend.localize('de', @morning, '%p')
461 assert_equal
'pm', @backend.localize('de', @evening, '%p')
464 def test_translate_given_no_format_it_does_not_fail
465 assert_nothing_raised
{ @backend.localize
'de', @morning }
468 def test_translate_given_an_unknown_format_it_does_not_fail
469 assert_nothing_raised
{ @backend.localize
'de', @morning, '%x' }
473 class I18nSimpleBackendHelperMethodsTest
< Test
::Unit::TestCase
475 @backend = I18n
::Backend::Simple.new
478 def test_deep_symbolize_keys_works
479 result
= @backend.send
:deep_symbolize_keys, 'foo' => {'bar' => {'baz' => 'bar'}}
480 expected
= {:foo => {:bar => {:baz => 'bar'}}}
481 assert_equal expected
, result
485 class I18nSimpleBackendLoadTranslationsTest
< Test
::Unit::TestCase
486 include I18nSimpleBackendTestSetup
488 def test_load_translations_with_unknown_file_type_raises_exception
489 assert_raise(I18n
::UnknownFileType) { @backend.load_translations
"#{@locale_dir}/en.xml" }
492 def test_load_translations_with_ruby_file_type_does_not_raise_exception
493 assert_nothing_raised
{ @backend.load_translations
"#{@locale_dir}/en.rb" }
496 def test_load_rb_loads_data_from_ruby_file
497 data = @backend.send
:load_rb, "#{@locale_dir}/en.rb"
498 assert_equal({:'en-Ruby' => {:foo => {:bar => "baz"}}}, data)
501 def test_load_rb_loads_data_from_yaml_file
502 data = @backend.send
:load_yml, "#{@locale_dir}/en.yml"
503 assert_equal({'en-Yaml' => {'foo' => {'bar' => 'baz'}}}, data)
506 def test_load_translations_loads_from_different_file_formats
507 @backend = I18n
::Backend::Simple.new
508 @backend.load_translations
"#{@locale_dir}/en.rb", "#{@locale_dir}/en.yml"
510 :'en-Ruby' => {:foo => {:bar => "baz"}},
511 :'en-Yaml' => {:foo => {:bar => "baz"}}
513 assert_equal expected
, backend_get_translations
517 class I18nSimpleBackendLoadPathTest
< Test
::Unit::TestCase
518 include I18nSimpleBackendTestSetup
524 def test_nested_load_paths_do_not_break_locale_loading
525 @backend = I18n
::Backend::Simple.new
526 I18n
.load_path
= [[File
.dirname(__FILE__
) + '/locale/en.yml']]
527 assert_nil backend_get_translations
528 assert_nothing_raised
{ @backend.send
:init_translations }
529 assert_not_nil backend_get_translations
532 def test_adding_arrays_of_filenames_to_load_path_do_not_break_locale_loading
533 @backend = I18n
::Backend::Simple.new
534 I18n
.load_path
<< Dir
[File
.dirname(__FILE__
) + '/locale/*.{rb,yml}']
535 assert_nil backend_get_translations
536 assert_nothing_raised
{ @backend.send
:init_translations }
537 assert_not_nil backend_get_translations
541 class I18nSimpleBackendReloadTranslationsTest
< Test
::Unit::TestCase
542 include I18nSimpleBackendTestSetup
545 @backend = I18n
::Backend::Simple.new
546 I18n
.load_path
= [File
.dirname(__FILE__
) + '/locale/en.yml']
547 assert_nil backend_get_translations
548 @backend.send
:init_translations
556 assert_not_nil backend_get_translations
559 def test_reload_translations_unloads_translations
561 assert_nil backend_get_translations
564 def test_reload_translations_uninitializes_translations
566 assert_equal
@backend.initialized
?, false