public
Description: The second incarnation of Globalize for Rails
Home | Edit | New

The chained backend

Overview

The chained backend class allows to use multiple backends for translation and localization.
It loops through all the backends in the order they were added to the chain.

Example


module I18n
 module Backend
   class RussianLocalization
     def localize(locale, object, format = :default)
       return nil unless locale.to_s == 'ru-RU'
       # add Russian localization code here ...
     end
   end
 end
end

I18n.backend = I18n::Backend::Chain.new
I18n.backend.add I18n::Backend::RussianLocalization.new
I18n.backend.add I18n::Backend::Simple.new

Chaining

Backends can be added to the chain as an instances, classes or symbols:


  I18n.chain_backends I18n::Backend::Simple.new, I18n::Backend::Simple, :simple

The following also works


  I18n::Backend::Chain.new I18n::Backend::Simple.new, I18n::Backend::Simple, :simple

Backends can be also added to an existing chain


  I18n.backend.add I18n::Backend::Simple.new, I18n::Backend::Simple, :simple

Translating

The backend returns the first match as a result for a standard translating.
In the case of bulk translation and namespace lookup the backend chain receives and returns translations from all backends.
If no result was found I18n::MissingTranslationData error is raised.


  I18n.t :foo
  I18n.t [:foo, :bar]

Localizing

The chained backend returns the first hit as a result of localization. Therefore backends need to return nil when they can’t localize the
passed object.


  I18n.l Time.now

Last edited by mseppae, Tue Aug 26 07:44:56 -0700 2008
Home | Edit | New
Versions: