i18n 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +4 -0
  3. data/lib/i18n/backend.rb +1 -0
  4. data/lib/i18n/backend/base.rb +12 -1
  5. data/lib/i18n/backend/cache_file.rb +36 -0
  6. data/lib/i18n/backend/chain.rb +22 -0
  7. data/lib/i18n/backend/key_value.rb +24 -0
  8. data/lib/i18n/backend/simple.rb +1 -1
  9. data/lib/i18n/config.rb +16 -0
  10. data/lib/i18n/core_ext/hash.rb +8 -0
  11. data/lib/i18n/interpolate/ruby.rb +5 -3
  12. data/lib/i18n/version.rb +1 -1
  13. metadata +16 -52
  14. data/gemfiles/Gemfile.rails-3.2.x +0 -10
  15. data/gemfiles/Gemfile.rails-4.0.x +0 -10
  16. data/gemfiles/Gemfile.rails-4.1.x +0 -10
  17. data/gemfiles/Gemfile.rails-4.2.x +0 -10
  18. data/gemfiles/Gemfile.rails-5.0.x +0 -10
  19. data/gemfiles/Gemfile.rails-5.1.x +0 -10
  20. data/gemfiles/Gemfile.rails-master +0 -10
  21. data/test/api/all_features_test.rb +0 -58
  22. data/test/api/cascade_test.rb +0 -28
  23. data/test/api/chain_test.rb +0 -24
  24. data/test/api/fallbacks_test.rb +0 -30
  25. data/test/api/key_value_test.rb +0 -24
  26. data/test/api/memoize_test.rb +0 -56
  27. data/test/api/override_test.rb +0 -42
  28. data/test/api/pluralization_test.rb +0 -30
  29. data/test/api/simple_test.rb +0 -28
  30. data/test/backend/cache_test.rb +0 -109
  31. data/test/backend/cascade_test.rb +0 -86
  32. data/test/backend/chain_test.rb +0 -122
  33. data/test/backend/exceptions_test.rb +0 -36
  34. data/test/backend/fallbacks_test.rb +0 -211
  35. data/test/backend/interpolation_compiler_test.rb +0 -118
  36. data/test/backend/key_value_test.rb +0 -85
  37. data/test/backend/memoize_test.rb +0 -79
  38. data/test/backend/metadata_test.rb +0 -48
  39. data/test/backend/pluralization_test.rb +0 -45
  40. data/test/backend/simple_test.rb +0 -111
  41. data/test/backend/transliterator_test.rb +0 -84
  42. data/test/core_ext/hash_test.rb +0 -36
  43. data/test/gettext/api_test.rb +0 -214
  44. data/test/gettext/backend_test.rb +0 -92
  45. data/test/i18n/exceptions_test.rb +0 -117
  46. data/test/i18n/gettext_plural_keys_test.rb +0 -20
  47. data/test/i18n/interpolate_test.rb +0 -91
  48. data/test/i18n/load_path_test.rb +0 -34
  49. data/test/i18n/middleware_test.rb +0 -24
  50. data/test/i18n_test.rb +0 -462
  51. data/test/locale/fallbacks_test.rb +0 -133
  52. data/test/locale/tag/rfc4646_test.rb +0 -143
  53. data/test/locale/tag/simple_test.rb +0 -32
  54. data/test/run_all.rb +0 -20
  55. data/test/test_data/locales/de.po +0 -82
  56. data/test/test_data/locales/en.rb +0 -3
  57. data/test/test_data/locales/en.yaml +0 -3
  58. data/test/test_data/locales/en.yml +0 -3
  59. data/test/test_data/locales/invalid/empty.yml +0 -0
  60. data/test/test_data/locales/invalid/syntax.yml +0 -4
  61. data/test/test_data/locales/plurals.rb +0 -113
  62. data/test/test_helper.rb +0 -61
