thedarkone-i18n 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. data/CHANGELOG.textile +57 -0
  2. data/README.textile +43 -9
  3. data/Rakefile +21 -0
  4. data/VERSION +1 -0
  5. data/lib/i18n.rb +87 -16
  6. data/lib/i18n/backend/base.rb +251 -0
  7. data/lib/i18n/backend/cache.rb +71 -0
  8. data/lib/i18n/backend/chain.rb +64 -0
  9. data/lib/i18n/backend/fallbacks.rb +53 -0
  10. data/lib/i18n/backend/fast.rb +53 -22
  11. data/lib/i18n/backend/fast/interpolation_compiler.rb +84 -0
  12. data/lib/i18n/backend/gettext.rb +65 -0
  13. data/lib/i18n/backend/lazy_reloading.rb +60 -0
  14. data/lib/i18n/backend/pluralization.rb +56 -0
  15. data/lib/i18n/backend/simple.rb +17 -240
  16. data/lib/i18n/exceptions.rb +13 -5
  17. data/lib/i18n/gettext.rb +25 -0
  18. data/lib/i18n/helpers/gettext.rb +35 -0
  19. data/lib/i18n/locale/fallbacks.rb +100 -0
  20. data/lib/i18n/locale/tag.rb +27 -0
  21. data/lib/i18n/locale/tag/parents.rb +24 -0
  22. data/lib/i18n/locale/tag/rfc4646.rb +78 -0
  23. data/lib/i18n/locale/tag/simple.rb +44 -0
  24. data/test/all.rb +5 -7
  25. data/test/api/basics.rb +15 -0
  26. data/test/api/interpolation.rb +85 -0
  27. data/test/api/lambda.rb +52 -0
  28. data/test/api/link.rb +47 -0
  29. data/test/api/localization/date.rb +65 -0
  30. data/test/api/localization/date_time.rb +63 -0
  31. data/test/api/localization/lambda.rb +26 -0
  32. data/test/api/localization/time.rb +63 -0
  33. data/test/api/pluralization.rb +37 -0
  34. data/test/api/translation.rb +51 -0
  35. data/test/backend/cache/cache_test.rb +57 -0
  36. data/test/backend/chain/api_test.rb +80 -0
  37. data/test/backend/chain/chain_test.rb +64 -0
  38. data/test/backend/fallbacks/api_test.rb +79 -0
  39. data/test/backend/fallbacks/fallbacks_test.rb +29 -0
  40. data/test/backend/fast/all.rb +5 -0
  41. data/test/backend/fast/api_test.rb +91 -0
  42. data/test/backend/fast/interpolation_compiler_test.rb +84 -0
  43. data/test/backend/fast/lookup_test.rb +24 -0
  44. data/test/backend/fast/setup.rb +22 -0
  45. data/test/backend/fast/translations_test.rb +89 -0
  46. data/test/backend/lazy_reloading/reloading_test.rb +67 -0
  47. data/test/backend/pluralization/api_test.rb +81 -0
  48. data/test/backend/pluralization/pluralization_test.rb +39 -0
  49. data/test/backend/simple/all.rb +5 -0
  50. data/test/backend/simple/api_test.rb +90 -0
  51. data/test/backend/simple/lookup_test.rb +24 -0
  52. data/test/backend/simple/setup.rb +151 -0
  53. data/test/backend/simple/translations_test.rb +89 -0
  54. data/test/fixtures/locales/de.po +61 -0
  55. data/test/fixtures/locales/en.rb +3 -0
  56. data/test/fixtures/locales/en.yml +3 -0
  57. data/test/fixtures/locales/plurals.rb +112 -0
  58. data/test/gettext/api_test.rb +78 -0
  59. data/test/gettext/backend_test.rb +35 -0
  60. data/test/i18n_exceptions_test.rb +6 -25
  61. data/test/i18n_load_path_test.rb +23 -0
  62. data/test/i18n_test.rb +56 -18
  63. data/test/locale/fallbacks_test.rb +128 -0
  64. data/test/locale/tag/rfc4646_test.rb +147 -0
  65. data/test/locale/tag/simple_test.rb +35 -0
  66. data/test/test_helper.rb +72 -0
  67. data/test/with_options.rb +34 -0
  68. metadata +109 -19
  69. data/i18n.gemspec +0 -31
  70. data/lib/i18n/backend/fast/pluralization_compiler.rb +0 -50
  71. data/test/backend_test.rb +0 -633
  72. data/test/fast_backend_test.rb +0 -34
  73. data/test/locale/en.rb +0 -1
  74. data/test/locale/en.yml +0 -3
  75. data/test/pluralization_compiler_test.rb +0 -35
