Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

51 lines (46 sloc) 1.803 kb
# encoding: utf-8
# 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
# Cascade module strips one segment off the scope part of the key and then
# tries to look up the key in that scope.
# E.g. when a lookup for the key :"" does not yield a result then
# the segment :bar will be stripped off the scope part :"" and the new
# scope :foo will be used to look up the key :baz. If that does not succeed
# then the remaining scope segment :foo will be omitted, too, and again the
# key :baz will be looked up (now with no scope).
# To enable a cascading lookup one passes the :cascade option:
# I18n.t(:'', :cascade => true)
# This will return the first translation found for :"", :"foo.baz"
# or :baz in this order.
# The cascading lookup takes precedence over resolving any given defaults.
# I.e. defaults will kick in after the cascading lookups haven't succeeded.
# This behavior is useful for libraries like ActiveRecord validations where
# the library wants to give users a bunch of more or less fine-grained options
# of scopes for a particular key.
# Thanks to Clemens Kofler for the initial idea and implementation! See
module I18n
module Backend
module Cascade
def lookup(locale, key, scope = [], options = {})
return unless key
return super unless options[:cascade]
locale, *scope = I18n.normalize_keys(locale, key, scope, options[:separator])
key = scope.pop
result = super
return result unless result.nil?
end while scope.pop
Jump to Line
Something went wrong with that request. Please try again.