i18n 0.9.5 → 1.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (97) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +71 -32
  3. data/lib/i18n/backend/base.rb +75 -31
  4. data/lib/i18n/backend/cache.rb +10 -11
  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 +37 -6
  8. data/lib/i18n/backend/fallbacks.rb +43 -14
  9. data/lib/i18n/backend/flatten.rb +10 -5
  10. data/lib/i18n/backend/gettext.rb +4 -2
  11. data/lib/i18n/backend/interpolation_compiler.rb +3 -1
  12. data/lib/i18n/backend/key_value.rb +31 -4
  13. data/lib/i18n/backend/lazy_loadable.rb +184 -0
  14. data/lib/i18n/backend/memoize.rb +10 -2
  15. data/lib/i18n/backend/metadata.rb +5 -3
  16. data/lib/i18n/backend/pluralization.rb +3 -1
  17. data/lib/i18n/backend/simple.rb +29 -16
  18. data/lib/i18n/backend/transliterator.rb +2 -0
  19. data/lib/i18n/backend.rb +5 -1
  20. data/lib/i18n/config.rb +20 -2
  21. data/lib/i18n/exceptions.rb +60 -17
  22. data/lib/i18n/gettext/helpers.rb +4 -2
  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 +8 -6
  26. data/lib/i18n/locale/fallbacks.rb +21 -20
  27. data/lib/i18n/locale/tag/parents.rb +8 -6
  28. data/lib/i18n/locale/tag/simple.rb +1 -1
  29. data/lib/i18n/locale.rb +2 -0
  30. data/lib/i18n/middleware.rb +2 -0
  31. data/lib/i18n/tests/basics.rb +3 -5
  32. data/lib/i18n/tests/defaults.rb +1 -1
  33. data/lib/i18n/tests/interpolation.rb +12 -7
  34. data/lib/i18n/tests/link.rb +11 -1
  35. data/lib/i18n/tests/localization/date.rb +32 -10
  36. data/lib/i18n/tests/localization/date_time.rb +28 -7
  37. data/lib/i18n/tests/localization/procs.rb +7 -5
  38. data/lib/i18n/tests/localization/time.rb +27 -5
  39. data/lib/i18n/tests/lookup.rb +4 -4
  40. data/lib/i18n/tests/pluralization.rb +1 -1
  41. data/lib/i18n/tests/procs.rb +12 -1
  42. data/lib/i18n/tests.rb +2 -0
  43. data/lib/i18n/utils.rb +55 -0
  44. data/lib/i18n/version.rb +3 -1
  45. data/lib/i18n.rb +123 -50
  46. metadata +16 -61
  47. data/gemfiles/Gemfile.rails-3.2.x +0 -10
  48. data/gemfiles/Gemfile.rails-4.0.x +0 -10
  49. data/gemfiles/Gemfile.rails-4.1.x +0 -10
  50. data/gemfiles/Gemfile.rails-4.2.x +0 -10
  51. data/gemfiles/Gemfile.rails-5.0.x +0 -10
  52. data/gemfiles/Gemfile.rails-5.1.x +0 -10
  53. data/gemfiles/Gemfile.rails-master +0 -10
  54. data/lib/i18n/core_ext/hash.rb +0 -29
  55. data/lib/i18n/core_ext/kernel/suppress_warnings.rb +0 -8
  56. data/lib/i18n/core_ext/string/interpolate.rb +0 -9
  57. data/test/api/all_features_test.rb +0 -58
  58. data/test/api/cascade_test.rb +0 -28
  59. data/test/api/chain_test.rb +0 -24
  60. data/test/api/fallbacks_test.rb +0 -30
  61. data/test/api/key_value_test.rb +0 -24
  62. data/test/api/memoize_test.rb +0 -56
  63. data/test/api/override_test.rb +0 -42
  64. data/test/api/pluralization_test.rb +0 -30
  65. data/test/api/simple_test.rb +0 -28
  66. data/test/backend/cache_test.rb +0 -109
  67. data/test/backend/cascade_test.rb +0 -86
  68. data/test/backend/chain_test.rb +0 -122
  69. data/test/backend/exceptions_test.rb +0 -36
  70. data/test/backend/fallbacks_test.rb +0 -219
  71. data/test/backend/interpolation_compiler_test.rb +0 -118
  72. data/test/backend/key_value_test.rb +0 -61
  73. data/test/backend/memoize_test.rb +0 -79
  74. data/test/backend/metadata_test.rb +0 -48
  75. data/test/backend/pluralization_test.rb +0 -45
  76. data/test/backend/simple_test.rb +0 -103
  77. data/test/backend/transliterator_test.rb +0 -84
  78. data/test/core_ext/hash_test.rb +0 -36
  79. data/test/gettext/api_test.rb +0 -214
  80. data/test/gettext/backend_test.rb +0 -92
  81. data/test/i18n/exceptions_test.rb +0 -117
  82. data/test/i18n/gettext_plural_keys_test.rb +0 -20
  83. data/test/i18n/interpolate_test.rb +0 -91
  84. data/test/i18n/load_path_test.rb +0 -34
  85. data/test/i18n/middleware_test.rb +0 -24
  86. data/test/i18n_test.rb +0 -462
  87. data/test/locale/fallbacks_test.rb +0 -133
  88. data/test/locale/tag/rfc4646_test.rb +0 -143
  89. data/test/locale/tag/simple_test.rb +0 -32
  90. data/test/run_all.rb +0 -20
  91. data/test/test_data/locales/de.po +0 -82
  92. data/test/test_data/locales/en.rb +0 -3
  93. data/test/test_data/locales/en.yml +0 -3
  94. data/test/test_data/locales/invalid/empty.yml +0 -0
  95. data/test/test_data/locales/invalid/syntax.yml +0 -4
  96. data/test/test_data/locales/plurals.rb +0 -113
  97. data/test/test_helper.rb +0 -61