@@ -0,0 +1,67 @@
1
+ # encoding: utf-8
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
4
+ require 'i18n/backend/lazy_reloading'
5
+
6
+ class I18nBackendLazyReloadingTest < Test::Unit::TestCase
7
+ class LazyBackend < I18n::Backend::Simple
8
+ include I18n::Backend::LazyReloading
9
+ end
10
+
11
+ include Tests::Backend::Simple::Setup::Base
12
+
13
+ def new_backend
14
+ LazyBackend.new
15
+ end
16
+
17
+ def locale_fixture_path(file)
18
+ File.join(locales_dir, file)
19
+ end
20
+
21
+ def trigger_reload
22
+ I18n.backend.reload!
23
+ I18n.backend.available_locales
24
+ end
25
+
26
+ def assert_triggers_translations_reload
27
+ yield
28
+ I18n.backend.expects(:init_translations)
29
+ trigger_reload
30
+ end
31
+
32
+ def assert_does_not_trigger_translations_reload
33
+ yield
34
+ I18n.backend.expects(:init_translations).never
35
+ trigger_reload
36
+ end
37
+
38
+ def setup
39
+ I18n.backend = new_backend
40
+ I18n.load_path = [locale_fixture_path('en.yml')]
41
+ I18n.backend.send(:init_translations)
42
+ end
43
+
44
+ def test_does_not_perform_reload_if_translation_files_are_not_updated
45
+ assert_does_not_trigger_translations_reload do
46
+ I18n.backend.reload!
47
+ end
48
+ end
49
+
50
+ def test_performs_reload_if_new_translation_is_added
51
+ assert_triggers_translations_reload do
52
+ I18n.load_path << locale_fixture_path('en.rb')
53
+ end
54
+ end
55
+
56
+ def test_performs_reload_if_translation_is_removed
57
+ assert_triggers_translations_reload do
58
+ I18n.load_path.clear
59
+ end
60
+ end
61
+
62
+ def test_performs_reload_if_translation_file_is_updated
63
+ assert_triggers_translations_reload do
64
+ File.expects(:mtime).with(I18n.load_path.first).returns(Time.now - 10)
65
+ end
66
+ end
67
+ 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
+
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
4
+
5
+ class I18nSimpleBackendLookupTest < Test::Unit::TestCase
6
+ include Tests::Backend::Simple::Setup::Base
7
+
8
+ # useful because this way we can use the backend with no key for interpolation/pluralization
9
+ def test_lookup_given_nil_as_a_key_returns_nil
10
+ assert_nil I18n.backend.send(:lookup, :en, nil)
11
+ end
12
+
13
+ def test_lookup_given_nested_keys_looks_up_a_nested_hash_value
14
+ assert_equal 'bar', I18n.backend.send(:lookup, :en, :bar, [:foo])
15
+ end
16
+
17
+ def test_lookup_using_a_custom_separator
18
+ assert_equal 'bar', I18n.backend.send(:lookup, :en, 'foo|bar', [], '|')
19
+ end
20
+
21
+ def test_default_using_a_custom_separator
22
+ assert_equal 'bar', I18n.backend.send(:default, :en, :'does_not_exist', :'foo|bar', :separator => '|')
23
+ end
24
+ end
@@ -0,0 +1,151 @@
1
+ # encoding: utf-8
2
+
3
+ module Tests
4
+ module Backend
5
+ module Simple
6
+ module Setup
7
+ module Base
8
+ def setup
9
+ super
10
+ I18n.locale = nil
11
+ I18n.default_locale = :en
12
+ I18n.backend = new_backend
13
+ backend_store_translations :en, :foo => {:bar => 'bar', :baz => 'baz'}
14
+ end
15
+
16
+ def new_backend
17
+ I18n::Backend::Simple.new
18
+ end
19
+
20
+ def teardown
21
+ super
22
+ I18n.load_path = []
23
+ I18n.backend = nil
24
+ end
25
+ end
26
+
27
+ module Localization
28
+ include Base
29
+
30
+ def setup
31
+ super
32
+ setup_datetime_translations
33
+ setup_datetime_lambda_translations
34
+ @old_timezone, ENV['TZ'] = ENV['TZ'], 'UTC'
35
+ end
36
+
37
+ def teardown
38
+ super
39
+ @old_timezone ? ENV['TZ'] = @old_timezone : ENV.delete('TZ')
40
+ end
41
+
42
+ def setup_datetime_translations
43
+ backend_store_translations :de, {
44
+ :date => {
45
+ :formats => {
46
+ :default => "%d.%m.%Y",
47
+ :short => "%d. %b",
48
+ :long => "%d. %B %Y",
49
+ :long_ordinalized => lambda { |date, options|
50
+ tz = " (#{options[:timezone]})" if options[:timezone]
51
+ "#{date.day}ter %B %Y#{tz}"
52
+ }
53
+ },
54
+ :day_names => %w(Sonntag Montag Dienstag Mittwoch Donnerstag Freitag Samstag),
55
+ :abbr_day_names => %w(So Mo Di Mi Do Fr Sa),
56
+ :month_names => %w(Januar Februar März April Mai Juni Juli August September Oktober November Dezember).unshift(nil),
57
+ :abbr_month_names => %w(Jan Feb Mar Apr Mai Jun Jul Aug Sep Okt Nov Dez).unshift(nil),
58
+ :order => [:day, :month, :year]
59
+ },
60
+ :time => {
61
+ :formats => {
62
+ :default => "%a, %d. %b %Y %H:%M:%S %z",
63
+ :short => "%d. %b %H:%M",
64
+ :long => "%d. %B %Y %H:%M",
65
+ :long_ordinalized => lambda { |date, options|
66
+ tz = " (#{options[:timezone]})" if options[:timezone]
67
+ "#{date.day}ter %B %Y, %H:%M Uhr#{tz}"
68
+ }
69
+ },
70
+ :am => 'am',
71
+ :pm => 'pm'
72
+ },
73
+ :datetime => {
74
+ :distance_in_words => {
75
+ :half_a_minute => 'half a minute',
76
+ :less_than_x_seconds => {
77
+ :one => 'less than 1 second',
78
+ :other => 'less than {{count}} seconds'
79
+ },
80
+ :x_seconds => {
81
+ :one => '1 second',
82
+ :other => '{{count}} seconds'
83
+ },
84
+ :less_than_x_minutes => {
85
+ :one => 'less than a minute',
86
+ :other => 'less than {{count}} minutes'
87
+ },
88
+ :x_minutes => {
89
+ :one => '1 minute',
90
+ :other => '{{count}} minutes'
91
+ },
92
+ :about_x_hours => {
93
+ :one => 'about 1 hour',
94
+ :other => 'about {{count}} hours'
95
+ },
96
+ :x_days => {
97
+ :one => '1 day',
98
+ :other => '{{count}} days'
99
+ },
100
+ :about_x_months => {
101
+ :one => 'about 1 month',
102
+ :other => 'about {{count}} months'
103
+ },
104
+ :x_months => {
105
+ :one => '1 month',
106
+ :other => '{{count}} months'
107
+ },
108
+ :about_x_years => {
109
+ :one => 'about 1 year',
110
+ :other => 'about {{count}} year'
111
+ },
112
+ :over_x_years => {
113
+ :one => 'over 1 year',
114
+ :other => 'over {{count}} years'
115
+ }
116
+ }
117
+ }
118
+ }
119
+ end
120
+
121
+ def setup_datetime_lambda_translations
122
+ backend_store_translations 'ru', {
123
+ :date => {
124
+ :'day_names' => lambda { |key, options|
125
+ (options[:format] =~ /^%A/) ?
126
+ %w(Воскресенье Понедельник Вторник Среда Четверг Пятница Суббота) :
127
+ %w(воскресенье понедельник вторник среда четверг пятница суббота)
128
+ },
129
+ :'abbr_day_names' => %w(Вс Пн Вт Ср Чт Пт Сб),
130
+ :'month_names' => lambda { |key, options|
131
+ (options[:format] =~ /(%d|%e)(\s*)?(%B)/) ?
132
+ %w(января февраля марта апреля мая июня июля августа сентября октября ноября декабря).unshift(nil) :
133
+ %w(Январь Февраль Март Апрель Май Июнь Июль Август Сентябрь Октябрь Ноябрь Декабрь).unshift(nil)
134
+ },
135
+ :'abbr_month_names' => lambda { |key, options|
136
+ (options[:format] =~ /(%d|%e)(\s*)(%b)/) ?
137
+ %w(янв. февр. марта апр. мая июня июля авг. сент. окт. нояб. дек.).unshift(nil) :
138
+ %w(янв. февр. март апр. май июнь июль авг. сент. окт. нояб. дек.).unshift(nil)
139
+ },
140
+ },
141
+ :time => {
142
+ :am => "утра",
143
+ :pm => "вечера"
144
+ }
145
+ }
146
+ end
147
+ end
148
+ end
149
+ end
150
+ end
151
+ end