Permalink
Browse files

only cascade if :cascade option was passed

  • Loading branch information...
1 parent 9054984 commit 8f02aa290e211cdc94015138fc1a8b439631fd54 Sven Fuchs committed Jan 21, 2010
Showing with 25 additions and 16 deletions.
  1. +2 −1 lib/i18n/backend/cascade.rb
  2. +23 −15 test/cases/backend/cascade_test.rb
@@ -1,6 +1,6 @@
# encoding: utf-8
-# The cascade module adds the ability to do cascading lookups to backends that
+# The Cascade module adds the ability to do cascading lookups to backends that
# are compatible to the Simple backend.
#
# By cascading lookups we mean that for any key that can not be found the
@@ -35,6 +35,7 @@ module Backend
module Cascade
def lookup(locale, key, scope = [], options = {})
return unless key
+ return super unless options[:cascade]
locale, *scope = I18n.send(:normalize_translation_keys, locale, key, scope, options[:separator])
key = scope.pop
@@ -16,27 +16,35 @@ def setup
)
end
+ def lookup(key, options = {})
+ I18n.t(key, options.merge(:cascade => true))
+ end
+
test "still returns an existing translation as usual" do
- assert_equal 'foo', I18n.t(:foo)
- assert_equal 'baz', I18n.t(:'bar.baz')
+ assert_equal 'foo', lookup(:foo)
+ assert_equal 'baz', lookup(:'bar.baz')
end
test "falls back by cutting keys off the end of the scope" do
- assert_equal 'foo', I18n.t(:'does_not_exist.foo')
- assert_equal 'foo', I18n.t(:'does_not_exist.does_not_exist.foo')
+ assert_equal 'foo', lookup(:'missing.foo')
+ assert_equal 'foo', lookup(:'missing.missing.foo')
- assert_equal 'baz', I18n.t(:'bar.does_not_exist.baz')
- assert_equal 'baz', I18n.t(:'bar.does_not_exist.does_not_exist.baz')
+ assert_equal 'baz', lookup(:'bar.missing.baz')
+ assert_equal 'baz', lookup(:'bar.missing.missing.baz')
end
test "raises I18n::MissingTranslationData exception when no translation was found" do
- assert_raises(I18n::MissingTranslationData) { I18n.t(:'foo.does_not_exist', :raise => true) }
- assert_raises(I18n::MissingTranslationData) { I18n.t(:'bar.baz.does_not_exist', :raise => true) }
- assert_raises(I18n::MissingTranslationData) { I18n.t(:'does_not_exist.bar.baz', :raise => true) }
+ assert_raises(I18n::MissingTranslationData) { lookup(:'foo.missing', :raise => true) }
+ assert_raises(I18n::MissingTranslationData) { lookup(:'bar.baz.missing', :raise => true) }
+ assert_raises(I18n::MissingTranslationData) { lookup(:'missing.bar.baz', :raise => true) }
end
test "cascades before evaluating the default" do
- assert_equal 'foo', I18n.t(:foo, :scope => :does_not_exist, :default => 'default')
+ assert_equal 'foo', lookup(:foo, :scope => :missing, :default => 'default')
+ end
+
+ test "cascades defaults, too" do
+ assert_equal 'foo', lookup(nil, :default => [:'missing.missing', :'missing.foo'])
end
test "let's us assemble required fallbacks for ActiveRecord validation messages" do
@@ -57,10 +65,10 @@ def setup
:odd => 'odd on errors'
}
)
- assert_equal 'blank on reply title', I18n.t(:'errors.reply.title.blank', :default => :'errors.topic.title.blank')
- assert_equal 'taken on reply', I18n.t(:'errors.reply.title.taken', :default => :'errors.topic.title.taken')
- assert_equal 'format on topic title', I18n.t(:'errors.reply.title.format', :default => :'errors.topic.title.format')
- assert_equal 'length on topic', I18n.t(:'errors.reply.title.length', :default => :'errors.topic.title.length')
- assert_equal 'odd on errors', I18n.t(:'errors.reply.title.odd', :default => :'errors.topic.title.odd')
+ assert_equal 'blank on reply title', lookup(:'errors.reply.title.blank', :default => :'errors.topic.title.blank')
+ assert_equal 'taken on reply', lookup(:'errors.reply.title.taken', :default => :'errors.topic.title.taken')
+ assert_equal 'format on topic title', lookup(:'errors.reply.title.format', :default => :'errors.topic.title.format')
+ assert_equal 'length on topic', lookup(:'errors.reply.title.length', :default => :'errors.topic.title.length')
+ assert_equal 'odd on errors', lookup(:'errors.reply.title.odd', :default => :'errors.topic.title.odd')
end
end

0 comments on commit 8f02aa2

Please sign in to comment.