i18n 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of i18n might be problematic. Click here for more details.

Files changed (68) hide show
  1. data/CHANGELOG.textile +19 -0
  2. data/README.textile +41 -15
  3. data/Rakefile +1 -1
  4. data/VERSION +1 -1
  5. data/lib/i18n.rb +1 -1
  6. data/lib/i18n/backend/active_record.rb +6 -5
  7. data/lib/i18n/backend/active_record/missing.rb +64 -0
  8. data/lib/i18n/backend/active_record/translation.rb +2 -2
  9. data/lib/i18n/backend/base.rb +2 -2
  10. data/lib/i18n/backend/chain.rb +3 -3
  11. data/lib/i18n/helpers/gettext.rb +1 -1
  12. data/test/all.rb +4 -4
  13. data/test/api/basics.rb +8 -8
  14. data/test/api/defaults.rb +40 -0
  15. data/test/api/interpolation.rb +65 -67
  16. data/test/api/link.rb +31 -38
  17. data/test/api/localization/date.rb +69 -50
  18. data/test/api/localization/date_time.rb +66 -42
  19. data/test/api/localization/procs.rb +54 -0
  20. data/test/api/localization/time.rb +71 -52
  21. data/test/api/lookup.rb +38 -0
  22. data/test/api/pluralization.rb +22 -24
  23. data/test/api/procs.rb +40 -0
  24. data/test/cases/api/active_record_test.rb +28 -0
  25. data/test/cases/api/all_features_test.rb +37 -0
  26. data/test/cases/api/chain_test.rb +26 -0
  27. data/test/cases/api/fallbacks_test.rb +33 -0
  28. data/test/cases/api/pluralization_test.rb +33 -0
  29. data/test/cases/api/simple_test.rb +21 -0
  30. data/test/{backend/active_record_missing/active_record_missing_test.rb → cases/backend/active_record/missing_test.rb} +5 -8
  31. data/test/{backend/active_record → cases/backend}/active_record_test.rb +13 -3
  32. data/test/{backend/cache → cases/backend}/cache_test.rb +7 -7
  33. data/test/{backend/chain → cases/backend}/chain_test.rb +1 -1
  34. data/test/{backend/fallbacks → cases/backend}/fallbacks_test.rb +5 -5
  35. data/test/{backend/simple → cases/backend}/helpers_test.rb +1 -1
  36. data/test/{backend/metadata → cases/backend}/metadata_test.rb +12 -10
  37. data/test/{backend/pluralization → cases/backend}/pluralization_test.rb +10 -10
  38. data/test/cases/backend/simple_test.rb +77 -0
  39. data/test/{core_ext → cases/core_ext}/string/interpolate_test.rb +15 -15
  40. data/test/{gettext → cases/gettext}/api_test.rb +1 -4
  41. data/test/{gettext → cases/gettext}/backend_test.rb +9 -2
  42. data/test/{i18n_exceptions_test.rb → cases/i18n_exceptions_test.rb} +1 -1
  43. data/test/{i18n_load_path_test.rb → cases/i18n_load_path_test.rb} +2 -2
  44. data/test/{i18n_test.rb → cases/i18n_test.rb} +1 -1
  45. data/test/{locale → cases/locale}/fallbacks_test.rb +1 -3
  46. data/test/{locale → cases/locale}/tag/rfc4646_test.rb +1 -3
  47. data/test/{locale → cases/locale}/tag/simple_test.rb +1 -3
  48. data/test/fixtures/locales/de.po +5 -0
  49. data/test/test_helper.rb +45 -30
  50. data/vendor/po_parser.rb +329 -0
  51. metadata +60 -69
  52. data/lib/i18n/backend/active_record_missing.rb +0 -55
  53. data/test/api/lambda.rb +0 -52
  54. data/test/api/localization/lambda.rb +0 -26
  55. data/test/api/translation.rb +0 -51
  56. data/test/backend/active_record/all.rb +0 -3
  57. data/test/backend/active_record/api_test.rb +0 -54
  58. data/test/backend/active_record/setup.rb +0 -166
  59. data/test/backend/all/api_test.rb +0 -88
  60. data/test/backend/chain/api_test.rb +0 -80
  61. data/test/backend/fallbacks/api_test.rb +0 -84
  62. data/test/backend/pluralization/api_test.rb +0 -86
  63. data/test/backend/simple/all.rb +0 -5
  64. data/test/backend/simple/api_test.rb +0 -92
  65. data/test/backend/simple/lookup_test.rb +0 -24
  66. data/test/backend/simple/setup.rb +0 -147
  67. data/test/backend/simple/translations_test.rb +0 -83
  68. data/test/with_options.rb +0 -34
