i18n 0.4.2 → 0.5.0beta1

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 (43) hide show
  1. data/CHANGELOG.textile +12 -3
  2. data/lib/i18n.rb +18 -21
  3. data/lib/i18n/backend.rb +0 -1
  4. data/lib/i18n/backend/base.rb +0 -13
  5. data/lib/i18n/backend/cache.rb +0 -2
  6. data/lib/i18n/backend/cascade.rb +6 -10
  7. data/lib/i18n/backend/chain.rb +0 -2
  8. data/lib/i18n/backend/cldr.rb +1 -2
  9. data/lib/i18n/backend/fallbacks.rb +0 -2
  10. data/lib/i18n/backend/gettext.rb +0 -2
  11. data/lib/i18n/backend/interpolation_compiler.rb +1 -3
  12. data/lib/i18n/backend/key_value.rb +1 -3
  13. data/lib/i18n/backend/memoize.rb +1 -3
  14. data/lib/i18n/backend/pluralization.rb +0 -2
  15. data/lib/i18n/backend/simple.rb +0 -2
  16. data/lib/i18n/config.rb +2 -2
  17. data/lib/i18n/core_ext/string/interpolate.rb +1 -3
  18. data/lib/i18n/exceptions.rb +29 -6
  19. data/lib/i18n/gettext.rb +0 -2
  20. data/lib/i18n/gettext/helpers.rb +0 -1
  21. data/lib/i18n/locale/fallbacks.rb +0 -2
  22. data/lib/i18n/locale/tag/parents.rb +0 -2
  23. data/lib/i18n/locale/tag/rfc4646.rb +0 -2
  24. data/lib/i18n/locale/tag/simple.rb +0 -2
  25. data/lib/i18n/tests.rb +12 -0
  26. data/lib/i18n/tests/basics.rb +54 -0
  27. data/lib/i18n/tests/defaults.rb +40 -0
  28. data/lib/i18n/tests/interpolation.rb +133 -0
  29. data/lib/i18n/tests/link.rb +56 -0
  30. data/lib/i18n/tests/localization.rb +19 -0
  31. data/lib/i18n/tests/localization/date.rb +84 -0
  32. data/lib/i18n/tests/localization/date_time.rb +77 -0
  33. data/lib/i18n/tests/localization/procs.rb +103 -0
  34. data/lib/i18n/tests/localization/time.rb +76 -0
  35. data/lib/i18n/tests/lookup.rb +74 -0
  36. data/lib/i18n/tests/pluralization.rb +35 -0
  37. data/lib/i18n/tests/procs.rb +55 -0
  38. data/lib/i18n/version.rb +1 -1
  39. metadata +19 -10
  40. data/lib/i18n/backend/active_record.rb +0 -61
  41. data/lib/i18n/backend/active_record/missing.rb +0 -65
  42. data/lib/i18n/backend/active_record/store_procs.rb +0 -38
  43. data/lib/i18n/backend/active_record/translation.rb +0 -110
