Module: I18nHelper

Included in:
DryCrud::Form::BuilderTest, FormHelperTest, FormatHelperTest, TableHelperTest
Defined in:
app/helpers/i18n_helper.rb

Overview

Translation helpers extending the Rails translate helper to support translation inheritance over the controller class hierarchy.

Instance Method Summary collapse

Instance Method Details

#translate_association(key, assoc = nil, variables = {}) ⇒ Object Also known as: ta

Translates the passed key for an active record association. This helper is used for rendering association dependent keys in forms like :no_entry, :none_available or :please_select. The key is looked up in the following order:

- activerecord.associations.models.{model_name}.{association_name}.{key}
- activerecord.associations.{association_model_name}.{key}
- global.associations.{key}

31
32
33
34
35
36
37
38
39
# File 'app/helpers/i18n_helper.rb', line 31

def translate_association(key, assoc = nil, variables = {})
  if assoc && assoc.options[:polymorphic].nil?
    variables[:default] ||= [association_klass_key(assoc, key).to_sym,
                             :"global.associations.#{key}"]
    t(association_owner_key(assoc, key), **variables)
  else
    t("global.associations.#{key}", **variables)
  end
end

#translate_inheritable(key, variables = {}) ⇒ Object Also known as: ti

Translates the passed key by looking it up over the controller hierarchy. The key is searched in the following order:

- {controller}.{current_partial}.{key}
- {controller}.{current_action}.{key}
- {controller}.global.{key}
- {parent_controller}.{current_partial}.{key}
- {parent_controller}.{current_action}.{key}
- {parent_controller}.global.{key}
- ...
- global.{key}

15
16
17
18
19
20
# File 'app/helpers/i18n_helper.rb', line 15

def translate_inheritable(key, variables = {})
  partial = defined?(@virtual_path) ? @virtual_path.gsub(/.*\/_?/, '') : nil
  defaults = inheritable_translation_defaults(key, partial)
  variables[:default] ||= defaults
  t(defaults.shift, **variables)
end