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