Sha256: dba448ca4fd68f01e6199627e698a00984a7b261eb83c39c0c9210100dc076d9

Contents?: true

Size: 976 Bytes

Versions: 42

Compression:

Stored size: 976 Bytes

Contents

module Para
  module TranslationsHelper
    # 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
    #
    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
  end
end

Version data entries

42 entries across 42 versions & 1 rubygems

Version Path
para-0.9.4 app/helpers/para/translations_helper.rb
para-0.9.3.3 app/helpers/para/translations_helper.rb
para-0.9.3.2 app/helpers/para/translations_helper.rb
para-0.9.3.1 app/helpers/para/translations_helper.rb
para-0.9.2 app/helpers/para/translations_helper.rb
para-0.9.0 app/helpers/para/translations_helper.rb
para-0.8.15 app/helpers/para/translations_helper.rb
para-0.8.14 app/helpers/para/translations_helper.rb
para-0.8.13 app/helpers/para/translations_helper.rb
para-0.8.12 app/helpers/para/translations_helper.rb
para-0.8.11 app/helpers/para/translations_helper.rb
para-0.8.10 app/helpers/para/translations_helper.rb
para-0.8.9 app/helpers/para/translations_helper.rb
para-0.8.8 app/helpers/para/translations_helper.rb
para-0.8.7 app/helpers/para/translations_helper.rb
para-0.8.5 app/helpers/para/translations_helper.rb
para-0.8.3.3 app/helpers/para/translations_helper.rb
para-0.8.3.2 app/helpers/para/translations_helper.rb
para-0.8.3.1 app/helpers/para/translations_helper.rb
para-0.8.3 app/helpers/para/translations_helper.rb