Module: Para::TranslationsHelper

Defined in:
app/helpers/para/translations_helper.rb

Instance Method Summary collapse

Instance Method Details

#model_translate(key, model: nil, scope: nil, **options) ⇒ Object

This helper method allows to use ActiveModel or ActiveRecord model hierarchy to use translations with automatic defaults from parent models.

This works by scanning all the model ancestors to find an existing translation, allowing defining parent class translations and optionnaly overriding translations in subclasses scope



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/helpers/para/translations_helper.rb', line 10

def model_translate(key, model: nil, scope: nil, **options)
  # Get model class if model argument was passed a model instance
  model = model.class if model.class.respond_to?(:model_name)

  # Create a key for every parent class that could contain a translation
  defaults = model.lookup_ancestors.map do |klass|
    :"#{ scope }.#{ klass.model_name.i18n_key }.#{ key }"
  end

  defaults << options.delete(:default) if options[:default]
  options[:default] = defaults

  ::I18n.translate(defaults.shift, **options)
end