i18n 0.1.0 → 0.2.0

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.

@@ -0,0 +1,40 @@
1
+ module Tests
2
+ module Backend
3
+ module Api
4
+ module Pluralization
5
+ def test_pluralize_given_nil_returns_the_given_entry
6
+ entry = {:one => 'bar', :other => 'bars'}
7
+ assert_equal entry, I18n.backend.send(:pluralize, nil, entry, nil)
8
+ end
9
+
10
+ def test_pluralize_given_0_returns_zero_string_if_zero_key_given
11
+ assert_equal 'zero', I18n.backend.send(:pluralize, nil, {:zero => 'zero', :one => 'bar', :other => 'bars'}, 0)
12
+ end
13
+
14
+ def test_pluralize_given_0_returns_plural_string_if_no_zero_key_given
15
+ assert_equal 'bars', I18n.backend.send(:pluralize, nil, {:one => 'bar', :other => 'bars'}, 0)
16
+ end
17
+
18
+ def test_pluralize_given_1_returns_singular_string
19
+ assert_equal 'bar', I18n.backend.send(:pluralize, nil, {:one => 'bar', :other => 'bars'}, 1)
20
+ end
21
+
22
+ def test_pluralize_given_2_returns_plural_string
23
+ assert_equal 'bars', I18n.backend.send(:pluralize, nil, {:one => 'bar', :other => 'bars'}, 2)
24
+ end
25
+
26
+ def test_pluralize_given_3_returns_plural_string
27
+ assert_equal 'bars', I18n.backend.send(:pluralize, nil, {:one => 'bar', :other => 'bars'}, 3)
28
+ end
29
+
30
+ def test_interpolate_given_incomplete_pluralization_data_raises_invalid_pluralization_data
31
+ assert_raises(I18n::InvalidPluralizationData){ I18n.backend.send(:pluralize, nil, {:one => 'bar'}, 2) }
32
+ end
33
+
34
+ def test_pluralize_given_pluralization_data_as_default
35
+ assert_equal 'bars', I18n.t(:bar, :count => 2, :default => { :one => 'bar', :other => 'bars' })
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,49 @@
1
+ module Tests
2
+ module Backend
3
+ module Api
4
+ module Translation
5
+ def translate(key, options = {})
6
+ I18n.backend.translate('en', key, options)
7
+ end
8
+
9
+ def test_given_no_keys_it_returns_the_default
10
+ assert_equal 'default', translate(nil, :default => 'default')
11
+ end
12
+
13
+ def test_translate_given_a_symbol_as_a_default_translates_the_symbol
14
+ assert_equal 'bar', translate(nil, :default => :'foo.bar')
15
+ end
16
+
17
+ def test_translate_default_with_scope_stays_in_scope_when_looking_up_the_symbol
18
+ assert_equal 'bar', translate(:missing, :default => :bar, :scope => :foo)
19
+ end
20
+
21
+ def test_translate_given_an_array_as_default_uses_the_first_match
22
+ assert_equal 'bar', translate(:does_not_exist, :scope => [:foo], :default => [:does_not_exist_2, :bar])
23
+ end
24
+
25
+ def test_translate_given_an_array_of_inexistent_keys_it_raises_missing_translation_data
26
+ assert_raises I18n::MissingTranslationData do
27
+ translate(:does_not_exist, :scope => [:foo], :default => [:does_not_exist_2, :does_not_exist_3])
28
+ end
29
+ end
30
+
31
+ def test_translate_an_array_of_keys_translates_all_of_them
32
+ assert_equal %w(bar baz), translate([:bar, :baz], :scope => [:foo])
33
+ end
34
+
35
+ def test_translate_with_a_missing_key_and_no_default_raises_missing_translation_data
36
+ assert_raises(I18n::MissingTranslationData) do
37
+ translate(:missing)
38
+ end
39
+ end
40
+
41
+ def test_translate_given_nil_as_a_locale_raises_an_argument_error
42
+ assert_raises(I18n::InvalidLocale) do
43
+ I18n.backend.translate(nil, :bar)
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ Dir[File.dirname(__FILE__) + '/*_test.rb'].each do |file|
2
+ require file
3
+ end
@@ -0,0 +1,85 @@
1
+ # encoding: utf-8
2
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
3
+
4
+ class I18nSimpleBackendApiTest < Test::Unit::TestCase
5
+ include Tests::Backend::Simple::Setup::Base
6
+ include Tests::Backend::Api::Basics
7
+ end
8
+
9
+ class I18nSimpleBackendTranslateTest < Test::Unit::TestCase
10
+ include Tests::Backend::Simple::Setup::Base
11
+ include Tests::Backend::Api::Translation
12
+
13
+ # implementation specific tests
14
+
15
+ def test_translate_calls_lookup_with_locale_given
16
+ I18n.backend.expects(:lookup).with('de', :bar, [:foo], nil).returns 'bar'
17
+ I18n.backend.translate 'de', :bar, :scope => [:foo]
18
+ end
19
+
20
+ def test_translate_calls_pluralize
21
+ I18n.backend.expects(:pluralize).with 'en', 'bar', 1
22
+ I18n.backend.translate 'en', :bar, :scope => [:foo], :count => 1
23
+ end
24
+
25
+ def test_translate_calls_interpolate
26
+ I18n.backend.expects(:interpolate).with 'en', 'bar', {}
27
+ I18n.backend.translate 'en', :bar, :scope => [:foo]
28
+ end
29
+
30
+ def test_translate_calls_interpolate_including_count_as_a_value
31
+ I18n.backend.expects(:interpolate).with 'en', 'bar', {:count => 1}
32
+ I18n.backend.translate 'en', :bar, :scope => [:foo], :count => 1
33
+ end
34
+ end
35
+
36
+ class I18nSimpleBackendInterpolateTest < Test::Unit::TestCase
37
+ include Tests::Backend::Simple::Setup::Base
38
+ include Tests::Backend::Api::Interpolation
39
+
40
+ # implementation specific tests
41
+
42
+ def test_interpolate_given_nil_as_a_string_returns_nil
43
+ assert_nil I18n.backend.send(:interpolate, nil, nil, :name => 'David')
44
+ end
45
+
46
+ def test_interpolate_given_an_non_string_as_a_string_returns_nil
47
+ assert_equal [], I18n.backend.send(:interpolate, nil, [], :name => 'David')
48
+ end
49
+ end
50
+
51
+ class I18nSimpleBackendLambdaTest < Test::Unit::TestCase
52
+ include Tests::Backend::Simple::Setup::Base
53
+ include Tests::Backend::Api::Lambda
54
+ end
55
+
56
+ class I18nSimpleBackendTranslateLinkedTest < Test::Unit::TestCase
57
+ include Tests::Backend::Simple::Setup::Base
58
+ include Tests::Backend::Api::Link
59
+ end
60
+
61
+ class I18nSimpleBackendPluralizationTest < Test::Unit::TestCase
62
+ include Tests::Backend::Simple::Setup::Base
63
+ include Tests::Backend::Api::Pluralization
64
+ end
65
+
66
+ class I18nSimpleBackendLocalizeDateTest < Test::Unit::TestCase
67
+ include Tests::Backend::Simple::Setup::Localization
68
+ include Tests::Backend::Api::Localization::Date
69
+ end
70
+
71
+ class I18nSimpleBackendLocalizeDateTimeTest < Test::Unit::TestCase
72
+ include Tests::Backend::Simple::Setup::Localization
73
+ include Tests::Backend::Api::Localization::DateTime
74
+ end
75
+
76
+ class I18nSimpleBackendLocalizeTimeTest < Test::Unit::TestCase
77
+ include Tests::Backend::Simple::Setup::Localization
78
+ include Tests::Backend::Api::Localization::Time
79
+ end
80
+
81
+ class I18nSimpleBackendLocalizeLambdaTest < Test::Unit::TestCase
82
+ include Tests::Backend::Simple::Setup::Localization
83
+ include Tests::Backend::Api::Localization::Lambda
84
+ end
85
+
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
3
+
4
+ class I18nSimpleBackendLookupTest < Test::Unit::TestCase
5
+ include Tests::Backend::Simple::Setup::Base
6
+
7
+ # useful because this way we can use the backend with no key for interpolation/pluralization
8
+ def test_lookup_given_nil_as_a_key_returns_nil
9
+ assert_nil I18n.backend.send(:lookup, :en, nil)
10
+ end
11
+
12
+ def test_lookup_given_nested_keys_looks_up_a_nested_hash_value
13
+ assert_equal 'bar', I18n.backend.send(:lookup, :en, :bar, [:foo])
14
+ end
15
+
16
+ def test_lookup_using_a_custom_separator
17
+ assert_equal 'bar', I18n.backend.send(:lookup, :en, 'foo|bar', [], '|')
18
+ end
19
+
20
+ def test_default_using_a_custom_separator
21
+ assert_equal 'bar', I18n.backend.send(:default, :en, :'does_not_exist', :'foo|bar', :separator => '|')
22
+ end
23
+ end
@@ -0,0 +1,21 @@
1
+ module Tests
2
+ module Backend
3
+ module Simple
4
+ module Setup
5
+ module Base
6
+ def setup
7
+ super
8
+ I18n.backend = I18n::Backend::Simple.new
9
+ backend_store_translations :en, :foo => {:bar => 'bar', :baz => 'baz'}
10
+ end
11
+
12
+ def teardown
13
+ super
14
+ I18n.load_path = []
15
+ I18n.backend = nil
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,129 @@
1
+ module Tests
2
+ module Backend
3
+ module Simple
4
+ module Setup
5
+ module Localization
6
+ include Base
7
+
8
+ def setup
9
+ super
10
+ setup_datetime_translations
11
+ setup_datetime_lambda_translations
12
+ @old_timezone, ENV['TZ'] = ENV['TZ'], 'UTC'
13
+ end
14
+
15
+ def teardown
16
+ super
17
+ @old_timezone ? ENV['TZ'] = @old_timezone : ENV.delete('TZ')
18
+ end
19
+
20
+ def setup_datetime_translations
21
+ backend_store_translations :de, {
22
+ :date => {
23
+ :formats => {
24
+ :default => "%d.%m.%Y",
25
+ :short => "%d. %b",
26
+ :long => "%d. %B %Y",
27
+ :long_ordinalized => lambda { |date, options|
28
+ tz = " (#{options[:timezone]})" if options[:timezone]
29
+ "#{date.day}ter %B %Y#{tz}"
30
+ }
31
+ },
32
+ :day_names => %w(Sonntag Montag Dienstag Mittwoch Donnerstag Freitag Samstag),
33
+ :abbr_day_names => %w(So Mo Di Mi Do Fr Sa),
34
+ :month_names => %w(Januar Februar März April Mai Juni Juli August September Oktober November Dezember).unshift(nil),
35
+ :abbr_month_names => %w(Jan Feb Mar Apr Mai Jun Jul Aug Sep Okt Nov Dez).unshift(nil),
36
+ :order => [:day, :month, :year]
37
+ },
38
+ :time => {
39
+ :formats => {
40
+ :default => "%a, %d. %b %Y %H:%M:%S %z",
41
+ :short => "%d. %b %H:%M",
42
+ :long => "%d. %B %Y %H:%M",
43
+ :long_ordinalized => lambda { |date, options|
44
+ tz = " (#{options[:timezone]})" if options[:timezone]
45
+ "#{date.day}ter %B %Y, %H:%M Uhr#{tz}"
46
+ }
47
+ },
48
+ :am => 'am',
49
+ :pm => 'pm'
50
+ },
51
+ :datetime => {
52
+ :distance_in_words => {
53
+ :half_a_minute => 'half a minute',
54
+ :less_than_x_seconds => {
55
+ :one => 'less than 1 second',
56
+ :other => 'less than {{count}} seconds'
57
+ },
58
+ :x_seconds => {
59
+ :one => '1 second',
60
+ :other => '{{count}} seconds'
61
+ },
62
+ :less_than_x_minutes => {
63
+ :one => 'less than a minute',
64
+ :other => 'less than {{count}} minutes'
65
+ },
66
+ :x_minutes => {
67
+ :one => '1 minute',
68
+ :other => '{{count}} minutes'
69
+ },
70
+ :about_x_hours => {
71
+ :one => 'about 1 hour',
72
+ :other => 'about {{count}} hours'
73
+ },
74
+ :x_days => {
75
+ :one => '1 day',
76
+ :other => '{{count}} days'
77
+ },
78
+ :about_x_months => {
79
+ :one => 'about 1 month',
80
+ :other => 'about {{count}} months'
81
+ },
82
+ :x_months => {
83
+ :one => '1 month',
84
+ :other => '{{count}} months'
85
+ },
86
+ :about_x_years => {
87
+ :one => 'about 1 year',
88
+ :other => 'about {{count}} year'
89
+ },
90
+ :over_x_years => {
91
+ :one => 'over 1 year',
92
+ :other => 'over {{count}} years'
93
+ }
94
+ }
95
+ }
96
+ }
97
+ end
98
+
99
+ def setup_datetime_lambda_translations
100
+ backend_store_translations 'ru', {
101
+ :date => {
102
+ :'day_names' => lambda { |key, options|
103
+ (options[:format] =~ /^%A/) ?
104
+ %w(Воскресенье Понедельник Вторник Среда Четверг Пятница Суббота) :
105
+ %w(воскресенье понедельник вторник среда четверг пятница суббота)
106
+ },
107
+ :'abbr_day_names' => %w(Вс Пн Вт Ср Чт Пт Сб),
108
+ :'month_names' => lambda { |key, options|
109
+ (options[:format] =~ /(%d|%e)(\s*)?(%B)/) ?
110
+ %w(января февраля марта апреля мая июня июля августа сентября октября ноября декабря).unshift(nil) :
111
+ %w(Январь Февраль Март Апрель Май Июнь Июль Август Сентябрь Октябрь Ноябрь Декабрь).unshift(nil)
112
+ },
113
+ :'abbr_month_names' => lambda { |key, options|
114
+ (options[:format] =~ /(%d|%e)(\s*)(%b)/) ?
115
+ %w(янв. февр. марта апр. мая июня июля авг. сент. окт. нояб. дек.).unshift(nil) :
116
+ %w(янв. февр. март апр. май июнь июль авг. сент. окт. нояб. дек.).unshift(nil)
117
+ },
118
+ },
119
+ :time => {
120
+ :am => "утра",
121
+ :pm => "вечера"
122
+ }
123
+ }
124
+ end
125
+ end
126
+ end
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,88 @@
1
+ # encoding: utf-8
2
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
3
+
4
+ class I18nSimpleBackendLoadTranslationsTest < Test::Unit::TestCase
5
+ include Tests::Backend::Simple::Setup::Base
6
+
7
+ def test_load_translations_with_unknown_file_type_raises_exception
8
+ assert_raises(I18n::UnknownFileType) { I18n.backend.load_translations "#{locales_dir}/en.xml" }
9
+ end
10
+
11
+ def test_load_translations_with_ruby_file_type_does_not_raise_exception
12
+ assert_nothing_raised { I18n.backend.load_translations "#{locales_dir}/en.rb" }
13
+ end
14
+
15
+ def test_load_rb_loads_data_from_ruby_file
16
+ data = I18n.backend.send :load_rb, "#{locales_dir}/en.rb"
17
+ assert_equal({ :en => { :fuh => { :bah => 'bas' } } }, data)
18
+ end
19
+
20
+ def test_load_rb_loads_data_from_yaml_file
21
+ data = I18n.backend.send :load_yml, "#{locales_dir}/en.yml"
22
+ assert_equal({ 'en' => { 'foo' => { 'bar' => 'baz' } } }, data)
23
+ end
24
+
25
+ def test_load_translations_loads_from_different_file_formats
26
+ I18n.backend = I18n::Backend::Simple.new
27
+ I18n.backend.load_translations "#{locales_dir}/en.rb", "#{locales_dir}/en.yml"
28
+ expected = { :en => { :fuh => { :bah => "bas" }, :foo => { :bar => "baz" } } }
29
+ assert_equal expected, backend_get_translations
30
+ end
31
+ end
32
+
33
+ class I18nSimpleBackendStoreTranslationsTest < Test::Unit::TestCase
34
+ include Tests::Backend::Simple::Setup::Base
35
+
36
+ def test_store_translations_adds_translations # no, really :-)
37
+ I18n.backend.store_translations :'en', :foo => 'bar'
38
+ assert_equal Hash[:'en', {:foo => 'bar'}], backend_get_translations
39
+ end
40
+
41
+ def test_store_translations_deep_merges_translations
42
+ I18n.backend.store_translations :'en', :foo => {:bar => 'bar'}
43
+ I18n.backend.store_translations :'en', :foo => {:baz => 'baz'}
44
+ assert_equal Hash[:'en', {:foo => {:bar => 'bar', :baz => 'baz'}}], backend_get_translations
45
+ end
46
+
47
+ def test_store_translations_forces_locale_to_sym
48
+ I18n.backend.store_translations 'en', :foo => 'bar'
49
+ assert_equal Hash[:'en', {:foo => 'bar'}], backend_get_translations
50
+ end
51
+
52
+ def test_store_translations_converts_keys_to_symbols
53
+ # backend_reset_translations!
54
+ I18n.backend.store_translations 'en', 'foo' => {'bar' => 'bar', 'baz' => 'baz'}
55
+ assert_equal Hash[:'en', {:foo => {:bar => 'bar', :baz => 'baz'}}], backend_get_translations
56
+ end
57
+
58
+ def test_deep_symbolize_keys_works
59
+ result = I18n.backend.send :deep_symbolize_keys, 'foo' => {'bar' => {'baz' => 'bar'}}
60
+ expected = {:foo => {:bar => {:baz => 'bar'}}}
61
+ assert_equal expected, result
62
+ end
63
+ end
64
+
65
+ class I18nSimpleBackendReloadTranslationsTest < Test::Unit::TestCase
66
+ include Tests::Backend::Simple::Setup::Base
67
+
68
+ def setup
69
+ I18n.backend = I18n::Backend::Simple.new
70
+ I18n.load_path = [locales_dir + '/en.yml']
71
+ assert_nil backend_get_translations
72
+ I18n.backend.send :init_translations
73
+ end
74
+
75
+ def test_setup
76
+ assert_not_nil backend_get_translations
77
+ end
78
+
79
+ def test_reload_translations_unloads_translations
80
+ I18n.backend.reload!
81
+ assert_nil backend_get_translations
82
+ end
83
+
84
+ def test_reload_translations_uninitializes_translations
85
+ I18n.backend.reload!
86
+ assert_equal I18n.backend.initialized?, false
87
+ end
88
+ end