Sha256: 3eae81548325627bf3738657bd2de2deef640fdd661132e8bbc72405cf05edd2

Contents?: true

Size: 1.44 KB

Versions: 26

Compression:

Stored size: 1.44 KB

Contents

module Bhf
  module I18nTranslationFallbackHelper
    def self.included(base) 
      base.module_eval do
        class << self
          def translate_with_fallback(text, options = {})
            return translate_without_fallback(text, options) unless text.to_s.split('.')[0] == 'bhf'

            default = options.delete(:default)
          
            [locale, :en].each do |lookup_locale|
              translation_found, translation = attempt_translation(text, options.merge(:locale => lookup_locale))
              return translation if translation_found
            end
          
            # Ensure 'translation missing' return is exactly the default behaviour
            translate_without_fallback(text, options.merge(:default => default))
          end
        
          def attempt_translation(text, options = {})
            puts "Attempting translation of '#{text}' with locale '#{options[:locale]}'." if options[:debug]
            translation = translate_without_fallback(text, options.merge(:raise => true))
            translation_found = options[:locale]
          rescue I18n::MissingTranslationData
            translation_found = nil
            translation = "translation missing: #{options[:locale]}, #{text}"
          ensure
            return translation_found, translation
          end
          
          alias_method_chain :translate, :fallback
          alias_method :t, :translate_with_fallback
        end
      end 
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
bhf-0.4.2.2 lib/bhf/i18n.rb
bhf-0.4.2.1 lib/bhf/i18n.rb
bhf-0.4.2 lib/bhf/i18n.rb
bhf-0.4.1 lib/bhf/i18n.rb
bhf-0.4.0 lib/bhf/i18n.rb
bhf-0.3.11 lib/bhf/i18n.rb
bhf-0.3.10 lib/bhf/i18n.rb
bhf-0.3.9 lib/bhf/i18n.rb
bhf-0.3.8 lib/bhf/i18n.rb
bhf-0.3.7 lib/bhf/i18n.rb
bhf-0.3.6.4 lib/bhf/i18n.rb
bhf-0.3.6.3 lib/bhf/i18n.rb
bhf-0.3.6.2 lib/bhf/i18n.rb
bhf-0.3.6.1 lib/bhf/i18n.rb
bhf-0.3.5 lib/bhf/i18n.rb
bhf-0.3.4 lib/bhf/i18n.rb
bhf-0.3.3 lib/bhf/i18n.rb
bhf-0.3.2 lib/bhf/i18n.rb
bhf-0.3.1 lib/bhf/i18n.rb
bhf-0.3.0 lib/bhf/i18n.rb