Object
Backend that chains multiple other backends and checks each of them when a translation needs to be looked up. This is useful when you want to use standard translations with a Simple backend but store custom application translations in a database or other backends.
To use the Chain backend instantiate it and set it to the I18n module. You can add chained backends through the initializer or backends accessor:
# preserves the existing Simple backend set to I18n.backend I18n.backend = I18n::Backend::Chain.new(I18n::Backend::ActiveRecord.new, I18n.backend)
The implementation assumes that all backends added to the Chain implement a lookup method with the same API as Simple backend does.
# File lib/i18n/backend/chain.rb, line 36 36: def available_locales 37: backends.map { |backend| backend.available_locales }.flatten.uniq 38: end
# File lib/i18n/backend/chain.rb, line 61 61: def localize(locale, object, format = :default, options = {}) 62: backends.each do |backend| 63: begin 64: result = backend.localize(locale, object, format, options) and return result 65: rescue MissingTranslationData 66: end 67: end 68: raise(I18n::MissingTranslationData.new(locale, format, options)) 69: end
# File lib/i18n/backend/chain.rb, line 28 28: def reload! 29: backends.each { |backend| backend.reload! } 30: end
# File lib/i18n/backend/chain.rb, line 32 32: def store_translations(locale, data, options = {}) 33: backends.first.store_translations(locale, data, options = {}) 34: end
# File lib/i18n/backend/chain.rb, line 40 40: def translate(locale, key, options = {}) 41: return key.map { |k| translate(locale, k, options) } if key.is_a?(Array) 42: 43: default = options.delete(:default) 44: namespace = {} 45: backends.each do |backend| 46: begin 47: options.update(:default => default) if default and backend == backends.last 48: translation = backend.translate(locale, key, options) 49: if namespace_lookup?(translation, options) 50: namespace.update(translation) 51: elsif !translation.nil? 52: return translation 53: end 54: rescue MissingTranslationData 55: end 56: end 57: return namespace unless namespace.empty? 58: raise(I18n::MissingTranslationData.new(locale, key, options)) 59: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.