Module: Bhf::I18nTranslationFallbackHelper

Defined in:
lib/bhf/i18n.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/bhf/i18n.rb', line 3

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