Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 55 lines (50 sloc) 2.16 kb
8f02aa2 @svenfuchs only cascade if :cascade option was passed
authored
1 # The Cascade module adds the ability to do cascading lookups to backends that
8164836 @svenfuchs Comment Cascade module
authored
2 # are compatible to the Simple backend.
3 #
4 # By cascading lookups we mean that for any key that can not be found the
5 # Cascade module strips one segment off the scope part of the key and then
6 # tries to look up the key in that scope.
7 #
8 # E.g. when a lookup for the key :"foo.bar.baz" does not yield a result then
9 # the segment :bar will be stripped off the scope part :"foo.bar" and the new
10 # scope :foo will be used to look up the key :baz. If that does not succeed
11 # then the remaining scope segment :foo will be omitted, too, and again the
12 # key :baz will be looked up (now with no scope).
13 #
9054984 @svenfuchs pass full options hash to #lookup. gives extending modules more control.
authored
14 # To enable a cascading lookup one passes the :cascade option:
15 #
16 # I18n.t(:'foo.bar.baz', :cascade => true)
17 #
18 # This will return the first translation found for :"foo.bar.baz", :"foo.baz"
19 # or :baz in this order.
20 #
21 # The cascading lookup takes precedence over resolving any given defaults.
22 # I.e. defaults will kick in after the cascading lookups haven't succeeded.
8164836 @svenfuchs Comment Cascade module
authored
23 #
24 # This behavior is useful for libraries like ActiveRecord validations where
25 # the library wants to give users a bunch of more or less fine-grained options
26 # of scopes for a particular key.
27 #
28 # Thanks to Clemens Kofler for the initial idea and implementation! See
29 # http://github.com/clemens/i18n-cascading-backend
30
8009aef @svenfuchs experimental cascading backend (thanks to Clemens Kofler for the init…
authored
31 module I18n
32 module Backend
33 module Cascade
9054984 @svenfuchs pass full options hash to #lookup. gives extending modules more control.
authored
34 def lookup(locale, key, scope = [], options = {})
e2b8a76 @svenfuchs refactor cascade module, add some options for customization
authored
35 return super unless cascade = options[:cascade]
9054984 @svenfuchs pass full options hash to #lookup. gives extending modules more control.
authored
36
0b9a1f2 @svenfuchs add default options to Cascade#lookup
authored
37 cascade = { :step => 1 } unless cascade.is_a?(Hash)
23c9fef @svenfuchs simplify Backend::Cascade and add a test case assembling an actionvie…
authored
38 step = cascade[:step] || 1
39 offset = cascade[:offset] || 1
e2b8a76 @svenfuchs refactor cascade module, add some options for customization
authored
40 separator = options[:separator] || I18n.default_separator
7cd5728 @svenfuchs default :skip_root to true, rename :length to :offset in I18n::Backen…
authored
41 skip_root = cascade.has_key?(:skip_root) ? cascade[:skip_root] : true
e2b8a76 @svenfuchs refactor cascade module, add some options for customization
authored
42
23c9fef @svenfuchs simplify Backend::Cascade and add a test case assembling an actionvie…
authored
43 scope = I18n.normalize_keys(nil, key, scope, separator)
44 key = (scope.slice!(-offset, offset) || []).join(separator)
8009aef @svenfuchs experimental cascading backend (thanks to Clemens Kofler for the init…
authored
45
46 begin
47 result = super
48 return result unless result.nil?
999d673 make sure, already cached scope is not modified, fixes issue #160
Friedrich Delgado authored
49 scope = scope.dup
23c9fef @svenfuchs simplify Backend::Cascade and add a test case assembling an actionvie…
authored
50 end while (!scope.empty? || !skip_root) && scope.slice!(-step, step)
8009aef @svenfuchs experimental cascading backend (thanks to Clemens Kofler for the init…
authored
51 end
52 end
53 end
54 end
Something went wrong with that request. Please try again.