pepe-i18n 0.2.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 (61) hide show
  1. data/CHANGELOG.textile +57 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.textile +42 -0
  4. data/Rakefile +21 -0
  5. data/VERSION +1 -0
  6. data/lib/i18n.rb +270 -0
  7. data/lib/i18n/backend/base.rb +251 -0
  8. data/lib/i18n/backend/cache.rb +71 -0
  9. data/lib/i18n/backend/chain.rb +64 -0
  10. data/lib/i18n/backend/fallbacks.rb +53 -0
  11. data/lib/i18n/backend/gettext.rb +65 -0
  12. data/lib/i18n/backend/pluralization.rb +56 -0
  13. data/lib/i18n/backend/simple.rb +23 -0
  14. data/lib/i18n/exceptions.rb +61 -0
  15. data/lib/i18n/gettext.rb +25 -0
  16. data/lib/i18n/helpers/gettext.rb +35 -0
  17. data/lib/i18n/locale/fallbacks.rb +100 -0
  18. data/lib/i18n/locale/tag.rb +27 -0
  19. data/lib/i18n/locale/tag/parents.rb +24 -0
  20. data/lib/i18n/locale/tag/rfc4646.rb +78 -0
  21. data/lib/i18n/locale/tag/simple.rb +44 -0
  22. data/lib/i18n/string.rb +95 -0
  23. data/test/all.rb +5 -0
  24. data/test/api/basics.rb +15 -0
  25. data/test/api/interpolation.rb +85 -0
  26. data/test/api/lambda.rb +52 -0
  27. data/test/api/link.rb +47 -0
  28. data/test/api/localization/date.rb +65 -0
  29. data/test/api/localization/date_time.rb +63 -0
  30. data/test/api/localization/lambda.rb +26 -0
  31. data/test/api/localization/time.rb +63 -0
  32. data/test/api/pluralization.rb +37 -0
  33. data/test/api/translation.rb +51 -0
  34. data/test/backend/cache/cache_test.rb +57 -0
  35. data/test/backend/chain/api_test.rb +80 -0
  36. data/test/backend/chain/chain_test.rb +64 -0
  37. data/test/backend/fallbacks/api_test.rb +79 -0
  38. data/test/backend/fallbacks/fallbacks_test.rb +29 -0
  39. data/test/backend/pluralization/api_test.rb +81 -0
  40. data/test/backend/pluralization/pluralization_test.rb +39 -0
  41. data/test/backend/simple/all.rb +5 -0
  42. data/test/backend/simple/api_test.rb +90 -0
  43. data/test/backend/simple/lookup_test.rb +24 -0
  44. data/test/backend/simple/setup.rb +147 -0
  45. data/test/backend/simple/translations_test.rb +89 -0
  46. data/test/fixtures/locales/de.po +61 -0
  47. data/test/fixtures/locales/en.rb +3 -0
  48. data/test/fixtures/locales/en.yml +3 -0
  49. data/test/fixtures/locales/plurals.rb +112 -0
  50. data/test/gettext/api_test.rb +78 -0
  51. data/test/gettext/backend_test.rb +35 -0
  52. data/test/i18n_exceptions_test.rb +97 -0
  53. data/test/i18n_load_path_test.rb +23 -0
  54. data/test/i18n_test.rb +163 -0
  55. data/test/locale/fallbacks_test.rb +128 -0
  56. data/test/locale/tag/rfc4646_test.rb +147 -0
  57. data/test/locale/tag/simple_test.rb +35 -0
  58. data/test/string_test.rb +94 -0
  59. data/test/test_helper.rb +71 -0
  60. data/test/with_options.rb +34 -0
  61. metadata +151 -0
