Sha256: 530bdaeb91f85e7347614392571a3ac4348e1a8b3645d16a4077137ef9f94851

Contents?: true

Size: 1.9 KB

Versions: 2

Compression:

Stored size: 1.9 KB

Contents

require 'i18n/locale/fallbacks'

# I18n locale fallbacks are useful when you want your application to use
# translations from other locales when translations for the current locale are
# missing. E.g. you might want to use :en translations when translations in
# your applications main locale :de are missing.
#
# To enable locale fallbacks you can simply include the Fallbacks module to 
# the Simple backend - or whatever other backend you are using:
#
#   I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
module I18n
  @@fallbacks = nil

  class << self
    # Returns the current fallbacks implementation. Defaults to +I18n::Locale::Fallbacks+.
    def fallbacks
      @@fallbacks ||= I18n::Locale::Fallbacks.new
    end

    # Sets the current fallbacks implementation. Use this to set a different fallbacks implementation.
    def fallbacks=(fallbacks)
      @@fallbacks = fallbacks
    end
  end

  module Backend
    module Fallbacks
      # Overwrites the Base backend translate method so that it will try each
      # locale given by I18n.fallbacks for the given locale. E.g. for the
      # locale :"de-DE" it might try the locales :"de-DE", :de and :en
      # (depends on the fallbacks implementation) until it finds a result with
      # the given options. If it does not find any result for any of the
      # locales it will then raise a MissingTranslationData exception as
      # usual.
      #
      # The default option takes precedence over fallback locales, i.e. it
      # will first evaluate a given default option before falling back to
      # another locale.
      def translate(locale, key, options = {})
        I18n.fallbacks[locale].each do |fallback|
          begin
            result = super(fallback, key, options) and return result
          rescue I18n::MissingTranslationData
          end
        end
        raise(I18n::MissingTranslationData.new(locale, key, options))
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
theoooo-i18n-0.2.1 lib/i18n/backend/fallbacks.rb
theoooo-i18n-0.2.2 lib/i18n/backend/fallbacks.rb