i18n 0.9.5 → 1.6.0

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.
Files changed (86) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +54 -13
  3. data/lib/i18n/backend/base.rb +46 -17
  4. data/lib/i18n/backend/cache.rb +8 -9
  5. data/lib/i18n/backend/cache_file.rb +36 -0
  6. data/lib/i18n/backend/cascade.rb +3 -1
  7. data/lib/i18n/backend/chain.rb +33 -3
  8. data/lib/i18n/backend/fallbacks.rb +11 -13
  9. data/lib/i18n/backend/flatten.rb +2 -0
  10. data/lib/i18n/backend/gettext.rb +4 -0
  11. data/lib/i18n/backend/interpolation_compiler.rb +3 -1
  12. data/lib/i18n/backend/key_value.rb +31 -2
  13. data/lib/i18n/backend/memoize.rb +10 -2
  14. data/lib/i18n/backend/metadata.rb +5 -3
  15. data/lib/i18n/backend/pluralization.rb +2 -0
  16. data/lib/i18n/backend/simple.rb +26 -8
  17. data/lib/i18n/backend/transliterator.rb +2 -0
  18. data/lib/i18n/backend.rb +3 -0
  19. data/lib/i18n/config.rb +20 -2
  20. data/lib/i18n/core_ext/hash.rb +42 -24
  21. data/lib/i18n/exceptions.rb +23 -16
  22. data/lib/i18n/gettext/helpers.rb +3 -1
  23. data/lib/i18n/gettext/po_parser.rb +7 -7
  24. data/lib/i18n/gettext.rb +2 -0
  25. data/lib/i18n/interpolate/ruby.rb +5 -3
  26. data/lib/i18n/locale/fallbacks.rb +1 -1
  27. data/lib/i18n/locale.rb +2 -0
  28. data/lib/i18n/middleware.rb +2 -0
  29. data/lib/i18n/tests/interpolation.rb +5 -4
  30. data/lib/i18n/tests/localization/date.rb +28 -6
  31. data/lib/i18n/tests/localization/date_time.rb +27 -6
  32. data/lib/i18n/tests/localization/time.rb +26 -4
  33. data/lib/i18n/tests.rb +2 -0
  34. data/lib/i18n/version.rb +3 -1
  35. data/lib/i18n.rb +72 -30
  36. metadata +24 -58
  37. data/gemfiles/Gemfile.rails-3.2.x +0 -10
  38. data/gemfiles/Gemfile.rails-4.0.x +0 -10
  39. data/gemfiles/Gemfile.rails-4.1.x +0 -10
  40. data/gemfiles/Gemfile.rails-4.2.x +0 -10
  41. data/gemfiles/Gemfile.rails-5.0.x +0 -10
  42. data/gemfiles/Gemfile.rails-5.1.x +0 -10
  43. data/gemfiles/Gemfile.rails-master +0 -10
  44. data/lib/i18n/core_ext/kernel/suppress_warnings.rb +0 -8
  45. data/lib/i18n/core_ext/string/interpolate.rb +0 -9
  46. data/test/api/all_features_test.rb +0 -58
  47. data/test/api/cascade_test.rb +0 -28
  48. data/test/api/chain_test.rb +0 -24
  49. data/test/api/fallbacks_test.rb +0 -30
  50. data/test/api/key_value_test.rb +0 -24
  51. data/test/api/memoize_test.rb +0 -56
  52. data/test/api/override_test.rb +0 -42
  53. data/test/api/pluralization_test.rb +0 -30
  54. data/test/api/simple_test.rb +0 -28
  55. data/test/backend/cache_test.rb +0 -109
  56. data/test/backend/cascade_test.rb +0 -86
  57. data/test/backend/chain_test.rb +0 -122
  58. data/test/backend/exceptions_test.rb +0 -36
  59. data/test/backend/fallbacks_test.rb +0 -219
  60. data/test/backend/interpolation_compiler_test.rb +0 -118
  61. data/test/backend/key_value_test.rb +0 -61
  62. data/test/backend/memoize_test.rb +0 -79
  63. data/test/backend/metadata_test.rb +0 -48
  64. data/test/backend/pluralization_test.rb +0 -45
  65. data/test/backend/simple_test.rb +0 -103
  66. data/test/backend/transliterator_test.rb +0 -84
  67. data/test/core_ext/hash_test.rb +0 -36
  68. data/test/gettext/api_test.rb +0 -214
  69. data/test/gettext/backend_test.rb +0 -92
  70. data/test/i18n/exceptions_test.rb +0 -117
  71. data/test/i18n/gettext_plural_keys_test.rb +0 -20
  72. data/test/i18n/interpolate_test.rb +0 -91
  73. data/test/i18n/load_path_test.rb +0 -34
  74. data/test/i18n/middleware_test.rb +0 -24
  75. data/test/i18n_test.rb +0 -462
  76. data/test/locale/fallbacks_test.rb +0 -133
  77. data/test/locale/tag/rfc4646_test.rb +0 -143
  78. data/test/locale/tag/simple_test.rb +0 -32
  79. data/test/run_all.rb +0 -20
  80. data/test/test_data/locales/de.po +0 -82
  81. data/test/test_data/locales/en.rb +0 -3
  82. data/test/test_data/locales/en.yml +0 -3
  83. data/test/test_data/locales/invalid/empty.yml +0 -0
  84. data/test/test_data/locales/invalid/syntax.yml +0 -4
  85. data/test/test_data/locales/plurals.rb +0 -113
  86. data/test/test_helper.rb +0 -61