@@ -0,0 +1,80 @@
1
+ # encoding: utf-8
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
4
+ require 'i18n/backend/chain'
5
+
6
+ module ChainSetup
7
+ def setup
8
+ super
9
+ first = I18n::Backend::Simple.new
10
+ I18n.backend = I18n::Backend::Chain.new(first, I18n.backend)
11
+ end
12
+
13
+ def test_using_chain_backend
14
+ assert_equal I18n::Backend::Chain, I18n.backend.class
15
+ end
16
+ end
17
+
18
+ class I18nChainBackendApiBasicsTest < Test::Unit::TestCase
19
+ include ChainSetup
20
+ include Tests::Backend::Api::Basics
21
+ end
22
+
23
+ class I18nChainBackendApiTranslateTest < Test::Unit::TestCase
24
+ include Tests::Backend::Simple::Setup::Base
25
+ include ChainSetup
26
+ include Tests::Backend::Api::Translation
27
+ end
28
+
29
+ class I18nChainBackendApiInterpolateTest < Test::Unit::TestCase
30
+ include Tests::Backend::Simple::Setup::Base
31
+ include ChainSetup
32
+ include Tests::Backend::Api::Interpolation
33
+ end
34
+
35
+ class I18nChainBackendApiLambdaTest < Test::Unit::TestCase
36
+ include Tests::Backend::Simple::Setup::Base
37
+ include ChainSetup
38
+ include Tests::Backend::Api::Lambda
39
+ end
40
+
41
+ class I18nChainBackendApiTranslateLinkedTest < Test::Unit::TestCase
42
+ include Tests::Backend::Simple::Setup::Base
43
+ include ChainSetup
44
+ include Tests::Backend::Api::Link
45
+ end
46
+
47
+ class I18nChainBackendApiPluralizationTest < Test::Unit::TestCase
48
+ include Tests::Backend::Simple::Setup::Base
49
+ include ChainSetup
50
+ include Tests::Backend::Api::Pluralization
51
+ end
52
+
53
+ class I18nChainBackendApiLocalizeDateTest < Test::Unit::TestCase
54
+ include Tests::Backend::Simple::Setup::Base
55
+ include Tests::Backend::Simple::Setup::Localization
56
+ include ChainSetup
57
+ include Tests::Backend::Api::Localization::Date
58
+ end
59
+
60
+ class I18nChainBackendApiLocalizeDateTimeTest < Test::Unit::TestCase
61
+ include Tests::Backend::Simple::Setup::Base
62
+ include Tests::Backend::Simple::Setup::Localization
63
+ include ChainSetup
64
+ include Tests::Backend::Api::Localization::DateTime
65
+ end
66
+
67
+ class I18nChainBackendApiLocalizeTimeTest < Test::Unit::TestCase
68
+ include Tests::Backend::Simple::Setup::Base
69
+ include Tests::Backend::Simple::Setup::Localization
70
+ include ChainSetup
71
+ include Tests::Backend::Api::Localization::Time
72
+ end
73
+
74
+ class I18nChainBackendApiLocalizeLambdaTest < Test::Unit::TestCase
75
+ include Tests::Backend::Simple::Setup::Base
76
+ include Tests::Backend::Simple::Setup::Localization
77
+ include ChainSetup
78
+ include Tests::Backend::Api::Localization::Lambda
79
+ end
80
+
@@ -0,0 +1,64 @@
1
+ # encoding: utf-8
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
4
+ require 'i18n/backend/chain'
5
+
6
+ class I18nChainBackendTest < Test::Unit::TestCase
7
+ def setup
8
+ @first = backend(:en => {
9
+ :foo => 'Foo', :formats => { :short => 'short' }, :plural_1 => { :one => '{{count}}' }
10
+ })
11
+ @second = backend(:en => {
12
+ :bar => 'Bar', :formats => { :long => 'long' }, :plural_2 => { :one => 'one' }
13
+ })
14
+ @chain = I18n.backend = I18n::Backend::Chain.new(@first, @second)
15
+ end
16
+
17
+ define_method "test: looks up translations from the first chained backend" do
18
+ assert_equal 'Foo', @first.send(:translations)[:en][:foo]
19
+ assert_equal 'Foo', I18n.t(:foo)
20
+ end
21
+
22
+ define_method "test: looks up translations from the second chained backend" do
23
+ assert_equal 'Bar', @second.send(:translations)[:en][:bar]
24
+ assert_equal 'Bar', I18n.t(:bar)
25
+ end
26
+
27
+ define_method "test: defaults only apply to lookups on the last backend in the chain" do
28
+ assert_equal 'Foo', I18n.t(:foo, :default => 'Bah')
29
+ assert_equal 'Bar', I18n.t(:bar, :default => 'Bah')
30
+ assert_equal 'Bah', I18n.t(:bah, :default => 'Bah') # default kicks in only here
31
+ end
32
+
33
+ define_method "test: default" do
34
+ assert_equal 'Fuh', I18n.t(:default => 'Fuh')
35
+ assert_equal 'Zero', I18n.t(:default => { :zero => 'Zero' }, :count => 0)
36
+ assert_equal({ :zero => 'Zero' }, I18n.t(:default => { :zero => 'Zero' }))
37
+ assert_equal 'Foo', I18n.t(:default => :foo)
38
+ end
39
+
40
+ define_method "test: namespace lookup collects results from all backends" do
41
+ assert_equal({ :short => 'short', :long => 'long' }, I18n.t(:formats))
42
+ end
43
+
44
+ define_method "test: namespace lookup with only the first backend returning a result" do
45
+ assert_equal({ :one => '{{count}}' }, I18n.t(:plural_1))
46
+ end
47
+
48
+ define_method "test: pluralization still works" do
49
+ assert_equal '1', I18n.t(:plural_1, :count => 1)
50
+ assert_equal 'one', I18n.t(:plural_2, :count => 1)
51
+ end
52
+
53
+ define_method "test: bulk lookup collects results from all backends" do
54
+ assert_equal ['Foo', 'Bar'], I18n.t([:foo, :bar])
55
+ end
56
+
57
+ protected
58
+
59
+ def backend(translations)
60
+ backend = I18n::Backend::Simple.new
61
+ translations.each { |locale, translations| backend.store_translations(locale, translations) }
62
+ backend
63
+ end
64
+ end
@@ -0,0 +1,79 @@
1
+ # encoding: utf-8
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
4
+ require 'i18n/backend/fallbacks'
5
+
6
+ module FallbacksSetup
7
+ def setup
8
+ super
9
+ I18n.backend.meta_class.send(:include, I18n::Backend::Fallbacks)
10
+ end
11
+
12
+ def test_uses_fallbacks
13
+ assert I18n.backend.meta_class.included_modules.include?(I18n::Backend::Fallbacks)
14
+ end
15
+ end
16
+
17
+ class I18nFallbacksBackendApiBasicsTest < Test::Unit::TestCase
18
+ include FallbacksSetup
19
+ include Tests::Backend::Api::Basics
20
+ end
21
+
22
+ class I18nFallbacksBackendApiTranslateTest < Test::Unit::TestCase
23
+ include Tests::Backend::Simple::Setup::Base
24
+ include FallbacksSetup
25
+ include Tests::Backend::Api::Translation
26
+ end
27
+
28
+ class I18nFallbacksBackendApiInterpolateTest < Test::Unit::TestCase
29
+ include Tests::Backend::Simple::Setup::Base
30
+ include FallbacksSetup
31
+ include Tests::Backend::Api::Interpolation
32
+ end
33
+
34
+ class I18nFallbacksBackendApiLambdaTest < Test::Unit::TestCase
35
+ include Tests::Backend::Simple::Setup::Base
36
+ include FallbacksSetup
37
+ include Tests::Backend::Api::Lambda
38
+ end
39
+
40
+ class I18nFallbacksBackendApiTranslateLinkedTest < Test::Unit::TestCase
41
+ include Tests::Backend::Simple::Setup::Base
42
+ include FallbacksSetup
43
+ include Tests::Backend::Api::Link
44
+ end
45
+
46
+ class I18nFallbacksBackendApiPluralizationTest < Test::Unit::TestCase
47
+ include Tests::Backend::Simple::Setup::Base
48
+ include FallbacksSetup
49
+ include Tests::Backend::Api::Pluralization
50
+ end
51
+
52
+ class I18nFallbacksBackendApiLocalizeDateTest < Test::Unit::TestCase
53
+ include Tests::Backend::Simple::Setup::Base
54
+ include Tests::Backend::Simple::Setup::Localization
55
+ include FallbacksSetup
56
+ include Tests::Backend::Api::Localization::Date
57
+ end
58
+
59
+ class I18nFallbacksBackendApiLocalizeDateTimeTest < Test::Unit::TestCase
60
+ include Tests::Backend::Simple::Setup::Base
61
+ include Tests::Backend::Simple::Setup::Localization
62
+ include FallbacksSetup
63
+ include Tests::Backend::Api::Localization::DateTime
64
+ end
65
+
66
+ class I18nFallbacksBackendApiLocalizeTimeTest < Test::Unit::TestCase
67
+ include Tests::Backend::Simple::Setup::Base
68
+ include Tests::Backend::Simple::Setup::Localization
69
+ include FallbacksSetup
70
+ include Tests::Backend::Api::Localization::Time
71
+ end
72
+
73
+ class I18nFallbacksBackendApiLocalizeLambdaTest < Test::Unit::TestCase
74
+ include Tests::Backend::Simple::Setup::Base
75
+ include Tests::Backend::Simple::Setup::Localization
76
+ include FallbacksSetup
77
+ include Tests::Backend::Api::Localization::Lambda
78
+ end
79
+
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
4
+ require 'i18n/backend/fallbacks'
5
+
6
+ class I18nFallbacksBackendTest < Test::Unit::TestCase
7
+ def setup
8
+ I18n.backend = I18n::Backend::Simple.new
9
+ I18n.backend.meta_class.send(:include, I18n::Backend::Fallbacks)
10
+ backend_store_translations(:en, :foo => 'Foo')
11
+ end
12
+
13
+ define_method "test: fallbacks for :de are [:de, :en]" do
14
+ assert_equal [:de, :en], I18n.fallbacks[:de]
15
+ end
16
+
17
+ define_method "test: still returns the English translation as usual" do
18
+ assert_equal 'Foo', I18n.t(:foo, :locale => :en)
19
+ end
20
+
21
+ define_method "test: returns the English translation for a missing German translation" do
22
+ assert_equal 'Foo', I18n.t(:foo, :locale => :de)
23
+ end
24
+
25
+ define_method "test: raises I18n::MissingTranslationData exception when no translation was found" do
26
+ assert_raises(I18n::MissingTranslationData) { I18n.t(:bar, :locale => :en, :raise => true) }
27
+ assert_raises(I18n::MissingTranslationData) { I18n.t(:bar, :locale => :de, :raise => true) }
28
+ end
29
+ end
@@ -0,0 +1,81 @@
1
+ # encoding: utf-8
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
4
+ require 'i18n/backend/pluralization'
5
+
6
+ module PluralizationSetup
7
+ def setup
8
+ super
9
+ I18n.backend.meta_class.send(:include, I18n::Backend::Pluralization)
10
+ I18n.load_path << locales_dir + '/plurals.rb'
11
+ end
12
+
13
+ def test_uses_pluralization
14
+ assert I18n.backend.meta_class.included_modules.include?(I18n::Backend::Pluralization)
15
+ end
16
+ end
17
+
18
+ class I18nPluralizationBackendApiBasicsTest < Test::Unit::TestCase
19
+ include PluralizationSetup
20
+ include Tests::Backend::Api::Basics
21
+ end
22
+
23
+ class I18nPluralizationBackendApiTranslateTest < Test::Unit::TestCase
24
+ include Tests::Backend::Simple::Setup::Base
25
+ include PluralizationSetup
26
+ include Tests::Backend::Api::Translation
27
+ end
28
+
29
+ class I18nPluralizationBackendApiInterpolateTest < Test::Unit::TestCase
30
+ include Tests::Backend::Simple::Setup::Base
31
+ include PluralizationSetup
32
+ include Tests::Backend::Api::Interpolation
33
+ end
34
+
35
+ class I18nPluralizationBackendApiLambdaTest < Test::Unit::TestCase
36
+ include Tests::Backend::Simple::Setup::Base
37
+ include PluralizationSetup
38
+ include Tests::Backend::Api::Lambda
39
+ end
40
+
41
+ class I18nPluralizationBackendApiTranslateLinkedTest < Test::Unit::TestCase
42
+ include Tests::Backend::Simple::Setup::Base
43
+ include PluralizationSetup
44
+ include Tests::Backend::Api::Link
45
+ end
46
+
47
+ class I18nPluralizationBackendApiPluralizeTest < Test::Unit::TestCase
48
+ include Tests::Backend::Simple::Setup::Base
49
+ include Tests::Backend::Simple::Setup::Localization
50
+ include PluralizationSetup
51
+ include Tests::Backend::Api::Pluralization
52
+ end
53
+
54
+ class I18nPluralizationBackendApiLocalizeDateTest < Test::Unit::TestCase
55
+ include Tests::Backend::Simple::Setup::Base
56
+ include Tests::Backend::Simple::Setup::Localization
57
+ include PluralizationSetup
58
+ include Tests::Backend::Api::Localization::Date
59
+ end
60
+
61
+ class I18nPluralizationBackendApiLocalizeDateTimeTest < Test::Unit::TestCase
62
+ include Tests::Backend::Simple::Setup::Base
63
+ include Tests::Backend::Simple::Setup::Localization
64
+ include PluralizationSetup
65
+ include Tests::Backend::Api::Localization::DateTime
66
+ end
67
+
68
+ class I18nPluralizationBackendApiLocalizeTimeTest < Test::Unit::TestCase
69
+ include Tests::Backend::Simple::Setup::Base
70
+ include Tests::Backend::Simple::Setup::Localization
71
+ include PluralizationSetup
72
+ include Tests::Backend::Api::Localization::Time
73
+ end
74
+
75
+ class I18nPluralizationBackendApiLocalizeLambdaTest < Test::Unit::TestCase
76
+ include Tests::Backend::Simple::Setup::Base
77
+ include Tests::Backend::Simple::Setup::Localization
78
+ include PluralizationSetup
79
+ include Tests::Backend::Api::Localization::Lambda
80
+ end
81
+
@@ -0,0 +1,39 @@
1
+ # encoding: utf-8
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
4
+ require 'i18n/backend/pluralization'
5
+
6
+ class I18nPluralizationBackendTest < Test::Unit::TestCase
7
+ def setup
8
+ I18n.backend = I18n::Backend::Simple.new
9
+ I18n.backend.meta_class.send(:include, I18n::Backend::Pluralization)
10
+ @pluralizer = lambda { |n| n == 1 ? :one : n == 0 || (2..10).include?(n % 100) ? :few : (11..19).include?(n % 100) ? :many : :other }
11
+ backend_store_translations(:foo, :i18n => { :pluralize => @pluralizer })
12
+ @entry = { :zero => 'zero', :one => 'one', :few => 'few', :many => 'many', :other => 'other' }
13
+ end
14
+
15
+ define_method :"test: pluralization picks a pluralizer from :'i18n.pluralize'" do
16
+ assert_equal @pluralizer, I18n.backend.send(:pluralizer, :foo)
17
+ end
18
+
19
+ define_method :"test: pluralization picks :one for 1" do
20
+ assert_equal 'one', I18n.t(:count => 1, :default => @entry, :locale => :foo)
21
+ end
22
+
23
+ define_method :"test: pluralization picks :few for 2" do
24
+ assert_equal 'few', I18n.t(:count => 2, :default => @entry, :locale => :foo)
25
+ end
26
+
27
+ define_method :"test: pluralization picks :many for 11" do
28
+ assert_equal 'many', I18n.t(:count => 11, :default => @entry, :locale => :foo)
29
+ end
30
+
31
+ define_method :"test: pluralization picks zero for 0 if the key is contained in the data" do
32
+ assert_equal 'zero', I18n.t(:count => 0, :default => @entry, :locale => :foo)
33
+ end
34
+
35
+ define_method :"test: pluralization picks few for 0 if the key is not contained in the data" do
36
+ @entry.delete(:zero)
37
+ assert_equal 'few', I18n.t(:count => 0, :default => @entry, :locale => :foo)
38
+ end
39
+ end
@@ -0,0 +1,5 @@
1
+ # encoding: utf-8
2
+
3
+ Dir[File.dirname(__FILE__) + '/*_test.rb'].each do |file|
4
+ require file
5
+ end
@@ -0,0 +1,90 @@
1
+ # encoding: utf-8
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
4
+
5
+ class I18nSimpleBackendApiBasicsTest < Test::Unit::TestCase
6
+ include Tests::Backend::Simple::Setup::Base
7
+ include Tests::Backend::Api::Basics
8
+
9
+ def test_uses_simple_backend
10
+ assert_equal I18n::Backend::Simple, I18n.backend.class
11
+ end
12
+ end
13
+
14
+ class I18nSimpleBackendApiTranslateTest < Test::Unit::TestCase
15
+ include Tests::Backend::Simple::Setup::Base
16
+ include Tests::Backend::Api::Translation
17
+
18
+ # implementation specific tests
19
+
20
+ def test_translate_calls_lookup_with_locale_given
21
+ I18n.backend.expects(:lookup).with('de', :bar, [:foo], nil).returns 'bar'
22
+ I18n.backend.translate 'de', :bar, :scope => [:foo]
23
+ end
24
+
25
+ def test_translate_calls_pluralize
26
+ I18n.backend.expects(:pluralize).with 'en', 'bar', 1
27
+ I18n.backend.translate 'en', :bar, :scope => [:foo], :count => 1
28
+ end
29
+
30
+ def test_translate_calls_interpolate
31
+ I18n.backend.expects(:interpolate).with 'en', 'bar', {}
32
+ I18n.backend.translate 'en', :bar, :scope => [:foo]
33
+ end
34
+
35
+ def test_translate_calls_interpolate_including_count_as_a_value
36
+ I18n.backend.expects(:interpolate).with 'en', 'bar', {:count => 1}
37
+ I18n.backend.translate 'en', :bar, :scope => [:foo], :count => 1
38
+ end
39
+ end
40
+
41
+ class I18nSimpleBackendApiInterpolateTest < Test::Unit::TestCase
42
+ include Tests::Backend::Simple::Setup::Base
43
+ include Tests::Backend::Api::Interpolation
44
+
45
+ # implementation specific tests
46
+
47
+ def test_interpolate_given_nil_as_a_string_returns_nil
48
+ assert_nil I18n.backend.send(:interpolate, nil, nil, :name => 'David')
49
+ end
50
+
51
+ def test_interpolate_given_an_non_string_as_a_string_returns_nil
52
+ assert_equal [], I18n.backend.send(:interpolate, nil, [], :name => 'David')
53
+ end
54
+ end
55
+
56
+ class I18nSimpleBackendApiLambdaTest < Test::Unit::TestCase
57
+ include Tests::Backend::Simple::Setup::Base
58
+ include Tests::Backend::Api::Lambda
59
+ end
60
+
61
+ class I18nSimpleBackendApiTranslateLinkedTest < Test::Unit::TestCase
62
+ include Tests::Backend::Simple::Setup::Base
63
+ include Tests::Backend::Api::Link
64
+ end
65
+
66
+ class I18nSimpleBackendApiPluralizationTest < Test::Unit::TestCase
67
+ include Tests::Backend::Simple::Setup::Base
68
+ include Tests::Backend::Api::Pluralization
69
+ end
70
+
71
+ class I18nSimpleBackendApiLocalizeDateTest < Test::Unit::TestCase
72
+ include Tests::Backend::Simple::Setup::Localization
73
+ include Tests::Backend::Api::Localization::Date
74
+ end
75
+
76
+ class I18nSimpleBackendApiLocalizeDateTimeTest < Test::Unit::TestCase
77
+ include Tests::Backend::Simple::Setup::Localization
78
+ include Tests::Backend::Api::Localization::DateTime
79
+ end
80
+
81
+ class I18nSimpleBackendApiLocalizeTimeTest < Test::Unit::TestCase
82
+ include Tests::Backend::Simple::Setup::Localization
83
+ include Tests::Backend::Api::Localization::Time
84
+ end
85
+
86
+ class I18nSimpleBackendApiLocalizeLambdaTest < Test::Unit::TestCase
87
+ include Tests::Backend::Simple::Setup::Localization
88
+ include Tests::Backend::Api::Localization::Lambda
89
+ end
90
+