@@ -1,117 +0,0 @@
1
- require 'test_helper'
2
-
3
- class I18nExceptionsTest < I18n::TestCase
4
- def test_invalid_locale_stores_locale
5
- force_invalid_locale
6
- rescue I18n::ArgumentError => exception
7
- assert_nil exception.locale
8
- end
9
-
10
- test "passing an invalid locale raises an InvalidLocale exception" do
11
- force_invalid_locale do |exception|
12
- assert_equal 'nil is not a valid locale', exception.message
13
- end
14
- end
15
-
16
- test "MissingTranslation can be initialized without options" do
17
- exception = I18n::MissingTranslation.new(:en, 'foo')
18
- assert_equal({}, exception.options)
19
- end
20
-
21
- test "MissingTranslationData exception stores locale, key and options" do
22
- force_missing_translation_data do |exception|
23
- assert_equal 'de', exception.locale
24
- assert_equal :foo, exception.key
25
- assert_equal({:scope => :bar}, exception.options)
26
- end
27
- end
28
-
29
- test "MissingTranslationData message contains the locale and scoped key" do
30
- force_missing_translation_data do |exception|
31
- assert_equal 'translation missing: de.bar.foo', exception.message
32
- end
33
- end
34
-
35
- test "InvalidPluralizationData stores entry, count and key" do
36
- force_invalid_pluralization_data do |exception|
37
- assert_equal({:other => "bar"}, exception.entry)
38
- assert_equal 1, exception.count
39
- assert_equal :one, exception.key
40
- end
41
- end
42
-
43
- test "InvalidPluralizationData message contains count, data and missing key" do
44
- force_invalid_pluralization_data do |exception|
45
- assert_match '1', exception.message
46
- assert_match '{:other=>"bar"}', exception.message
47
- assert_match 'one', exception.message
48
- end
49
- end
50
-
51
- test "MissingInterpolationArgument stores key and string" do
52
- assert_raise(I18n::MissingInterpolationArgument) { force_missing_interpolation_argument }
53
- force_missing_interpolation_argument do |exception|
54
- assert_equal :bar, exception.key
55
- assert_equal "%{bar}", exception.string
56
- end
57
- end
58
-
59
- test "MissingInterpolationArgument message contains the missing and given arguments" do
60
- force_missing_interpolation_argument do |exception|
61
- assert_equal 'missing interpolation argument :bar in "%{bar}" ({:baz=>"baz"} given)', exception.message
62
- end
63
- end
64
-
65
- test "ReservedInterpolationKey stores key and string" do
66
- force_reserved_interpolation_key do |exception|
67
- assert_equal :scope, exception.key
68
- assert_equal "%{scope}", exception.string
69
- end
70
- end
71
-
72
- test "ReservedInterpolationKey message contains the reserved key" do
73
- force_reserved_interpolation_key do |exception|
74
- assert_equal 'reserved key :scope used in "%{scope}"', exception.message
75
- end
76
- end
77
-
78
- test "MissingTranslationData#new can be initialized with just two arguments" do
79
- assert I18n::MissingTranslationData.new('en', 'key')
80
- end
81
-
82
- private
83
-
84
- def force_invalid_locale
85
- I18n.translate(:foo, :locale => nil)
86
- rescue I18n::ArgumentError => e
87
- block_given? ? yield(e) : raise(e)
88
- end
89
-
90
- def force_missing_translation_data(options = {})
91
- store_translations('de', :bar => nil)
92
- I18n.translate(:foo, options.merge(:scope => :bar, :locale => :de))
93
- rescue I18n::ArgumentError => e
94
- block_given? ? yield(e) : raise(e)
95
- end
96
-
97
- def force_invalid_pluralization_data
98
- store_translations('de', :foo => { :other => 'bar' })
99
- I18n.translate(:foo, :count => 1, :locale => :de)
100
- rescue I18n::ArgumentError => e
101
- block_given? ? yield(e) : raise(e)
102
- end
103
-
104
- def force_missing_interpolation_argument
105
- store_translations('de', :foo => "%{bar}")
106
- I18n.translate(:foo, :baz => 'baz', :locale => :de)
107
- rescue I18n::ArgumentError => e
108
- block_given? ? yield(e) : raise(e)
109
- end
110
-
111
- def force_reserved_interpolation_key
112
- store_translations('de', :foo => "%{scope}")
113
- I18n.translate(:foo, :baz => 'baz', :locale => :de)
114
- rescue I18n::ArgumentError => e
115
- block_given? ? yield(e) : raise(e)
116
- end
117
- end
@@ -1,20 +0,0 @@
1
- require 'test_helper'
2
-
3
- class I18nGettextPluralKeysTest < I18n::TestCase
4
- def setup
5
- super
6
- I18n::Gettext.plural_keys[:zz] = [:value1, :value2]
7
- end
8
-
9
- test "Returns the plural keys of the given locale if present" do
10
- assert_equal I18n::Gettext.plural_keys(:zz), [:value1, :value2]
11
- end
12
-
13
- test "Returns the plural keys of :en if given locale not present" do
14
- assert_equal I18n::Gettext.plural_keys(:yy), [:one, :other]
15
- end
16
-
17
- test "Returns the whole hash with no arguments" do
18
- assert_equal I18n::Gettext.plural_keys, { :en => [:one, :other], :zz => [:value1, :value2] }
19
- end
20
- end
@@ -1,91 +0,0 @@
1
- require 'test_helper'
2
-
3
- # thanks to Masao's String extensions, some tests taken from Masao's tests
4
- # http://github.com/mutoh/gettext/blob/edbbe1fa8238fa12c7f26f2418403015f0270e47/test/test_string.rb
5
-
6
- class I18nInterpolateTest < I18n::TestCase
7
- test "String interpolates a hash argument w/ named placeholders" do
8
- assert_equal "Masao Mutoh", I18n.interpolate("%{first} %{last}", :first => 'Masao', :last => 'Mutoh' )
9
- end
10
-
11
- test "String interpolates a hash argument w/ named placeholders (reverse order)" do
12
- assert_equal "Mutoh, Masao", I18n.interpolate("%{last}, %{first}", :first => 'Masao', :last => 'Mutoh' )
13
- end
14
-
15
- test "String interpolates named placeholders with sprintf syntax" do
16
- assert_equal "10, 43.4", I18n.interpolate("%<integer>d, %<float>.1f", :integer => 10, :float => 43.4)
17
- end
18
-
19
- test "String interpolates named placeholders with sprintf syntax, does not recurse" do
20
- assert_equal "%<not_translated>s", I18n.interpolate("%{msg}", :msg => '%<not_translated>s', :not_translated => 'should not happen' )
21
- end
22
-
23
- test "String interpolation does not replace anything when no placeholders are given" do
24
- assert_equal "aaa", I18n.interpolate("aaa", :num => 1)
25
- end
26
-
27
- test "String interpolation sprintf behaviour equals Ruby 1.9 behaviour" do
28
- assert_equal "1", I18n.interpolate("%<num>d", :num => 1)
29
- assert_equal "0b1", I18n.interpolate("%<num>#b", :num => 1)
30
- assert_equal "foo", I18n.interpolate("%<msg>s", :msg => "foo")
31
- assert_equal "1.000000", I18n.interpolate("%<num>f", :num => 1.0)
32
- assert_equal " 1", I18n.interpolate("%<num>3.0f", :num => 1.0)
33
- assert_equal "100.00", I18n.interpolate("%<num>2.2f", :num => 100.0)
34
- assert_equal "0x64", I18n.interpolate("%<num>#x", :num => 100.0)
35
- assert_raise(ArgumentError) { I18n.interpolate("%<num>,d", :num => 100) }
36
- assert_raise(ArgumentError) { I18n.interpolate("%<num>/d", :num => 100) }
37
- end
38
-
39
- test "String interpolation raises an I18n::MissingInterpolationArgument when the string has extra placeholders" do
40
- assert_raise(I18n::MissingInterpolationArgument) do # Ruby 1.9 msg: "key not found"
41
- I18n.interpolate("%{first} %{last}", :first => 'Masao')
42
- end
43
- end
44
-
45
- test "String interpolation does not raise when extra values were passed" do
46
- assert_nothing_raised do
47
- assert_equal "Masao Mutoh", I18n.interpolate("%{first} %{last}", :first => 'Masao', :last => 'Mutoh', :salutation => 'Mr.' )
48
- end
49
- end
50
-
51
- test "% acts as escape character in String interpolation" do
52
- assert_equal "%{first}", I18n.interpolate("%%{first}", :first => 'Masao')
53
- assert_equal "% 1", I18n.interpolate("%% %<num>d", :num => 1.0)
54
- assert_equal "%{num} %<num>d", I18n.interpolate("%%{num} %%<num>d", :num => 1)
55
- end
56
-
57
- def test_sprintf_mix_unformatted_and_formatted_named_placeholders
58
- assert_equal "foo 1.000000", I18n.interpolate("%{name} %<num>f", :name => "foo", :num => 1.0)
59
- end
60
-
61
- class RailsSafeBuffer < String
62
-
63
- def gsub(*args, &block)
64
- to_str.gsub(*args, &block)
65
- end
66
-
67
- end
68
- test "with String subclass that redefined gsub method" do
69
- assert_equal "Hello mars world", I18n.interpolate(RailsSafeBuffer.new("Hello %{planet} world"), :planet => 'mars')
70
- end
71
- end
72
-
73
- class I18nMissingInterpolationCustomHandlerTest < I18n::TestCase
74
- def setup
75
- super
76
- @old_handler = I18n.config.missing_interpolation_argument_handler
77
- I18n.config.missing_interpolation_argument_handler = lambda do |key, values, string|
78
- "missing key is #{key}, values are #{values.inspect}, given string is '#{string}'"
79
- end
80
- end
81
-
82
- def teardown
83
- I18n.config.missing_interpolation_argument_handler = @old_handler
84
- super
85
- end
86
-
87
- test "String interpolation can use custom missing interpolation handler" do
88
- assert_equal %|Masao missing key is last, values are {:first=>"Masao"}, given string is '%{first} %{last}'|,
89
- I18n.interpolate("%{first} %{last}", :first => 'Masao')
90
- end
91
- end
@@ -1,34 +0,0 @@
1
- require 'test_helper'
2
-
3
- class I18nLoadPathTest < I18n::TestCase
4
- def setup
5
- super
6
- I18n.locale = :en
7
- I18n.backend = I18n::Backend::Simple.new
8
- store_translations(:en, :foo => {:bar => 'bar', :baz => 'baz'})
9
- end
10
-
11
- test "nested load paths do not break locale loading" do
12
- I18n.load_path = [[locales_dir + '/en.yml']]
13
- assert_equal "baz", I18n.t(:'foo.bar')
14
- end
15
-
16
- test "loading an empty yml file raises an InvalidLocaleData exception" do
17
- assert_raise I18n::InvalidLocaleData do
18
- I18n.load_path = [[locales_dir + '/invalid/empty.yml']]
19
- I18n.t(:'foo.bar', :default => "baz")
20
- end
21
- end
22
-
23
- test "loading an invalid yml file raises an InvalidLocaleData exception" do
24
- assert_raise I18n::InvalidLocaleData do
25
- I18n.load_path = [[locales_dir + '/invalid/syntax.yml']]
26
- I18n.t(:'foo.bar', :default => "baz")
27
- end
28
- end
29
-
30
- test "adding arrays of filenames to the load path does not break locale loading" do
31
- I18n.load_path << Dir[locales_dir + '/*.{rb,yml}']
32
- assert_equal "baz", I18n.t(:'foo.bar')
33
- end
34
- end
@@ -1,24 +0,0 @@
1
- require 'test_helper'
2
-
3
- class I18nMiddlewareTest < I18n::TestCase
4
- def setup
5
- super
6
- I18n.default_locale = :fr
7
- @app = DummyRackApp.new
8
- @middleware = I18n::Middleware.new(@app)
9
- end
10
-
11
- test "middleware initializes new config object after request" do
12
- old_i18n_config_object_id = Thread.current[:i18n_config].object_id
13
- @middleware.call({})
14
-
15
- updated_i18n_config_object_id = Thread.current[:i18n_config].object_id
16
- assert_not_equal updated_i18n_config_object_id, old_i18n_config_object_id
17
- end
18
-
19
- test "succesfully resets i18n locale to default locale by defining new config" do
20
- @middleware.call({})
21
-
22
- assert_equal :fr, I18n.locale
23
- end
24
- end