Module: E9Rails::Helpers::Translation::InstanceMethods

Defined in:
lib/e9_rails/helpers/translation.rb

Instance Method Summary collapse

Instance Method Details

#e9_translate(lookup_key, opts = {}) ⇒ Object Also known as: e9_t



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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/e9_rails/helpers/translation.rb', line 11

def e9_translate(lookup_key, opts = {})
  # TODO is there a nicer way to say this?
  defaults = []

  lookup_key = lookup_key.to_s

  # if a scoped key is passed assume it's fully scoped (after e9)
  if lookup_key.include?(".")
    defaults << lookup_key
    # reset the key for base lookup
    lookup_key = lookup_key.split('.').last

  # elsif scope is passed, delete it and use the path as the first default
  elsif scope = opts.delete(:scope)
    # this lets you pass scope as an empty string
    scope = nil if scope.blank?
    defaults << (Array.wrap(scope) << lookup_key).compact.join('.')

  # the difference between default and scope is that default expects a key,
  # ignoring the lookup key
  elsif default = opts.delete(:default)
    defaults << default
  end

  # append default lookup of controller path, e.g. "e9.admin.foos.lookup_key"
  defaults << [controller_path.gsub(/\//, '.'), lookup_key].join('.')

  # and after that, base lookup for the key
  defaults << lookup_key

  # sym them and clean up possible accidental double dots
  defaults.map! {|default| [:e9, default].join('.').gsub(/\.+/, '.').to_sym }
  defaults.uniq!

  lookup_key = defaults.shift

  begin
    model_name = (opts.delete(:resource_class) || resource_class).model_name
    opts.reverse_merge!(
      :model      => model_name.human,
      :models     => model_name.human.pluralize,
      :collection => model_name.collection,
      :element    => model_name.element
    )
  rescue
  end

  ::I18n.translate(lookup_key, opts.merge(:default => defaults))
end