@@ -1,30 +0,0 @@
1
- require 'test_helper'
2
-
3
- class I18nPluralizationApiTest < I18n::TestCase
4
- class Backend < I18n::Backend::Simple
5
- include I18n::Backend::Pluralization
6
- end
7
-
8
- def setup
9
- I18n.backend = Backend.new
10
- super
11
- end
12
-
13
- include I18n::Tests::Basics
14
- include I18n::Tests::Defaults
15
- include I18n::Tests::Interpolation
16
- include I18n::Tests::Link
17
- include I18n::Tests::Lookup
18
- include I18n::Tests::Pluralization
19
- include I18n::Tests::Procs
20
- include I18n::Tests::Localization::Date
21
- include I18n::Tests::Localization::DateTime
22
- include I18n::Tests::Localization::Time
23
- include I18n::Tests::Localization::Procs
24
-
25
- test "make sure we use a backend with Pluralization included" do
26
- assert_equal Backend, I18n.backend.class
27
- end
28
-
29
- # links: test that keys stored on one backend can link to keys stored on another backend
30
- end
@@ -1,28 +0,0 @@
1
- require 'test_helper'
2
-
3
- class I18nSimpleBackendApiTest < I18n::TestCase
4
- class Backend < I18n::Backend::Simple
5
- include I18n::Backend::Pluralization
6
- end
7
-
8
- def setup
9
- I18n.backend = I18n::Backend::Simple.new
10
- super
11
- end
12
-
13
- include I18n::Tests::Basics
14
- include I18n::Tests::Defaults
15
- include I18n::Tests::Interpolation
16
- include I18n::Tests::Link
17
- include I18n::Tests::Lookup
18
- include I18n::Tests::Pluralization
19
- include I18n::Tests::Procs
20
- include I18n::Tests::Localization::Date
21
- include I18n::Tests::Localization::DateTime
22
- include I18n::Tests::Localization::Time
23
- include I18n::Tests::Localization::Procs
24
-
25
- test "make sure we use the Simple backend" do
26
- assert_equal I18n::Backend::Simple, I18n.backend.class
27
- end
28
- end
@@ -1,109 +0,0 @@
1
- require 'test_helper'
2
- require 'digest/md5'
3
-
4
- begin
5
- require 'active_support'
6
- rescue LoadError
7
- $stderr.puts "Skipping cache tests using ActiveSupport"
8
- else
9
-
10
- class I18nBackendCacheTest < I18n::TestCase
11
- class Backend < I18n::Backend::Simple
12
- include I18n::Backend::Cache
13
- end
14
-
15
- def setup
16
- I18n.backend = Backend.new
17
- super
18
- I18n.cache_store = ActiveSupport::Cache.lookup_store(:memory_store)
19
- I18n.cache_key_digest = nil
20
- end
21
-
22
- def teardown
23
- super
24
- I18n.cache_store = nil
25
- end
26
-
27
- test "it uses the cache" do
28
- assert I18n.cache_store.is_a?(ActiveSupport::Cache::MemoryStore)
29
- end
30
-
31
- test "translate hits the backend and caches the response" do
32
- I18n.backend.expects(:lookup).returns('Foo')
33
- assert_equal 'Foo', I18n.t(:foo)
34
-
35
- I18n.backend.expects(:lookup).never
36
- assert_equal 'Foo', I18n.t(:foo)
37
-
38
- I18n.backend.expects(:lookup).returns('Bar')
39
- assert_equal 'Bar', I18n.t(:bar)
40
- end
41
-
42
- test "translate returns a cached false response" do
43
- I18n.backend.expects(:lookup).never
44
- I18n.cache_store.expects(:read).returns(false)
45
- assert_equal false, I18n.t(:foo)
46
- end
47
-
48
- test "still raises MissingTranslationData but also caches it" do
49
- assert_raise(I18n::MissingTranslationData) { I18n.t(:missing, :raise => true) }
50
- assert_raise(I18n::MissingTranslationData) { I18n.t(:missing, :raise => true) }
51
- assert_equal 1, I18n.cache_store.instance_variable_get(:@data).size
52
-
53
- # I18n.backend.expects(:lookup).returns(nil)
54
- # assert_raise(I18n::MissingTranslationData) { I18n.t(:missing, :raise => true) }
55
- # I18n.backend.expects(:lookup).never
56
- # assert_raise(I18n::MissingTranslationData) { I18n.t(:missing, :raise => true) }
57
- end
58
-
59
- test "uses 'i18n' as a cache key namespace by default" do
60
- assert_equal 0, I18n.backend.send(:cache_key, :en, :foo, {}).index('i18n')
61
- end
62
-
63
- test "adds a custom cache key namespace" do
64
- with_cache_namespace('bar') do
65
- assert_equal 0, I18n.backend.send(:cache_key, :en, :foo, {}).index('i18n/bar/')
66
- end
67
- end
68
-
69
- test "adds locale and hash of key and hash of options" do
70
- options = { :bar=>1 }
71
- options_hash = RUBY_VERSION <= "1.9" ? options.inspect.hash : options.hash
72
- assert_equal "i18n//en/#{:foo.hash}/#{options_hash}", I18n.backend.send(:cache_key, :en, :foo, options)
73
- end
74
-
75
- test "cache_key uses configured digest method" do
76
- md5 = Digest::MD5.new
77
- options = { :bar=>1 }
78
- options_hash = options.inspect
79
- with_cache_key_digest(md5) do
80
- assert_equal "i18n//en/#{md5.hexdigest(:foo.to_s)}/#{md5.hexdigest(options_hash)}", I18n.backend.send(:cache_key, :en, :foo, options)
81
- end
82
- end
83
-
84
- test "keys should not be equal" do
85
- interpolation_values1 = { :foo => 1, :bar => 2 }
86
- interpolation_values2 = { :foo => 2, :bar => 1 }
87
-
88
- key1 = I18n.backend.send(:cache_key, :en, :some_key, interpolation_values1)
89
- key2 = I18n.backend.send(:cache_key, :en, :some_key, interpolation_values2)
90
-
91
- assert key1 != key2
92
- end
93
-
94
- protected
95
-
96
- def with_cache_namespace(namespace)
97
- I18n.cache_namespace = namespace
98
- yield
99
- I18n.cache_namespace = nil
100
- end
101
-
102
- def with_cache_key_digest(digest)
103
- I18n.cache_key_digest = digest
104
- yield
105
- I18n.cache_key_digest = nil
106
- end
107
- end
108
-
109
- end # AS cache check
@@ -1,86 +0,0 @@
1
- require 'test_helper'
2
-
3
- class I18nBackendCascadeTest < I18n::TestCase
4
- class Backend < I18n::Backend::Simple
5
- include I18n::Backend::Cascade
6
- end
7
-
8
- def setup
9
- super
10
- I18n.backend = Backend.new
11
- store_translations(:en, :foo => 'foo', :bar => { :baz => 'baz' })
12
- @cascade_options = { :step => 1, :offset => 1, :skip_root => false }
13
- end
14
-
15
- def lookup(key, options = {})
16
- I18n.t(key, options.merge(:cascade => @cascade_options))
17
- end
18
-
19
- test "still returns an existing translation as usual" do
20
- assert_equal 'foo', lookup(:foo)
21
- assert_equal 'baz', lookup(:'bar.baz')
22
- end
23
-
24
- test "falls back by cutting keys off the end of the scope" do
25
- assert_equal 'foo', lookup(:foo, :scope => :'missing')
26
- assert_equal 'foo', lookup(:foo, :scope => :'missing.missing')
27
- assert_equal 'baz', lookup(:baz, :scope => :'bar.missing')
28
- assert_equal 'baz', lookup(:baz, :scope => :'bar.missing.missing')
29
- end
30
-
31
- test "raises I18n::MissingTranslationData exception when no translation was found" do
32
- assert_raise(I18n::MissingTranslationData) { lookup(:'foo.missing', :raise => true) }
33
- assert_raise(I18n::MissingTranslationData) { lookup(:'bar.baz.missing', :raise => true) }
34
- assert_raise(I18n::MissingTranslationData) { lookup(:'missing.bar.baz', :raise => true) }
35
- end
36
-
37
- test "cascades before evaluating the default" do
38
- assert_equal 'foo', lookup(:foo, :scope => :missing, :default => 'default')
39
- end
40
-
41
- test "cascades defaults, too" do
42
- assert_equal 'foo', lookup(nil, :default => [:'missing.missing', :'missing.foo'])
43
- end
44
-
45
- test "works with :offset => 2 and a single key" do
46
- @cascade_options[:offset] = 2
47
- lookup(:foo)
48
- end
49
-
50
- test "assemble required fallbacks for ActiveRecord validation messages" do
51
- store_translations(:en,
52
- :errors => {
53
- :odd => 'errors.odd',
54
- :reply => { :title => { :blank => 'errors.reply.title.blank' }, :taken => 'errors.reply.taken' },
55
- :topic => { :title => { :format => 'errors.topic.title.format' }, :length => 'errors.topic.length' }
56
- }
57
- )
58
- assert_equal 'errors.reply.title.blank', lookup(:'errors.reply.title.blank', :default => :'errors.topic.title.blank')
59
- assert_equal 'errors.reply.taken', lookup(:'errors.reply.title.taken', :default => :'errors.topic.title.taken')
60
- assert_equal 'errors.topic.title.format', lookup(:'errors.reply.title.format', :default => :'errors.topic.title.format')
61
- assert_equal 'errors.topic.length', lookup(:'errors.reply.title.length', :default => :'errors.topic.title.length')
62
- assert_equal 'errors.odd', lookup(:'errors.reply.title.odd', :default => :'errors.topic.title.odd')
63
- end
64
-
65
- test "assemble action view translation helper lookup cascade" do
66
- @cascade_options[:offset] = 2
67
-
68
- store_translations(:en,
69
- :menu => { :show => 'menu.show' },
70
- :namespace => {
71
- :menu => { :new => 'namespace.menu.new' },
72
- :controller => {
73
- :menu => { :edit => 'namespace.controller.menu.edit' },
74
- :action => {
75
- :menu => { :destroy => 'namespace.controller.action.menu.destroy' }
76
- }
77
- }
78
- }
79
- )
80
-
81
- assert_equal 'menu.show', lookup(:'namespace.controller.action.menu.show')
82
- assert_equal 'namespace.menu.new', lookup(:'namespace.controller.action.menu.new')
83
- assert_equal 'namespace.controller.menu.edit', lookup(:'namespace.controller.action.menu.edit')
84
- assert_equal 'namespace.controller.action.menu.destroy', lookup(:'namespace.controller.action.menu.destroy')
85
- end
86
- end
@@ -1,122 +0,0 @@
1
- require 'test_helper'
2
-
3
- class I18nBackendChainTest < I18n::TestCase
4
- def setup
5
- super
6
- @first = backend(:en => {
7
- :foo => 'Foo', :formats => {
8
- :short => 'short',
9
- :subformats => {:short => 'short'},
10
- },
11
- :plural_1 => { :one => '%{count}' },
12
- :dates => {:a => "A"}
13
- })
14
- @second = backend(:en => {
15
- :bar => 'Bar', :formats => {
16
- :long => 'long',
17
- :subformats => {:long => 'long'},
18
- },
19
- :plural_2 => { :one => 'one' },
20
- :dates => {:a => "B", :b => "B"}
21
- })
22
- @chain = I18n.backend = I18n::Backend::Chain.new(@first, @second)
23
- end
24
-
25
- test "looks up translations from the first chained backend" do
26
- assert_equal 'Foo', @first.send(:translations)[:en][:foo]
27
- assert_equal 'Foo', I18n.t(:foo)
28
- end
29
-
30
- test "looks up translations from the second chained backend" do
31
- assert_equal 'Bar', @second.send(:translations)[:en][:bar]
32
- assert_equal 'Bar', I18n.t(:bar)
33
- end
34
-
35
- test "defaults only apply to lookups on the last backend in the chain" do
36
- assert_equal 'Foo', I18n.t(:foo, :default => 'Bah')
37
- assert_equal 'Bar', I18n.t(:bar, :default => 'Bah')
38
- assert_equal 'Bah', I18n.t(:bah, :default => 'Bah') # default kicks in only here
39
- end
40
-
41
- test "default" do
42
- assert_equal 'Fuh', I18n.t(:default => 'Fuh')
43
- assert_equal 'Zero', I18n.t(:default => { :zero => 'Zero' }, :count => 0)
44
- assert_equal({ :zero => 'Zero' }, I18n.t(:default => { :zero => 'Zero' }))
45
- assert_equal 'Foo', I18n.t(:default => :foo)
46
- end
47
-
48
- test 'default is returned if translation is missing' do
49
- assert_equal({}, I18n.t(:'i18n.transliterate.rule', :locale => 'en', :default => {}))
50
- end
51
-
52
- test "namespace lookup collects results from all backends and merges deep hashes" do
53
- assert_equal({:long=>"long", :subformats=>{:long=>"long", :short=>"short"}, :short=>"short"}, I18n.t(:formats))
54
- end
55
-
56
- test "namespace lookup collects results from all backends and lets leftmost backend take priority" do
57
- assert_equal({ :a => "A", :b => "B" }, I18n.t(:dates))
58
- end
59
-
60
- test "namespace lookup with only the first backend returning a result" do
61
- assert_equal({ :one => '%{count}' }, I18n.t(:plural_1))
62
- end
63
-
64
- test "pluralization still works" do
65
- assert_equal '1', I18n.t(:plural_1, :count => 1)
66
- assert_equal 'one', I18n.t(:plural_2, :count => 1)
67
- end
68
-
69
- test "bulk lookup collects results from all backends" do
70
- assert_equal ['Foo', 'Bar'], I18n.t([:foo, :bar])
71
- assert_equal ['Foo', 'Bar', 'Bah'], I18n.t([:foo, :bar, :bah], :default => 'Bah')
72
- assert_equal [{
73
- :long=>"long",
74
- :subformats=>{:long=>"long", :short=>"short"},
75
- :short=>"short"}, {:one=>"one"},
76
- "Bah"], I18n.t([:formats, :plural_2, :bah], :default => 'Bah')
77
- end
78
-
79
- test "store_translations options are not dropped while transfering to backend" do
80
- @first.expects(:store_translations).with(:foo, {:bar => :baz}, {:option => 'persists'})
81
- I18n.backend.store_translations :foo, {:bar => :baz}, {:option => 'persists'}
82
- end
83
-
84
- protected
85
-
86
- def backend(translations)
87
- backend = I18n::Backend::Simple.new
88
- translations.each { |locale, data| backend.store_translations(locale, data) }
89
- backend
90
- end
91
- end
92
-
93
- class I18nBackendChainWithKeyValueTest < I18n::TestCase
94
- def setup_backend!(subtrees = true)
95
- first = I18n::Backend::KeyValue.new({}, subtrees)
96
- first.store_translations(:en, :plural_1 => { :one => '%{count}' })
97
-
98
- second = I18n::Backend::Simple.new
99
- second.store_translations(:en, :plural_2 => { :one => 'one' })
100
- I18n.backend = I18n::Backend::Chain.new(first, second)
101
- end
102
-
103
- test "subtrees enabled: looks up pluralization translations from the first chained backend" do
104
- setup_backend!
105
- assert_equal '1', I18n.t(:plural_1, count: 1)
106
- end
107
-
108
- test "subtrees disabled: looks up pluralization translations from the first chained backend" do
109
- setup_backend!(false)
110
- assert_equal '1', I18n.t(:plural_1, count: 1)
111
- end
112
-
113
- test "subtrees enabled: looks up translations from the second chained backend" do
114
- setup_backend!
115
- assert_equal 'one', I18n.t(:plural_2, count: 1)
116
- end
117
-
118
- test "subtrees disabled: looks up translations from the second chained backend" do
119
- setup_backend!(false)
120
- assert_equal 'one', I18n.t(:plural_2, count: 1)
121
- end
122
- end if I18n::TestCase.key_value?
@@ -1,36 +0,0 @@
1
- require 'test_helper'
2
-
3
- class I18nBackendExceptionsTest < I18n::TestCase
4
- def setup
5
- super
6
- I18n.backend = I18n::Backend::Simple.new
7
- end
8
-
9
- test "throw message: MissingTranslation message from #translate includes the given scope and full key" do
10
- exception = catch(:exception) do
11
- I18n.t(:'baz.missing', :scope => :'foo.bar', :throw => true)
12
- end
13
- assert_equal "translation missing: en.foo.bar.baz.missing", exception.message
14
- end
15
-
16
- test "exceptions: MissingTranslationData message from #translate includes the given scope and full key" do
17
- begin
18
- I18n.t(:'baz.missing', :scope => :'foo.bar', :raise => true)
19
- rescue I18n::MissingTranslationData => exception
20
- end
21
- assert_equal "translation missing: en.foo.bar.baz.missing", exception.message
22
- end
23
-
24
- test "exceptions: MissingTranslationData message from #localize includes the given scope and full key" do
25
- begin
26
- I18n.l(Time.now, :format => :foo)
27
- rescue I18n::MissingTranslationData => exception
28
- end
29
- assert_equal "translation missing: en.time.formats.foo", exception.message
30
- end
31
-
32
- test "exceptions: MissingInterpolationArgument message includes missing key, provided keys and full string" do
33
- exception = I18n::MissingInterpolationArgument.new('key', {:this => 'was given'}, 'string')
34
- assert_equal 'missing interpolation argument "key" in "string" ({:this=>"was given"} given)', exception.message
35
- end
36
- end
@@ -1,219 +0,0 @@
1
- require 'test_helper'
2
-
3
- class I18nBackendFallbacksTranslateTest < I18n::TestCase
4
- class Backend < I18n::Backend::Simple
5
- include I18n::Backend::Fallbacks
6
- end
7
-
8
- def setup
9
- super
10
- I18n.backend = Backend.new
11
- store_translations(:en, :foo => 'Foo in :en', :bar => 'Bar in :en', :buz => 'Buz in :en', :interpolate => 'Interpolate %{value}')
12
- store_translations(:de, :bar => 'Bar in :de', :baz => 'Baz in :de')
13
- store_translations(:'de-DE', :baz => 'Baz in :de-DE')
14
- store_translations(:'pt-BR', :baz => 'Baz in :pt-BR')
15
- end
16
-
17
- test "still returns an existing translation as usual" do
18
- assert_equal 'Foo in :en', I18n.t(:foo, :locale => :en)
19
- assert_equal 'Bar in :de', I18n.t(:bar, :locale => :de)
20
- assert_equal 'Baz in :de-DE', I18n.t(:baz, :locale => :'de-DE')
21
- end
22
-
23
- test "returns interpolated value if no key provided" do
24
- assert_equal 'Interpolate %{value}', I18n.t(:interpolate)
25
- end
26
-
27
- test "returns the :en translation for a missing :de translation" do
28
- assert_equal 'Foo in :en', I18n.t(:foo, :locale => :de)
29
- end
30
-
31
- test "returns the :de translation for a missing :'de-DE' translation" do
32
- assert_equal 'Bar in :de', I18n.t(:bar, :locale => :'de-DE')
33
- end
34
-
35
- test "returns the :en translation for translation missing in both :de and :'de-De'" do
36
- assert_equal 'Buz in :en', I18n.t(:buz, :locale => :'de-DE')
37
- end
38
-
39
- test "returns the :de translation for a missing :'de-DE' when :default is a String" do
40
- assert_equal 'Bar in :de', I18n.t(:bar, :locale => :'de-DE', :default => "Default Bar")
41
- assert_equal "Default Bar", I18n.t(:missing_bar, :locale => :'de-DE', :default => "Default Bar")
42
- end
43
-
44
- test "returns the :de translation for a missing :'de-DE' when defaults is a Symbol (which exists in :en)" do
45
- assert_equal "Bar in :de", I18n.t(:bar, :locale => :'de-DE', :default => [:buz])
46
- end
47
-
48
- test "returns the :'de-DE' default :baz translation for a missing :'de-DE' (which exists in :de)" do
49
- assert_equal "Baz in :de-DE", I18n.t(:bar, :locale => :'de-DE', :default => [:baz])
50
- end
51
-
52
- test "returns the :de translation for a missing :'de-DE' when :default is a Proc" do
53
- assert_equal 'Bar in :de', I18n.t(:bar, :locale => :'de-DE', :default => Proc.new { "Default Bar" })
54
- assert_equal "Default Bar", I18n.t(:missing_bar, :locale => :'de-DE', :default => Proc.new { "Default Bar" })
55
- end
56
-
57
- test "returns the :de translation for a missing :'de-DE' when :default is a Hash" do
58
- assert_equal 'Bar in :de', I18n.t(:bar, :locale => :'de-DE', :default => {})
59
- assert_equal({}, I18n.t(:missing_bar, :locale => :'de-DE', :default => {}))
60
- end
61
-
62
- test "returns the :de translation for a missing :'de-DE' when :default is nil" do
63
- assert_equal 'Bar in :de', I18n.t(:bar, :locale => :'de-DE', :default => nil)
64
- assert_nil I18n.t(:missing_bar, :locale => :'de-DE', :default => nil)
65
- end
66
-
67
- test "returns the translation missing message if the default is also missing" do
68
- assert_equal 'translation missing: de-DE.missing_bar', I18n.t(:missing_bar, :locale => :'de-DE', :default => [:missing_baz])
69
- end
70
-
71
- test "returns the :'de-DE' default :baz translation for a missing :'de-DE' when defaults contains Symbol" do
72
- assert_equal 'Baz in :de-DE', I18n.t(:missing_foo, :locale => :'de-DE', :default => [:baz, "Default Bar"])
73
- end
74
-
75
- test "returns the defaults translation for a missing :'de-DE' when defaults contains a String or Proc before Symbol" do
76
- assert_equal "Default Bar", I18n.t(:missing_foo, :locale => :'de-DE', :default => [:missing_bar, "Default Bar", :baz])
77
- assert_equal "Default Bar", I18n.t(:missing_foo, :locale => :'de-DE', :default => [:missing_bar, Proc.new { "Default Bar" }, :baz])
78
- end
79
-
80
- test "returns the default translation for a missing :'de-DE' and existing :de when default is a Hash" do
81
- assert_equal 'Default 6 Bars', I18n.t(:missing_foo, :locale => :'de-DE', :default => [:missing_bar, {:other => "Default %{count} Bars"}, "Default Bar"], :count => 6)
82
- end
83
-
84
- test "returns the default translation for a missing :de translation even when default is a String when fallback is disabled" do
85
- assert_equal 'Default String', I18n.t(:foo, :locale => :de, :default => 'Default String', :fallback => false)
86
- end
87
-
88
- test "raises I18n::MissingTranslationData exception when fallback is disabled even when fallback translation exists" do
89
- assert_raise(I18n::MissingTranslationData) { I18n.t(:foo, :locale => :de, :fallback => false, :raise => true) }
90
- end
91
-
92
- test "raises I18n::MissingTranslationData exception when no translation was found" do
93
- assert_raise(I18n::MissingTranslationData) { I18n.t(:faa, :locale => :en, :raise => true) }
94
- assert_raise(I18n::MissingTranslationData) { I18n.t(:faa, :locale => :de, :raise => true) }
95
- end
96
-
97
- test "should ensure that default is not splitted on new line char" do
98
- assert_equal "Default \n Bar", I18n.t(:missing_bar, :default => "Default \n Bar")
99
- end
100
-
101
- test "should not raise error when enforce_available_locales is true, :'pt' is missing and default is a Symbol" do
102
- I18n.enforce_available_locales = true
103
- begin
104
- assert_equal 'Foo', I18n.t(:'model.attrs.foo', :locale => :'pt-BR', :default => [:'attrs.foo', "Foo"])
105
- ensure
106
- I18n.enforce_available_locales = false
107
- end
108
- end
109
- end
110
-
111
- class I18nBackendFallbacksLocalizeTest < I18n::TestCase
112
- class Backend < I18n::Backend::Simple
113
- include I18n::Backend::Fallbacks
114
- end
115
-
116
- def setup
117
- super
118
- I18n.backend = Backend.new
119
- store_translations(:en, :date => { :formats => { :en => 'en' }, :day_names => %w(Sunday) })
120
- store_translations(:de, :date => { :formats => { :de => 'de' } })
121
- end
122
-
123
- test "still uses an existing format as usual" do
124
- assert_equal 'en', I18n.l(Date.today, :format => :en, :locale => :en)
125
- end
126
-
127
- test "looks up and uses a fallback locale's format for a key missing in the given locale (1)" do
128
- assert_equal 'en', I18n.l(Date.today, :format => :en, :locale => :de)
129
- end
130
-
131
- test "looks up and uses a fallback locale's format for a key missing in the given locale (2)" do
132
- assert_equal 'de', I18n.l(Date.today, :format => :de, :locale => :'de-DE')
133
- end
134
-
135
- test "still uses an existing day name translation as usual" do
136
- assert_equal 'Sunday', I18n.l(Date.new(2010, 1, 3), :format => '%A', :locale => :en)
137
- end
138
-
139
- test "uses a fallback locale's translation for a key missing in the given locale" do
140
- assert_equal 'Sunday', I18n.l(Date.new(2010, 1, 3), :format => '%A', :locale => :de)
141
- end
142
- end
143
-
144
- class I18nBackendFallbacksWithChainTest < I18n::TestCase
145
- class Backend < I18n::Backend::Simple
146
- include I18n::Backend::Fallbacks
147
- end
148
-
149
- class Chain < I18n::Backend::Chain
150
- include I18n::Backend::Fallbacks
151
- end
152
-
153
- def setup
154
- super
155
- backend = Backend.new
156
- backend.store_translations(:de, :foo => 'FOO')
157
- backend.store_translations(:'pt-BR', :foo => 'Baz in :pt-BR')
158
- I18n.backend = Chain.new(I18n::Backend::Simple.new, backend)
159
- end
160
-
161
- test "falls back from de-DE to de when there is no translation for de-DE available" do
162
- assert_equal 'FOO', I18n.t(:foo, :locale => :'de-DE')
163
- end
164
-
165
- test "falls back from de-DE to de when there is no translation for de-DE available when using arrays, too" do
166
- assert_equal ['FOO', 'FOO'], I18n.t([:foo, :foo], :locale => :'de-DE')
167
- end
168
-
169
- test "should not raise error when enforce_available_locales is true, :'pt' is missing and default is a Symbol" do
170
- I18n.enforce_available_locales = true
171
- begin
172
- assert_equal 'Foo', I18n.t(:'model.attrs.foo', :locale => :'pt-BR', :default => [:'attrs.foo', "Foo"])
173
- ensure
174
- I18n.enforce_available_locales = false
175
- end
176
- end
177
- end
178
-
179
- class I18nBackendFallbacksExistsTest < I18n::TestCase
180
- class Backend < I18n::Backend::Simple
181
- include I18n::Backend::Fallbacks
182
- end
183
-
184
- def setup
185
- super
186
- I18n.backend = Backend.new
187
- store_translations(:en, :foo => 'Foo in :en', :bar => 'Bar in :en')
188
- store_translations(:de, :bar => 'Bar in :de')
189
- store_translations(:'de-DE', :baz => 'Baz in :de-DE')
190
- end
191
-
192
- test "exists? given an existing key will return true" do
193
- assert_equal true, I18n.exists?(:foo)
194
- end
195
-
196
- test "exists? given a non-existing key will return false" do
197
- assert_equal false, I18n.exists?(:bogus)
198
- end
199
-
200
- test "exists? given an existing key and an existing locale will return true" do
201
- assert_equal true, I18n.exists?(:foo, :en)
202
- assert_equal true, I18n.exists?(:bar, :de)
203
- end
204
-
205
- test "exists? given a non-existing key and an existing locale will return false" do
206
- assert_equal false, I18n.exists?(:bogus, :en)
207
- assert_equal false, I18n.exists?(:bogus, :de)
208
- end
209
-
210
- test "exists? should return true given a key which is missing from the given locale and exists in a fallback locale" do
211
- assert_equal true, I18n.exists?(:foo, :de)
212
- assert_equal true, I18n.exists?(:foo, :'de-DE')
213
- end
214
-
215
- test "exists? should return false given a key which is missing from the given locale and all its fallback locales" do
216
- assert_equal false, I18n.exists?(:baz, :de)
217
- assert_equal false, I18n.exists?(:bogus, :'de-DE')
218
- end
219
- end