Sha256: 7da4a84517be7cdc4573b4746a7fc6d3cae945e6701df4b0951cbfce9d45fa88
Contents?: true
Size: 980 Bytes
Versions: 12
Compression:
Stored size: 980 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
12 entries across 12 versions & 1 rubygems