@@ -0,0 +1,76 @@
1
+ # encoding: utf-8
2
+
3
+ module I18n
4
+ module Tests
5
+ module Localization
6
+ module Time
7
+ def setup
8
+ super
9
+ setup_time_translations
10
+ @time = ::Time.parse('2008-03-01 6:00 UTC')
11
+ @other_time = ::Time.parse('2008-03-01 18:00 UTC')
12
+ end
13
+
14
+ test "localize Time: given the short format it uses it" do
15
+ # TODO should be Mrz, shouldn't it?
16
+ assert_equal '01. Mar 06:00', I18n.l(@time, :format => :short, :locale => :de)
17
+ end
18
+
19
+ test "localize Time: given the long format it uses it" do
20
+ assert_equal '01. März 2008 06:00', I18n.l(@time, :format => :long, :locale => :de)
21
+ end
22
+
23
+ # TODO Seems to break on Windows because ENV['TZ'] is ignored. What's a better way to do this?
24
+ # def test_localize_given_the_default_format_it_uses_it
25
+ # assert_equal 'Sa, 01. Mar 2008 06:00:00 +0000', I18n.l(@time, :format => :default, :locale => :de)
26
+ # end
27
+
28
+ test "localize Time: given a day name format it returns the correct day name" do
29
+ assert_equal 'Samstag', I18n.l(@time, :format => '%A', :locale => :de)
30
+ end
31
+
32
+ test "localize Time: given an abbreviated day name format it returns the correct abbreviated day name" do
33
+ assert_equal 'Sa', I18n.l(@time, :format => '%a', :locale => :de)
34
+ end
35
+
36
+ test "localize Time: given a month name format it returns the correct month name" do
37
+ assert_equal 'März', I18n.l(@time, :format => '%B', :locale => :de)
38
+ end
39
+
40
+ test "localize Time: given an abbreviated month name format it returns the correct abbreviated month name" do
41
+ # TODO should be Mrz, shouldn't it?
42
+ assert_equal 'Mar', I18n.l(@time, :format => '%b', :locale => :de)
43
+ end
44
+
45
+ test "localize Time: given a meridian indicator format it returns the correct meridian indicator" do
46
+ assert_equal 'am', I18n.l(@time, :format => '%p', :locale => :de)
47
+ assert_equal 'pm', I18n.l(@other_time, :format => '%p', :locale => :de)
48
+ end
49
+
50
+ test "localize Time: given an unknown format it does not fail" do
51
+ assert_nothing_raised { I18n.l(@time, :format => '%x') }
52
+ end
53
+
54
+ test "localize Time: given a format is missing it raises I18n::MissingTranslationData" do
55
+ assert_raise(I18n::MissingTranslationData) { I18n.l(@time, :format => :missing) }
56
+ end
57
+
58
+ protected
59
+
60
+ def setup_time_translations
61
+ I18n.backend.store_translations :de, {
62
+ :time => {
63
+ :formats => {
64
+ :default => "%a, %d. %b %Y %H:%M:%S %z",
65
+ :short => "%d. %b %H:%M",
66
+ :long => "%d. %B %Y %H:%M",
67
+ },
68
+ :am => 'am',
69
+ :pm => 'pm'
70
+ }
71
+ }
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,74 @@
1
+ # encoding: utf-8
2
+
3
+ module I18n
4
+ module Tests
5
+ module Lookup
6
+ def setup
7
+ super
8
+ I18n.backend.store_translations(:en, :foo => { :bar => 'bar', :baz => 'baz' }, :falsy => false, :truthy => true,
9
+ :string => "a", :array => %w(a b c), :hash => { "a" => "b" })
10
+ end
11
+
12
+ test "lookup: it returns a string" do
13
+ assert_equal("a", I18n.t(:string))
14
+ end
15
+
16
+ test "lookup: it returns hash" do
17
+ assert_equal({ :a => "b" }, I18n.t(:hash))
18
+ end
19
+
20
+ test "lookup: it returns a array" do
21
+ assert_equal(%w(a b c), I18n.t(:array))
22
+ end
23
+
24
+ test "lookup: it returns a native true" do
25
+ assert I18n.t(:truthy) === true
26
+ end
27
+
28
+ test "lookup: it returns a native false" do
29
+ assert I18n.t(:falsy) === false
30
+ end
31
+
32
+ test "lookup: given a missing key, no default and no raise option it returns an error message" do
33
+ assert_equal "translation missing: en.missing", I18n.t(:missing)
34
+ end
35
+
36
+ test "lookup: given a missing key, no default and the raise option it raises MissingTranslationData" do
37
+ assert_raise(I18n::MissingTranslationData) { I18n.t(:missing, :raise => true) }
38
+ end
39
+
40
+ test "lookup: does not raise an exception if no translation data is present for the given locale" do
41
+ assert_nothing_raised { I18n.t(:foo, :locale => :xx) }
42
+ end
43
+
44
+ test "lookup: given an array of keys it translates all of them" do
45
+ assert_equal %w(bar baz), I18n.t([:bar, :baz], :scope => [:foo])
46
+ end
47
+
48
+ test "lookup: using a custom scope separator" do
49
+ # data must have been stored using the custom separator when using the ActiveRecord backend
50
+ I18n.backend.store_translations(:en, { :foo => { :bar => 'bar' } }, { :separator => '|' })
51
+ assert_equal 'bar', I18n.t('foo|bar', :separator => '|')
52
+ end
53
+
54
+ # In fact it probably *should* fail but Rails currently relies on using the default locale instead.
55
+ # So we'll stick to this for now until we get it fixed in Rails.
56
+ test "lookup: given nil as a locale it does not raise but use the default locale" do
57
+ # assert_raise(I18n::InvalidLocale) { I18n.t(:bar, :locale => nil) }
58
+ assert_nothing_raised { I18n.t(:bar, :locale => nil) }
59
+ end
60
+
61
+ test "lookup: a resulting String is not frozen" do
62
+ assert !I18n.t(:string).frozen?
63
+ end
64
+
65
+ test "lookup: a resulting Array is not frozen" do
66
+ assert !I18n.t(:array).frozen?
67
+ end
68
+
69
+ test "lookup: a resulting Hash is not frozen" do
70
+ assert !I18n.t(:hash).frozen?
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,35 @@
1
+ # encoding: utf-8
2
+
3
+ module I18n
4
+ module Tests
5
+ module Pluralization
6
+ test "pluralization: given 0 it returns the :zero translation if it is defined" do
7
+ assert_equal 'zero', I18n.t(:default => { :zero => 'zero' }, :count => 0)
8
+ end
9
+
10
+ test "pluralization: given 0 it returns the :other translation if :zero is not defined" do
11
+ assert_equal 'bars', I18n.t(:default => { :other => 'bars' }, :count => 0)
12
+ end
13
+
14
+ test "pluralization: given 1 it returns the singular translation" do
15
+ assert_equal 'bar', I18n.t(:default => { :one => 'bar' }, :count => 1)
16
+ end
17
+
18
+ test "pluralization: given 2 it returns the :other translation" do
19
+ assert_equal 'bars', I18n.t(:default => { :other => 'bars' }, :count => 2)
20
+ end
21
+
22
+ test "pluralization: given 3 it returns the :other translation" do
23
+ assert_equal 'bars', I18n.t(:default => { :other => 'bars' }, :count => 3)
24
+ end
25
+
26
+ test "pluralization: given nil it returns the whole entry" do
27
+ assert_equal({ :one => 'bar' }, I18n.t(:default => { :one => 'bar' }, :count => nil))
28
+ end
29
+
30
+ test "pluralization: given incomplete pluralization data it raises I18n::InvalidPluralizationData" do
31
+ assert_raise(I18n::InvalidPluralizationData) { I18n.t(:default => { :one => 'bar' }, :count => 2) }
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,55 @@
1
+ # encoding: utf-8
2
+
3
+ module I18n
4
+ module Tests
5
+ module Procs
6
+ test "lookup: given a translation is a proc it calls the proc with the key and interpolation values" do
7
+ I18n.backend.store_translations(:en, :a_lambda => lambda { |*args| filter_args(*args) })
8
+ assert_equal '[:a_lambda, {:foo=>"foo"}]', I18n.t(:a_lambda, :foo => 'foo')
9
+ end
10
+
11
+ test "defaults: given a default is a Proc it calls it with the key and interpolation values" do
12
+ proc = lambda { |*args| filter_args(*args) }
13
+ assert_equal '[nil, {:foo=>"foo"}]', I18n.t(nil, :default => proc, :foo => 'foo')
14
+ end
15
+
16
+ test "defaults: given a default is a key that resolves to a Proc it calls it with the key and interpolation values" do
17
+ I18n.backend.store_translations(:en, :a_lambda => lambda { |*args| filter_args(*args) })
18
+ assert_equal '[:a_lambda, {:foo=>"foo"}]', I18n.t(nil, :default => :a_lambda, :foo => 'foo')
19
+ assert_equal '[:a_lambda, {:foo=>"foo"}]', I18n.t(nil, :default => [nil, :a_lambda], :foo => 'foo')
20
+ end
21
+
22
+ test "interpolation: given an interpolation value is a lambda it calls it with key and values before interpolating it" do
23
+ proc = lambda { |*args| filter_args(*args) }
24
+ assert_match %r(\[\{:foo=>#<Proc.*>\}\]), I18n.t(nil, :default => '%{foo}', :foo => proc)
25
+ end
26
+
27
+ test "interpolation: given a key resolves to a Proc that returns a string then interpolation still works" do
28
+ proc = lambda { |*args| "%{foo}: " + filter_args(*args) }
29
+ assert_equal 'foo: [nil, {:foo=>"foo"}]', I18n.t(nil, :default => proc, :foo => 'foo')
30
+ end
31
+
32
+ test "pluralization: given a key resolves to a Proc that returns valid data then pluralization still works" do
33
+ proc = lambda { |*args| { :zero => 'zero', :one => 'one', :other => 'other' } }
34
+ assert_equal 'zero', I18n.t(:default => proc, :count => 0)
35
+ assert_equal 'one', I18n.t(:default => proc, :count => 1)
36
+ assert_equal 'other', I18n.t(:default => proc, :count => 2)
37
+ end
38
+
39
+ test "lookup: given the option :resolve => false was passed it does not resolve proc translations" do
40
+ I18n.backend.store_translations(:en, :a_lambda => lambda { |*args| filter_args(*args) })
41
+ assert_equal Proc, I18n.t(:a_lambda, :resolve => false).class
42
+ end
43
+
44
+ test "lookup: given the option :resolve => false was passed it does not resolve proc default" do
45
+ assert_equal Proc, I18n.t(nil, :default => lambda { |*args| filter_args(*args) }, :resolve => false).class
46
+ end
47
+
48
+ protected
49
+
50
+ def filter_args(*args)
51
+ args.map {|arg| arg.delete(:fallback) if arg.is_a?(Hash) ; arg }.inspect
52
+ end
53
+ end
54
+ end
55
+ end
data/lib/i18n/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module I18n
2
- VERSION = "0.4.2"
2
+ VERSION = "0.5.0beta1"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i18n
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
5
- prerelease: false
4
+ hash: -1150612293
5
+ prerelease: true
6
6
  segments:
7
7
  - 0
8
- - 4
9
- - 2
10
- version: 0.4.2
8
+ - 5
9
+ - 0beta1
10
+ version: 0.5.0beta1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sven Fuchs
@@ -19,7 +19,7 @@ autorequire:
19
19
  bindir: bin
20
20
  cert_chain: []
21
21
 
22
- date: 2010-10-26 00:00:00 +02:00
22
+ date: 2010-11-14 00:00:00 +01:00
23
23
  default_executable:
24
24
  dependencies: []
25
25
 
@@ -34,10 +34,6 @@ extra_rdoc_files: []
34
34
  files:
35
35
  - lib/i18n.rb
36
36
  - lib/i18n/backend.rb
37
- - lib/i18n/backend/active_record.rb
38
- - lib/i18n/backend/active_record/missing.rb
39
- - lib/i18n/backend/active_record/store_procs.rb
40
- - lib/i18n/backend/active_record/translation.rb
41
37
  - lib/i18n/backend/base.rb
42
38
  - lib/i18n/backend/cache.rb
43
39
  - lib/i18n/backend/cascade.rb
@@ -66,6 +62,19 @@ files:
66
62
  - lib/i18n/locale/tag/parents.rb
67
63
  - lib/i18n/locale/tag/rfc4646.rb
68
64
  - lib/i18n/locale/tag/simple.rb
65
+ - lib/i18n/tests.rb
66
+ - lib/i18n/tests/basics.rb
67
+ - lib/i18n/tests/defaults.rb
68
+ - lib/i18n/tests/interpolation.rb
69
+ - lib/i18n/tests/link.rb
70
+ - lib/i18n/tests/localization.rb
71
+ - lib/i18n/tests/localization/date.rb
72
+ - lib/i18n/tests/localization/date_time.rb
73
+ - lib/i18n/tests/localization/procs.rb
74
+ - lib/i18n/tests/localization/time.rb
75
+ - lib/i18n/tests/lookup.rb
76
+ - lib/i18n/tests/pluralization.rb
77
+ - lib/i18n/tests/procs.rb
69
78
  - lib/i18n/version.rb
70
79
  - README.textile
71
80
  - MIT-LICENSE
@@ -1,61 +0,0 @@
1
- require 'i18n/backend/base'
2
- require 'i18n/backend/active_record/translation'
3
-
4
- module I18n
5
- module Backend
6
- class ActiveRecord
7
- autoload :Missing, 'i18n/backend/active_record/missing'
8
- autoload :StoreProcs, 'i18n/backend/active_record/store_procs'
9
- autoload :Translation, 'i18n/backend/active_record/translation'
10
-
11
- module Implementation
12
- include Base, Flatten
13
-
14
- def available_locales
15
- begin
16
- Translation.available_locales
17
- rescue ::ActiveRecord::StatementInvalid
18
- []
19
- end
20
- end
21
-
22
- def store_translations(locale, data, options = {})
23
- escape = options.fetch(:escape, true)
24
- flatten_translations(locale, data, escape, false).each do |key, value|
25
- Translation.locale(locale).lookup(expand_keys(key)).delete_all
26
- Translation.create(:locale => locale.to_s, :key => key.to_s, :value => value)
27
- end
28
- end
29
-
30
- protected
31
-
32
- def lookup(locale, key, scope = [], options = {})
33
- key = normalize_flat_keys(locale, key, scope, options[:separator])
34
- result = Translation.locale(locale).lookup(key).all
35
-
36
- if result.empty?
37
- nil
38
- elsif result.first.key == key
39
- result.first.value
40
- else
41
- chop_range = (key.size + FLATTEN_SEPARATOR.size)..-1
42
- result = result.inject({}) do |hash, r|
43
- hash[r.key.slice(chop_range)] = r.value
44
- hash
45
- end
46
- result.deep_symbolize_keys
47
- end
48
- end
49
-
50
- # For a key :'foo.bar.baz' return ['foo', 'foo.bar', 'foo.bar.baz']
51
- def expand_keys(key)
52
- key.to_s.split(FLATTEN_SEPARATOR).inject([]) do |keys, key|
53
- keys << [keys.last, key].compact.join(FLATTEN_SEPARATOR)
54
- end
55
- end
56
- end
57
-
58
- include Implementation
59
- end
60
- end
61
- end
@@ -1,65 +0,0 @@
1
- # This extension stores translation stub records for missing translations to
2
- # the database.
3
- #
4
- # This is useful if you have a web based translation tool. It will populate
5
- # the database with untranslated keys as the application is being used. A
6
- # translator can then go through these and add missing translations.
7
- #
8
- # Example usage:
9
- #
10
- # I18n::Backend::Chain.send(:include, I18n::Backend::ActiveRecord::Missing)
11
- # I18n.backend = I18n::Backend::Chain.new(I18n::Backend::ActiveRecord.new, I18n::Backend::Simple.new)
12
- #
13
- # Stub records for pluralizations will also be created for each key defined
14
- # in i18n.plural.keys.
15
- #
16
- # For example:
17
- #
18
- # # en.yml
19
- # en:
20
- # i18n:
21
- # plural:
22
- # keys: [:zero, :one, :other]
23
- #
24
- # # pl.yml
25
- # pl:
26
- # i18n:
27
- # plural:
28
- # keys: [:zero, :one, :few, :other]
29
- #
30
- # It will also persist interpolation keys in Translation#interpolations so
31
- # translators will be able to review and use them.
32
- module I18n
33
- module Backend
34
- class ActiveRecord
35
- module Missing
36
- include Flatten
37
-
38
- def store_default_translations(locale, key, options = {})
39
- count, scope, default, separator = options.values_at(:count, :scope, :default, :separator)
40
- separator ||= I18n.default_separator
41
- key = normalize_flat_keys(locale, key, scope, separator)
42
-
43
- unless ActiveRecord::Translation.locale(locale).lookup(key).exists?
44
- interpolations = options.keys - Base::RESERVED_KEYS
45
- keys = count ? I18n.t('i18n.plural.keys', :locale => locale).map { |k| [key, k].join(FLATTEN_SEPARATOR) } : [key]
46
- keys.each { |key| store_default_translation(locale, key, interpolations) }
47
- end
48
- end
49
-
50
- def store_default_translation(locale, key, interpolations)
51
- translation = ActiveRecord::Translation.new :locale => locale.to_s, :key => key
52
- translation.interpolations = interpolations
53
- translation.save
54
- end
55
-
56
- def translate(locale, key, options = {})
57
- super
58
- rescue I18n::MissingTranslationData => e
59
- self.store_default_translations(locale, key, options)
60
- raise e
61
- end
62
- end
63
- end
64
- end
65
- end
@@ -1,38 +0,0 @@
1
- # This module is intended to be mixed into the ActiveRecord backend to allow
2
- # storing Ruby Procs as translation values in the database.
3
- #
4
- # I18n.backend = I18n::Backend::ActiveRecord.new
5
- # I18n::Backend::ActiveRecord::Translation.send(:include, I18n::Backend::ActiveRecord::StoreProcs)
6
- #
7
- # The StoreProcs module requires the ParseTree and ruby2ruby gems and therefor
8
- # was extracted from the original backend.
9
- #
10
- # ParseTree is not compatible with Ruby 1.9.
11
-
12
- begin
13
- require 'ruby2ruby'
14
- require 'parse_tree'
15
- require 'parse_tree_extensions'
16
- rescue LoadError => e
17
- puts "can't use StoreProcs because: #{e.message}"
18
- end
19
-
20
- module I18n
21
- module Backend
22
- class ActiveRecord
23
- module StoreProcs
24
- def value=(v)
25
- case v
26
- when Proc
27
- write_attribute(:value, v.to_ruby)
28
- write_attribute(:is_proc, true)
29
- else
30
- write_attribute(:value, v)
31
- end
32
- end
33
-
34
- Translation.send(:include, self) if method(:to_s).respond_to?(:to_ruby)
35
- end
36
- end
37
- end
38
- end