@@ -1,85 +0,0 @@
1
- require 'test_helper'
2
-
3
- class I18nBackendKeyValueTest < I18n::TestCase
4
- def setup_backend!(subtree=true)
5
- I18n.backend = I18n::Backend::KeyValue.new({}, subtree)
6
- store_translations(:en, :foo => { :bar => 'bar', :baz => 'baz' })
7
- end
8
-
9
- def assert_flattens(expected, nested, escape=true, subtree=true)
10
- assert_equal expected, I18n.backend.flatten_translations("en", nested, escape, subtree)
11
- end
12
-
13
- test "hash flattening works" do
14
- setup_backend!
15
- assert_flattens(
16
- {:a=>'a', :b=>{:c=>'c', :d=>'d', :f=>{:x=>'x'}}, :"b.f" => {:x=>"x"}, :"b.c"=>"c", :"b.f.x"=>"x", :"b.d"=>"d"},
17
- {:a=>'a', :b=>{:c=>'c', :d=>'d', :f=>{:x=>'x'}}}
18
- )
19
- assert_flattens({:a=>{:b =>['a', 'b']}, :"a.b"=>['a', 'b']}, {:a=>{:b =>['a', 'b']}})
20
- assert_flattens({:"a\001b" => "c"}, {:"a.b" => "c"})
21
- assert_flattens({:"a.b"=>['a', 'b']}, {:a=>{:b =>['a', 'b']}}, true, false)
22
- assert_flattens({:"a.b" => "c"}, {:"a.b" => "c"}, false)
23
- end
24
-
25
- test "store_translations handle subtrees by default" do
26
- setup_backend!
27
- assert_equal({ :bar => 'bar', :baz => 'baz' }, I18n.t("foo"))
28
- end
29
-
30
- test "store_translations merge subtrees accordingly" do
31
- setup_backend!
32
- store_translations(:en, :foo => { :baz => "BAZ"})
33
- assert_equal('BAZ', I18n.t("foo.baz"))
34
- assert_equal({ :bar => 'bar', :baz => 'BAZ' }, I18n.t("foo"))
35
- end
36
-
37
- test "store_translations does not handle subtrees if desired" do
38
- setup_backend!(false)
39
- assert_raise I18n::MissingTranslationData do
40
- I18n.t("foo", :raise => true)
41
- end
42
- end
43
-
44
- test "subtrees enabled: given incomplete pluralization data it raises I18n::InvalidPluralizationData" do
45
- setup_backend!
46
- store_translations(:en, :bar => { :one => "One" })
47
- assert_raise(I18n::InvalidPluralizationData) { I18n.t(:bar, :count => 2) }
48
- end
49
-
50
- test "subtrees disabled: given incomplete pluralization data it returns an error message" do
51
- setup_backend!(false)
52
- store_translations(:en, :bar => { :one => "One" })
53
- assert_equal "translation missing: en.bar", I18n.t(:bar, :count => 2)
54
- end
55
-
56
- test "translate handles subtrees for pluralization" do
57
- setup_backend!(false)
58
- store_translations(:en, :bar => { :one => "One" })
59
- assert_equal("One", I18n.t("bar", :count => 1))
60
- end
61
-
62
- test "subtrees enabled: returns localized string given missing pluralization data" do
63
- setup_backend!(true)
64
- assert_equal 'bar', I18n.t("foo.bar", count: 1)
65
- end
66
-
67
- test "subtrees disabled: returns localized string given missing pluralization data" do
68
- setup_backend!(false)
69
- assert_equal 'bar', I18n.t("foo.bar", count: 1)
70
- end
71
-
72
- test "subtrees enabled: Returns fallback default given missing pluralization data" do
73
- setup_backend!(true)
74
- I18n.backend.extend I18n::Backend::Fallbacks
75
- assert_equal 'default', I18n.t(:missing_bar, count: 1, default: 'default')
76
- assert_equal 'default', I18n.t(:missing_bar, count: 0, default: 'default')
77
- end
78
-
79
- test "subtrees disabled: Returns fallback default given missing pluralization data" do
80
- setup_backend!(false)
81
- I18n.backend.extend I18n::Backend::Fallbacks
82
- assert_equal 'default', I18n.t(:missing_bar, count: 1, default: 'default')
83
- assert_equal 'default', I18n.t(:missing_bar, count: 0, default: 'default')
84
- end
85
- end if I18n::TestCase.key_value?
@@ -1,79 +0,0 @@
1
- require 'test_helper'
2
- # TODO: change back to "require 'backend/simple'" when dropping support to Ruby 1.8.7.
3
- require File.expand_path('../simple_test', __FILE__)
4
-
5
- class I18nBackendMemoizeTest < I18nBackendSimpleTest
6
- module MemoizeSpy
7
- attr_accessor :spy_calls
8
-
9
- def available_locales
10
- self.spy_calls = (self.spy_calls || 0) + 1
11
- super
12
- end
13
- end
14
-
15
- class MemoizeBackend < I18n::Backend::Simple
16
- include MemoizeSpy
17
- include I18n::Backend::Memoize
18
- end
19
-
20
- def setup
21
- super
22
- I18n.backend = MemoizeBackend.new
23
- end
24
-
25
- def test_memoizes_available_locales
26
- I18n.backend.spy_calls = 0
27
- assert_equal I18n.available_locales, I18n.available_locales
28
- assert_equal 1, I18n.backend.spy_calls
29
- end
30
-
31
- def test_resets_available_locales_on_reload!
32
- I18n.available_locales
33
- I18n.backend.spy_calls = 0
34
- I18n.reload!
35
- assert_equal I18n.available_locales, I18n.available_locales
36
- assert_equal 1, I18n.backend.spy_calls
37
- end
38
-
39
- def test_resets_available_locales_on_store_translations
40
- I18n.available_locales
41
- I18n.backend.spy_calls = 0
42
- I18n.backend.store_translations(:copa, :ca => :bana)
43
- assert_equal I18n.available_locales, I18n.available_locales
44
- assert I18n.available_locales.include?(:copa)
45
- assert_equal 1, I18n.backend.spy_calls
46
- end
47
-
48
- module TestLookup
49
- def lookup(locale, key, scope = [], options = {})
50
- keys = I18n.normalize_keys(locale, key, scope, options[:separator])
51
- keys.inspect
52
- end
53
- end
54
-
55
- def test_lookup_concurrent_consistency
56
- backend_impl = Class.new(I18n::Backend::Simple) do
57
- include TestLookup
58
- include I18n::Backend::Memoize
59
- end
60
- backend = backend_impl.new
61
-
62
- memoized_lookup = backend.send(:memoized_lookup)
63
-
64
- assert_equal "[:foo, :scoped, :sample]", backend.translate('foo', scope = [:scoped, :sample])
65
-
66
- results = []
67
- 30.times.inject([]) do |memo, i|
68
- memo << Thread.new do
69
- backend.translate('bar', scope); backend.translate(:baz, scope)
70
- end
71
- end.each(&:join)
72
-
73
- memoized_lookup = backend.send(:memoized_lookup)
74
- puts memoized_lookup.inspect if $VERBOSE
75
- assert_equal 3, memoized_lookup.size, "NON-THREAD-SAFE lookup memoization backend: #{memoized_lookup.class}"
76
- # if a plain Hash is used might eventually end up in a weird (inconsistent) state
77
- end
78
-
79
- end
@@ -1,48 +0,0 @@
1
- require 'test_helper'
2
-
3
- class I18nBackendMetadataTest < I18n::TestCase
4
- class Backend < I18n::Backend::Simple
5
- include I18n::Backend::Metadata
6
- end
7
-
8
- def setup
9
- super
10
- I18n.backend = Backend.new
11
- store_translations(:en, :foo => 'Hi %{name}')
12
- end
13
-
14
- test "translation strings carry metadata" do
15
- translation = I18n.t(:foo, :name => 'David')
16
- assert translation.respond_to?(:translation_metadata)
17
- assert translation.translation_metadata.is_a?(Hash)
18
- end
19
-
20
- test "translate adds the locale to metadata on Strings" do
21
- assert_equal :en, I18n.t(:foo, :name => 'David', :locale => :en).translation_metadata[:locale]
22
- end
23
-
24
- test "translate adds the key to metadata on Strings" do
25
- assert_equal :foo, I18n.t(:foo, :name => 'David').translation_metadata[:key]
26
- end
27
-
28
- test "translate adds the default to metadata on Strings" do
29
- assert_equal 'bar', I18n.t(:foo, :default => 'bar', :name => '').translation_metadata[:default]
30
- end
31
-
32
- test "translation adds the interpolation values to metadata on Strings" do
33
- assert_equal({:name => 'David'}, I18n.t(:foo, :name => 'David').translation_metadata[:values])
34
- end
35
-
36
- test "interpolation adds the original string to metadata on Strings" do
37
- assert_equal('Hi %{name}', I18n.t(:foo, :name => 'David').translation_metadata[:original])
38
- end
39
-
40
- test "pluralization adds the count to metadata on Strings" do
41
- assert_equal(1, I18n.t(:missing, :count => 1, :default => { :one => 'foo' }).translation_metadata[:count])
42
- end
43
-
44
- test "metadata works with frozen values" do
45
- assert_equal(1, I18n.t(:missing, :count => 1, :default => 'foo'.freeze).translation_metadata[:count])
46
- end
47
- end
48
-
@@ -1,45 +0,0 @@
1
- require 'test_helper'
2
-
3
- class I18nBackendPluralizationTest < I18n::TestCase
4
- class Backend < I18n::Backend::Simple
5
- include I18n::Backend::Pluralization
6
- include I18n::Backend::Fallbacks
7
- end
8
-
9
- def setup
10
- super
11
- I18n.backend = Backend.new
12
- @rule = lambda { |n| n == 1 ? :one : n == 0 || (2..10).include?(n % 100) ? :few : (11..19).include?(n % 100) ? :many : :other }
13
- store_translations(:xx, :i18n => { :plural => { :rule => @rule } })
14
- @entry = { :zero => 'zero', :one => 'one', :few => 'few', :many => 'many', :other => 'other' }
15
- end
16
-
17
- test "pluralization picks a pluralizer from :'i18n.pluralize'" do
18
- assert_equal @rule, I18n.backend.send(:pluralizer, :xx)
19
- end
20
-
21
- test "pluralization picks :one for 1" do
22
- assert_equal 'one', I18n.t(:count => 1, :default => @entry, :locale => :xx)
23
- end
24
-
25
- test "pluralization picks :few for 2" do
26
- assert_equal 'few', I18n.t(:count => 2, :default => @entry, :locale => :xx)
27
- end
28
-
29
- test "pluralization picks :many for 11" do
30
- assert_equal 'many', I18n.t(:count => 11, :default => @entry, :locale => :xx)
31
- end
32
-
33
- test "pluralization picks zero for 0 if the key is contained in the data" do
34
- assert_equal 'zero', I18n.t(:count => 0, :default => @entry, :locale => :xx)
35
- end
36
-
37
- test "pluralization picks few for 0 if the key is not contained in the data" do
38
- @entry.delete(:zero)
39
- assert_equal 'few', I18n.t(:count => 0, :default => @entry, :locale => :xx)
40
- end
41
-
42
- test "Fallbacks can pick up rules from fallback locales, too" do
43
- assert_equal @rule, I18n.backend.send(:pluralizer, :'xx-XX')
44
- end
45
- end
@@ -1,111 +0,0 @@
1
- require 'test_helper'
2
-
3
- class I18nBackendSimpleTest < I18n::TestCase
4
- def setup
5
- super
6
- I18n.backend = I18n::Backend::Simple.new
7
- I18n.load_path = [locales_dir + '/en.yml']
8
- end
9
-
10
- # useful because this way we can use the backend with no key for interpolation/pluralization
11
- test "simple backend translate: given nil as a key it still interpolations the default value" do
12
- assert_equal "Hi David", I18n.t(nil, :default => "Hi %{name}", :name => "David")
13
- end
14
-
15
- # loading translations
16
- test "simple load_translations: given an unknown file type it raises I18n::UnknownFileType" do
17
- assert_raise(I18n::UnknownFileType) { I18n.backend.load_translations("#{locales_dir}/en.xml") }
18
- end
19
-
20
- test "simple load_translations: given a YAML file name with yaml extension does not raise anything" do
21
- assert_nothing_raised { I18n.backend.load_translations("#{locales_dir}/en.yaml") }
22
- end
23
-
24
- test "simple load_translations: given a Ruby file name it does not raise anything" do
25
- assert_nothing_raised { I18n.backend.load_translations("#{locales_dir}/en.rb") }
26
- end
27
-
28
- test "simple load_translations: given no argument, it uses I18n.load_path" do
29
- I18n.backend.load_translations
30
- assert_equal({ :en => { :foo => { :bar => 'baz' } } }, I18n.backend.send(:translations))
31
- end
32
-
33
- test "simple load_rb: loads data from a Ruby file" do
34
- data = I18n.backend.send(:load_rb, "#{locales_dir}/en.rb")
35
- assert_equal({ :en => { :fuh => { :bah => 'bas' } } }, data)
36
- end
37
-
38
- test "simple load_yml: loads data from a YAML file" do
39
- data = I18n.backend.send(:load_yml, "#{locales_dir}/en.yml")
40
- assert_equal({ 'en' => { 'foo' => { 'bar' => 'baz' } } }, data)
41
- end
42
-
43
- test "simple load_translations: loads data from known file formats" do
44
- I18n.backend = I18n::Backend::Simple.new
45
- I18n.backend.load_translations("#{locales_dir}/en.rb", "#{locales_dir}/en.yml")
46
- expected = { :en => { :fuh => { :bah => "bas" }, :foo => { :bar => "baz" } } }
47
- assert_equal expected, translations
48
- end
49
-
50
- test "simple load_translations: given file names as array it does not raise anything" do
51
- assert_nothing_raised { I18n.backend.load_translations(["#{locales_dir}/en.rb", "#{locales_dir}/en.yml"]) }
52
- end
53
-
54
- # storing translations
55
-
56
- test "simple store_translations: stores translations, ... no, really :-)" do
57
- store_translations :'en', :foo => 'bar'
58
- assert_equal Hash[:'en', {:foo => 'bar'}], translations
59
- end
60
-
61
- test "simple store_translations: deep_merges with existing translations" do
62
- store_translations :'en', :foo => {:bar => 'bar'}
63
- store_translations :'en', :foo => {:baz => 'baz'}
64
- assert_equal Hash[:'en', {:foo => {:bar => 'bar', :baz => 'baz'}}], translations
65
- end
66
-
67
- test "simple store_translations: converts the given locale to a Symbol" do
68
- store_translations 'en', :foo => 'bar'
69
- assert_equal Hash[:'en', {:foo => 'bar'}], translations
70
- end
71
-
72
- test "simple store_translations: converts keys to Symbols" do
73
- store_translations 'en', 'foo' => {'bar' => 'bar', 'baz' => 'baz'}
74
- assert_equal Hash[:'en', {:foo => {:bar => 'bar', :baz => 'baz'}}], translations
75
- end
76
-
77
- test "simple store_translations: do not store translations unavailable locales if enforce_available_locales is true" do
78
- begin
79
- I18n.enforce_available_locales = true
80
- I18n.available_locales = [:en, :es]
81
- store_translations(:fr, :foo => {:bar => 'barfr', :baz => 'bazfr'})
82
- store_translations(:es, :foo => {:bar => 'bares', :baz => 'bazes'})
83
- assert_nil translations[:fr]
84
- assert_equal Hash[:foo, {:bar => 'bares', :baz => 'bazes'}], translations[:es]
85
- ensure
86
- I18n.config.enforce_available_locales = false
87
- end
88
- end
89
-
90
- test "simple store_translations: store translations for unavailable locales if enforce_available_locales is false" do
91
- I18n.available_locales = [:en, :es]
92
- store_translations(:fr, :foo => {:bar => 'barfr', :baz => 'bazfr'})
93
- assert_equal Hash[:foo, {:bar => 'barfr', :baz => 'bazfr'}], translations[:fr]
94
- end
95
-
96
- # reloading translations
97
-
98
- test "simple reload_translations: unloads translations" do
99
- I18n.backend.reload!
100
- assert_nil translations
101
- end
102
-
103
- test "simple reload_translations: uninitializes the backend" do
104
- I18n.backend.reload!
105
- assert_equal false, I18n.backend.initialized?
106
- end
107
-
108
- test "returns localized string given missing pluralization data" do
109
- assert_equal 'baz', I18n.t('foo.bar', count: 1)
110
- end
111
- end
@@ -1,84 +0,0 @@
1
- # encoding: utf-8
2
- require 'test_helper'
3
-
4
- class I18nBackendTransliterator < I18n::TestCase
5
- def setup
6
- super
7
- I18n.backend = I18n::Backend::Simple.new
8
- @proc = lambda { |n| n.upcase }
9
- @hash = { "ü" => "ue", "ö" => "oe", "a" => "a" }
10
- @transliterator = I18n::Backend::Transliterator.get
11
- end
12
-
13
- test "transliteration rule can be a proc" do
14
- store_translations(:xx, :i18n => {:transliterate => {:rule => @proc}})
15
- assert_equal "HELLO", I18n.backend.transliterate(:xx, "hello")
16
- end
17
-
18
- test "transliteration rule can be a hash" do
19
- store_translations(:xx, :i18n => {:transliterate => {:rule => @hash}})
20
- assert_equal "ue", I18n.backend.transliterate(:xx, "ü")
21
- end
22
-
23
- test "transliteration rule must be a proc or hash" do
24
- store_translations(:xx, :i18n => {:transliterate => {:rule => ""}})
25
- assert_raise I18n::ArgumentError do
26
- I18n.backend.transliterate(:xx, "ü")
27
- end
28
- end
29
-
30
- test "transliterator defaults to latin => ascii when no rule is given" do
31
- assert_equal "AEroskobing", I18n.backend.transliterate(:xx, "Ærøskøbing")
32
- end
33
-
34
- test "default transliterator should not modify ascii characters" do
35
- (0..127).each do |byte|
36
- char = [byte].pack("U")
37
- assert_equal char, @transliterator.transliterate(char)
38
- end
39
- end
40
-
41
- test "default transliterator correctly transliterates latin characters" do
42
- # create string with range of Unicode's western characters with
43
- # diacritics, excluding the division and multiplication signs which for
44
- # some reason or other are floating in the middle of all the letters.
45
- string = (0xC0..0x17E).to_a.reject {|c| [0xD7, 0xF7].include? c}.pack("U*")
46
- string.split(//) do |char|
47
- assert_match %r{^[a-zA-Z']*$}, @transliterator.transliterate(string)
48
- end
49
- end
50
-
51
- test "should replace non-ASCII chars not in map with a replacement char" do
52
- assert_equal "abc?", @transliterator.transliterate("abcſ")
53
- end
54
-
55
- test "can replace non-ASCII chars not in map with a custom replacement string" do
56
- assert_equal "abc#", @transliterator.transliterate("abcſ", "#")
57
- end
58
-
59
- test "default transliterator raises errors for invalid UTF-8" do
60
- assert_raise ArgumentError do
61
- @transliterator.transliterate("a\x92b")
62
- end
63
- end
64
-
65
- test "I18n.transliterate should transliterate using a default transliterator" do
66
- assert_equal "aeo", I18n.transliterate("áèö")
67
- end
68
-
69
- test "I18n.transliterate should transliterate using a locale" do
70
- store_translations(:xx, :i18n => {:transliterate => {:rule => @hash}})
71
- assert_equal "ue", I18n.transliterate("ü", :locale => :xx)
72
- end
73
-
74
- test "default transliterator fails with custom rules with uncomposed input" do
75
- char = [117, 776].pack("U*") # "ü" as ASCII "u" plus COMBINING DIAERESIS
76
- transliterator = I18n::Backend::Transliterator.get(@hash)
77
- assert_not_equal "ue", transliterator.transliterate(char)
78
- end
79
-
80
- test "DEFAULT_APPROXIMATIONS is frozen to prevent concurrency issues" do
81
- assert I18n::Backend::Transliterator::HashTransliterator::DEFAULT_APPROXIMATIONS.frozen?
82
- end
83
-
84
- end