Skip to content

Commit

Permalink
only cascade if :cascade option was passed
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Fuchs committed Jan 21, 2010
1 parent 9054984 commit 8f02aa2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
3 changes: 2 additions & 1 deletion lib/i18n/backend/cascade.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
Expand Down Expand Up @@ -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
Expand Down
38 changes: 23 additions & 15 deletions test/cases/backend/cascade_test.rb
Expand Up @@ -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
Expand All @@ -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.