Module: SplitTester::TranslationHelper

Defined in:
lib/split_tester/translation_helper.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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
36
37
38
39
40
41
# File 'lib/split_tester/translation_helper.rb', line 5

def self.included(base)
  # Using class_eval here because base.send(:include, InstanceMethods)
  # wasn't overwriting the translate method properly. Not sure why.
  base.class_eval {
     def translate(key, options = {})
       key = scope_key_by_partial(key)
       if is_split_test?
         # normalize the parameters so that we can add in 
         # the current_split_test_key properly
         scope = options.delete(:scope)
         keys = I18n.normalize_keys(I18n.locale, key, scope)
         keys.shift
         key = keys.join('.')

         # Set the standard key as a default to fall back on automatically
         if options[:default]
           options[:default] = [options[:default]] unless options[:default].is_a?(Array)
           options[:default].unshift(key.to_sym)
         else
           options[:default] = [key.to_sym]
         end

         key = "#{current_split_test_key}.#{key}"
       end
       translation = I18n.translate(key, options.merge!(:raise => true))
       if html_safe_translation_key?(key) && translation.respond_to?(:html_safe)
         translation.html_safe
       else
         translation
       end
     rescue I18n::MissingTranslationData => e
       keys = I18n.normalize_keys(e.locale, e.key, e.options[:scope])
       ('span', keys.join(', '), :class => 'translation_missing')
     end
    alias t translate
  }
end