3     def chain_backends(*args)
 
   4       self.backend = Globalize::Backend::Chain.new(*args)
 
  13         add(*args) unless args.empty?
 
  16       # Change this to a) accept any number of backends and b) accept classes.
 
  17       # When classes are passed instantiate them and add the instances as backends.
 
  18       # Return the added backends from #add.
 
  20       # Add an initialize method that accepts the same arguments and passes them
 
  21       # to #add, so we could:
 
  22       #   I18n.backend = Globalize::Backend::Chain.new(Globalize::Backend::Foo, Globalize::Backend::Bar)
 
  23       #   Globalize::Backend::Chain.new(:foo, :bar)
 
  24       #   Globalize.chain_backends :foo, :bar
 
  26         backends.each do |backend|
 
  27           backend = Globalize::Backend.const_get(backend.to_s.capitalize) if backend.is_a? Symbol
 
  28           backend = backend.new if backend.is_a? Class
 
  29           self.backends << backend
 
  33       def load_translations(*args)
 
  34         backends.each{|backend| backend.load_translations(*args) }
 
  38       # Never pass any default option to the backends but instead implement our own default
 
  39       # mechanism (e.g. symbols as defaults would need to be passed to the whole chain to
 
  42       # For namespace lookup: 
 
  43       # Only return if the result is not a hash OR count is not present, otherwise merge them.
 
  44       # So in effect the count variable would control whether we have a namespace lookup or a 
 
  45       # pluralization going on.
 
  48       # Make sure that we catch MissingTranslationData exceptions and raise
 
  49       # one in the end when no translation was found at all.
 
  51       # For bulk translation:
 
  52       # If the key is an array we need to call #translate for each of the
 
  53       # keys and collect the results.
 
  55       def translate(locale, key, options = {})
 
  56         raise I18n::InvalidLocale.new(locale) if locale.nil?
 
  57         return key.map{|k| translate locale, k, options } if key.is_a? Array
 
  59         default = options.delete(:default)
 
  60         result = backends.inject({}) do |namespace, backend|
 
  62             translation = backend.translate(locale.to_sym, key, options) 
 
  63             if namespace_lookup?(translation, options)
 
  64               namespace.merge! translation
 
  68           rescue I18n::MissingTranslationData
 
  71         result || default(locale, default, options) || raise(I18n::MissingTranslationData.new(locale, key, options))
 
  74       def localize(locale, object, format = :default)
 
  75         backends.each do |backend|
 
  76           result = backend.localize(locale, object, format) and return result
 
  85         def default(locale, default, options = {})
 
  87             when String then default
 
  88             when Symbol then translate locale, default, options
 
  89             when Array  then default.each do |obj| 
 
  90               result = default(locale, obj, options.dup) and return result
 
  93         rescue I18n::MissingTranslationData
 
  97         def namespace_lookup?(result, options)
 
  98           result.is_a?(Hash) and not options.has_key?(:count)