@@ -1,55 +0,0 @@
1
- #
2
- # This extension stores untranslated keys as translation stubs in the database. This is useful if you have a web based
3
- # translation tool. It will populate with untranslated keys as the application is being used. A translator can then go
4
- # through these. Example usage:
5
- #
6
- # I18n::Backend::Chain.send(:include, I18n::Backend::ActiveRecordMissing)
7
- # I18n.backend = I18nChainBackend.new(I18n::Backend::ActiveRecord.new, I18n::Backend::Simple.new)
8
- #
9
- # Stubs for pluralizations will also be created for each key defined in i18n.plural_keys. Eg:
10
- #
11
- # en:
12
- # i18n:
13
- # plural_keys: [:zero, :one, :other]
14
- #
15
- # pl:
16
- # i18n:
17
- # plural_keys: [:zero, :one, :few, :other]
18
- #
19
- # This extension can also persist interpolation keys in Translation#interpolations. This is useful for translators
20
- # who cannot infer possible interpolations from the keys, as they can with other solutions such as gettext or globalize.
21
- #
22
- module I18n
23
- module Backend
24
- module ActiveRecordMissing
25
-
26
- def store_default_translations(locale, key, options = {})
27
- count, scope, default, separator = options.values_at(:count, *Base::RESERVED_KEYS)
28
- separator ||= I18n.default_separator
29
- keys = I18n.send(:normalize_translation_keys, locale, key, scope, separator)[1..-1]
30
- key = keys.join(separator || I18n.default_separator)
31
-
32
- unless ActiveRecord::Translation.locale(locale).lookup(key, separator).exists?
33
- interpolations = options.reject { |name, value| Base::RESERVED_KEYS.include?(name) }.keys
34
- keys = count ? I18n.t('i18n.plural_keys', :locale => locale).map { |k| [key, k].join(separator) } : [key]
35
- keys.each { |key| store_default_translation(locale, key, interpolations) }
36
- end
37
- end
38
-
39
- def store_default_translation(locale, key, interpolations)
40
- translation = ActiveRecord::Translation.new :locale => locale.to_s, :key => key
41
- translation.interpolations = interpolations
42
- translation.save
43
- end
44
-
45
- def translate(locale, key, options = {})
46
- super
47
-
48
- rescue I18n::MissingTranslationData => e
49
- self.store_default_translations(locale, key, options)
50
-
51
- raise e
52
- end
53
- end
54
- end
55
- end
@@ -1,52 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Tests
4
- module Backend
5
- module Api
6
- module Lambda
7
- def test_translate_simple_proc
8
- setup_proc_translations
9
- assert_equal 'bar=bar, baz=baz, foo=foo', I18n.backend.translate('en', :a_lambda, :foo => 'foo', :bar => 'bar', :baz => 'baz')
10
- end
11
-
12
- def test_translate_proc_in_defaults
13
- setup_proc_translations
14
- assert_equal 'bar=bar, baz=baz, foo=foo', I18n.backend.translate('en', :does_not_exist, :default => :a_lambda, :foo => 'foo', :bar => 'bar', :baz => 'baz')
15
- assert_equal 'bar=bar, baz=baz, foo=foo', I18n.backend.translate('en', :does_not_exist, :default => [:does_not_exist_2, :does_not_exist_3, :a_lambda], :foo => 'foo', :bar => 'bar', :baz => 'baz')
16
- end
17
-
18
- def test_translate_proc_with_pluralize
19
- setup_proc_translations
20
- params = { :zero => 'zero', :one => 'one', :other => 'other' }
21
- assert_equal 'zero', I18n.backend.translate('en', :lambda_for_pluralize, params.merge(:count => 0))
22
- assert_equal 'one', I18n.backend.translate('en', :lambda_for_pluralize, params.merge(:count => 1))
23
- assert_equal 'other', I18n.backend.translate('en', :lambda_for_pluralize, params.merge(:count => 2))
24
- end
25
-
26
- def test_translate_proc_with_interpolate
27
- setup_proc_translations
28
- assert_equal 'bar baz foo', I18n.backend.translate('en', :lambda_for_interpolate, :foo => 'foo', :bar => 'bar', :baz => 'baz')
29
- end
30
-
31
- def test_translate_with_proc_as_default
32
- expected = 'result from lambda'
33
- assert_equal expected, I18n.backend.translate(:en, :'does not exist', :default => lambda { |key, values| expected })
34
- end
35
-
36
- private
37
-
38
- def setup_proc_translations
39
- I18n.backend.store_translations 'en', {
40
- :a_lambda => lambda { |key, values|
41
- values.keys.sort_by(&:to_s).collect { |key| "#{key}=#{values[key]}"}.join(', ')
42
- },
43
- :lambda_for_pluralize => lambda { |key, values| values },
44
- :lambda_for_interpolate => lambda { |key, values|
45
- "{{#{values.keys.sort_by(&:to_s).join('}} {{')}}}"
46
- }
47
- }
48
- end
49
- end
50
- end
51
- end
52
- end
@@ -1,26 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Tests
4
- module Backend
5
- module Api
6
- module Localization
7
- module Lambda
8
- def test_localize_uses_lambda_day_names
9
- assert_match /Суббота/, I18n.backend.localize('ru', time, "%A, %d %B")
10
- assert_match /суббота/, I18n.backend.localize('ru', time, "%d %B (%A)")
11
- end
12
-
13
- def test_localize_uses_lambda_month_names
14
- assert_match /марта/, I18n.backend.localize('ru', time, "%d %B %Y")
15
- assert_match /Март/, I18n.backend.localize('ru', time, "%B %Y")
16
- end
17
-
18
- def test_localize_uses_lambda_abbr_day_names
19
- assert_match /марта/, I18n.backend.localize('ru', time, "%d %b %Y")
20
- assert_match /март/, I18n.backend.localize('ru', time, "%b %Y")
21
- end
22
- end
23
- end
24
- end
25
- end
26
- end
@@ -1,51 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Tests
4
- module Backend
5
- module Api
6
- module Translation
7
- def translate(key, options = {})
8
- I18n.backend.translate('en', key, options)
9
- end
10
-
11
- def test_given_no_keys_it_returns_the_default
12
- assert_equal 'default', translate(nil, :default => 'default')
13
- end
14
-
15
- def test_translate_given_a_symbol_as_a_default_translates_the_symbol
16
- assert_equal 'bar', translate(nil, :default => :'foo.bar')
17
- end
18
-
19
- def test_translate_default_with_scope_stays_in_scope_when_looking_up_the_symbol
20
- assert_equal 'bar', translate(:missing, :default => :bar, :scope => :foo)
21
- end
22
-
23
- def test_translate_given_an_array_as_default_uses_the_first_match
24
- assert_equal 'bar', translate(:does_not_exist, :scope => [:foo], :default => [:does_not_exist_2, :bar])
25
- end
26
-
27
- def test_translate_given_an_array_of_inexistent_keys_it_raises_missing_translation_data
28
- assert_raises I18n::MissingTranslationData do
29
- translate(:does_not_exist, :scope => [:foo], :default => [:does_not_exist_2, :does_not_exist_3])
30
- end
31
- end
32
-
33
- def test_translate_an_array_of_keys_translates_all_of_them
34
- assert_equal %w(bar baz), translate([:bar, :baz], :scope => [:foo])
35
- end
36
-
37
- def test_translate_with_a_missing_key_and_no_default_raises_missing_translation_data
38
- assert_raises(I18n::MissingTranslationData) do
39
- translate(:missing)
40
- end
41
- end
42
-
43
- def test_translate_given_nil_as_a_locale_raises_an_argument_error
44
- assert_raises(I18n::InvalidLocale) do
45
- I18n.backend.translate(nil, :bar)
46
- end
47
- end
48
- end
49
- end
50
- end
51
- end
@@ -1,3 +0,0 @@
1
- Dir[File.dirname(__FILE__) + '/*_test.rb'].each do |file|
2
- require file
3
- end
@@ -1,54 +0,0 @@
1
- # encoding: utf-8
2
- require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
3
- require 'i18n/backend/active_record'
4
-
5
- class I18nActiveRecordBackendApiBasicsTest < Test::Unit::TestCase
6
- include Tests::Backend::ActiveRecord::Setup::Base
7
- include Tests::Backend::Api::Basics
8
- end
9
-
10
- class I18nActiveRecordBackendApiTranslateTest < Test::Unit::TestCase
11
- include Tests::Backend::ActiveRecord::Setup::Base
12
- include Tests::Backend::Api::Translation
13
- end
14
-
15
- class I18nActiveRecordBackendApiInterpolateTest < Test::Unit::TestCase
16
- include Tests::Backend::ActiveRecord::Setup::Base
17
- include Tests::Backend::Api::Interpolation
18
- end
19
-
20
- class I18nActiveRecordBackendApiLambdaTest < Test::Unit::TestCase
21
- include Tests::Backend::ActiveRecord::Setup::Base
22
- include Tests::Backend::Api::Lambda
23
- end
24
-
25
- class I18nActiveRecordBackendApiTranslateLinkedTest < Test::Unit::TestCase
26
- include Tests::Backend::ActiveRecord::Setup::Base
27
- include Tests::Backend::Api::Link
28
- end
29
-
30
- class I18nActiveRecordBackendApiPluralizationTest < Test::Unit::TestCase
31
- include Tests::Backend::ActiveRecord::Setup::Base
32
- include Tests::Backend::Api::Pluralization
33
- end
34
-
35
- class I18nActiveRecordBackendApiLocalizeDateTest < Test::Unit::TestCase
36
- include Tests::Backend::ActiveRecord::Setup::Localization
37
- include Tests::Backend::Api::Localization::Date
38
- end
39
-
40
- class I18nActiveRecordBackendApiLocalizeDateTimeTest < Test::Unit::TestCase
41
- include Tests::Backend::ActiveRecord::Setup::Localization
42
- include Tests::Backend::Api::Localization::DateTime
43
- end
44
-
45
- class I18nActiveRecordBackendApiLocalizeTimeTest < Test::Unit::TestCase
46
- include Tests::Backend::ActiveRecord::Setup::Localization
47
- include Tests::Backend::Api::Localization::Time
48
- end
49
-
50
- class I18nActiveRecordBackendApiLocalizeLambdaTest < Test::Unit::TestCase
51
- include Tests::Backend::ActiveRecord::Setup::Localization
52
- include Tests::Backend::Api::Localization::Lambda
53
- end
54
-
@@ -1,166 +0,0 @@
1
- # encoding: utf-8
2
- require 'active_record'
3
- require 'i18n/backend/active_record/store_procs'
4
-
5
- if I18n::Backend::Simple.method_defined?(:interpolate_with_deprecated_syntax)
6
- I18n::Backend::Simple.send(:remove_method, :interpolate) rescue NameError
7
- end
8
-
9
- ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
10
- ActiveRecord::Migration.verbose = false
11
- ActiveRecord::Schema.define(:version => 1) do
12
- create_table :translations do |t|
13
- t.string :locale
14
- t.string :key
15
- t.string :value
16
- t.string :interpolations
17
- t.boolean :is_proc, :default => false
18
- end
19
- end
20
-
21
- module Tests
22
- module Backend
23
- module ActiveRecord
24
- module Setup
25
- module Base
26
- def setup
27
- super
28
- I18n.locale = nil
29
- I18n.default_locale = :en
30
- I18n.backend = I18n::Backend::ActiveRecord.new
31
- I18n::Backend::ActiveRecord::Translation.send(:include, I18n::Backend::ActiveRecord::StoreProcs)
32
- backend_store_translations(:en, :foo => { :bar => 'bar', :baz => 'baz' })
33
- end
34
-
35
- def teardown
36
- super
37
- I18n::Backend::ActiveRecord::Translation.destroy_all
38
- I18n.backend = nil
39
- end
40
- end
41
-
42
- module Localization
43
- include Base
44
-
45
- def setup
46
- super
47
- setup_datetime_translations
48
- setup_datetime_lambda_translations
49
- @old_timezone, ENV['TZ'] = ENV['TZ'], 'UTC'
50
- end
51
-
52
- def teardown
53
- super
54
- @old_timezone ? ENV['TZ'] = @old_timezone : ENV.delete('TZ')
55
- end
56
-
57
- def setup_datetime_translations
58
- backend_store_translations :de, {
59
- :date => {
60
- :formats => {
61
- :default => "%d.%m.%Y",
62
- :short => "%d. %b",
63
- :long => "%d. %B %Y",
64
- :long_ordinalized => lambda { |date, options|
65
- tz = " (#{options[:timezone]})" if options[:timezone]
66
- "#{date.day}ter %B %Y#{tz}"
67
- }
68
- },
69
- :day_names => %w(Sonntag Montag Dienstag Mittwoch Donnerstag Freitag Samstag),
70
- :abbr_day_names => %w(So Mo Di Mi Do Fr Sa),
71
- :month_names => %w(Januar Februar März April Mai Juni Juli August September Oktober November Dezember).unshift(nil),
72
- :abbr_month_names => %w(Jan Feb Mar Apr Mai Jun Jul Aug Sep Okt Nov Dez).unshift(nil),
73
- :order => [:day, :month, :year]
74
- },
75
- :time => {
76
- :formats => {
77
- :default => "%a, %d. %b %Y %H:%M:%S %z",
78
- :short => "%d. %b %H:%M",
79
- :long => "%d. %B %Y %H:%M",
80
- :long_ordinalized => lambda { |date, options|
81
- tz = " (#{options[:timezone]})" if options[:timezone]
82
- "#{date.day}ter %B %Y, %H:%M Uhr#{tz}"
83
- }
84
- },
85
- :am => 'am',
86
- :pm => 'pm'
87
- },
88
- :datetime => {
89
- :distance_in_words => {
90
- :half_a_minute => 'half a minute',
91
- :less_than_x_seconds => {
92
- :one => 'less than 1 second',
93
- :other => 'less than {{count}} seconds'
94
- },
95
- :x_seconds => {
96
- :one => '1 second',
97
- :other => '{{count}} seconds'
98
- },
99
- :less_than_x_minutes => {
100
- :one => 'less than a minute',
101
- :other => 'less than {{count}} minutes'
102
- },
103
- :x_minutes => {
104
- :one => '1 minute',
105
- :other => '{{count}} minutes'
106
- },
107
- :about_x_hours => {
108
- :one => 'about 1 hour',
109
- :other => 'about {{count}} hours'
110
- },
111
- :x_days => {
112
- :one => '1 day',
113
- :other => '{{count}} days'
114
- },
115
- :about_x_months => {
116
- :one => 'about 1 month',
117
- :other => 'about {{count}} months'
118
- },
119
- :x_months => {
120
- :one => '1 month',
121
- :other => '{{count}} months'
122
- },
123
- :about_x_years => {
124
- :one => 'about 1 year',
125
- :other => 'about {{count}} year'
126
- },
127
- :over_x_years => {
128
- :one => 'over 1 year',
129
- :other => 'over {{count}} years'
130
- }
131
- }
132
- }
133
- }
134
- end
135
-
136
- def setup_datetime_lambda_translations
137
- backend_store_translations 'ru', {
138
- :date => {
139
- :'day_names' => lambda { |key, options|
140
- (options[:format] =~ /^%A/) ?
141
- %w(Воскресенье Понедельник Вторник Среда Четверг Пятница Суббота) :
142
- %w(воскресенье понедельник вторник среда четверг пятница суббота)
143
- },
144
- :'abbr_day_names' => %w(Вс Пн Вт Ср Чт Пт Сб),
145
- :'month_names' => lambda { |key, options|
146
- (options[:format] =~ /(%d|%e)(\s*)?(%B)/) ?
147
- %w(января февраля марта апреля мая июня июля августа сентября октября ноября декабря).unshift(nil) :
148
- %w(Январь Февраль Март Апрель Май Июнь Июль Август Сентябрь Октябрь Ноябрь Декабрь).unshift(nil)
149
- },
150
- :'abbr_month_names' => lambda { |key, options|
151
- (options[:format] =~ /(%d|%e)(\s*)(%b)/) ?
152
- %w(янв. февр. марта апр. мая июня июля авг. сент. окт. нояб. дек.).unshift(nil) :
153
- %w(янв. февр. март апр. май июнь июль авг. сент. окт. нояб. дек.).unshift(nil)
154
- },
155
- },
156
- :time => {
157
- :am => "утра",
158
- :pm => "вечера"
159
- }
160
- }
161
- end
162
- end
163
- end
164
- end
165
- end
166
- end
@@ -1,88 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
4
- require 'i18n/backend/chain'
5
-
6
- module AllSetup
7
- class Backend
8
- include I18n::Backend::Base
9
- include I18n::Backend::Cache
10
- include I18n::Backend::Metadata
11
- include I18n::Backend::Fallbacks
12
- include I18n::Backend::Pluralization
13
- end
14
-
15
- def setup
16
- super
17
- I18n.backend = I18n::Backend::Chain.new(Backend.new, I18n.backend)
18
- end
19
-
20
- def test_using_all_features_backend
21
- assert_equal Backend, I18n.backend.backends.first.class
22
- end
23
- end
24
-
25
- class I18nAllBackendApiBasicsTest < Test::Unit::TestCase
26
- include AllSetup
27
- include Tests::Backend::Api::Basics
28
- end
29
-
30
- class I18nAllBackendApiTranslateTest < Test::Unit::TestCase
31
- include Tests::Backend::Simple::Setup::Base
32
- include AllSetup
33
- include Tests::Backend::Api::Translation
34
- end
35
-
36
- class I18nAllBackendApiInterpolateTest < Test::Unit::TestCase
37
- include Tests::Backend::Simple::Setup::Base
38
- include AllSetup
39
- include Tests::Backend::Api::Interpolation
40
- end
41
-
42
- class I18nAllBackendApiLambdaTest < Test::Unit::TestCase
43
- include Tests::Backend::Simple::Setup::Base
44
- include AllSetup
45
- include Tests::Backend::Api::Lambda
46
- end
47
-
48
- class I18nAllBackendApiTranslateLinkedTest < Test::Unit::TestCase
49
- include Tests::Backend::Simple::Setup::Base
50
- include AllSetup
51
- include Tests::Backend::Api::Link
52
- end
53
-
54
- class I18nAllBackendApiPluralizationTest < Test::Unit::TestCase
55
- include Tests::Backend::Simple::Setup::Base
56
- include AllSetup
57
- include Tests::Backend::Api::Pluralization
58
- end
59
-
60
- class I18nAllBackendApiLocalizeDateTest < Test::Unit::TestCase
61
- include Tests::Backend::Simple::Setup::Base
62
- include Tests::Backend::Simple::Setup::Localization
63
- include AllSetup
64
- include Tests::Backend::Api::Localization::Date
65
- end
66
-
67
- class I18nAllBackendApiLocalizeDateTimeTest < Test::Unit::TestCase
68
- include Tests::Backend::Simple::Setup::Base
69
- include Tests::Backend::Simple::Setup::Localization
70
- include AllSetup
71
- include Tests::Backend::Api::Localization::DateTime
72
- end
73
-
74
- class I18nAllBackendApiLocalizeTimeTest < Test::Unit::TestCase
75
- include Tests::Backend::Simple::Setup::Base
76
- include Tests::Backend::Simple::Setup::Localization
77
- include AllSetup
78
- include Tests::Backend::Api::Localization::Time
79
- end
80
-
81
- class I18nAllBackendApiLocalizeLambdaTest < Test::Unit::TestCase
82
- include Tests::Backend::Simple::Setup::Base
83
- include Tests::Backend::Simple::Setup::Localization
84
- include AllSetup
85
- include Tests::Backend::Api::Localization::Lambda
86
- end
87
-
88
-