Sha256: 68013556d0ad2052032aadce569228607f4f17187f39a53c1bfaf1cbafffaff5

Contents?: true

Size: 1.39 KB

Versions: 44

Compression:

Stored size: 1.39 KB

Contents

module Agilibox::ModelI18n
  extend ActiveSupport::Concern

  MissingTranslationError = Class.new(StandardError)
  class << self
    attr_accessor :raise_on_missing_translations
  end
  self.raise_on_missing_translations = false

  def t(*args)
    self.class.t(*args)
  end

  def ts
    self.class.ts
  end

  def tv(attribute)
    value = public_send(attribute)
    t("#{attribute}.#{value}") if value.present?
  end

  class_methods do
    def t(attribute = nil, options = {})
      should_raise = Agilibox::ModelI18n.raise_on_missing_translations
      exception_class = Agilibox::ModelI18n::MissingTranslationError

      if should_raise
        options = options.merge(default: "")
      end

      if should_raise && attribute.nil?
        human = model_name.human(options)
        type = options[:count].to_i > 1 ? "plural" : "singular"
        raise exception_class, "translation missing: #{self} #{type} model name" if human.blank?
        return human
      end

      if should_raise && attribute
        human = human_attribute_name(attribute, options)
        raise exception_class, "translation missing: #{self}##{attribute}" if human.blank?
        return human
      end

      if attribute.nil?
        return model_name.human(options)
      end

      human_attribute_name(attribute, options) if attribute
    end

    def ts
      t(nil, count: 2)
    end
  end # class_methods
end # Agilibox::ModelI18n

Version data entries

44 entries across 44 versions & 1 rubygems

Version Path
agilibox-2.0.0 app/models/concerns/agilibox/model_i18n.rb
agilibox-1.11.0 app/models/concerns/agilibox/model_i18n.rb
agilibox-1.10.5 app/models/concerns/agilibox/model_i18n.rb
agilibox-1.10.2 app/models/concerns/agilibox/model_i18n.rb
agilibox-1.10.1 app/models/concerns/agilibox/model_i18n.rb
agilibox-1.10.0 app/models/concerns/agilibox/model_i18n.rb
agilibox-1.9.20 app/models/concerns/agilibox/model_i18n.rb
agilibox-1.9.19 app/models/concerns/agilibox/model_i18n.rb
agilibox-1.9.18 app/models/concerns/agilibox/model_i18n.rb
agilibox-1.9.17 app/models/concerns/agilibox/model_i18n.rb
agilibox-1.9.16 app/models/concerns/agilibox/model_i18n.rb
agilibox-1.9.15 app/models/concerns/agilibox/model_i18n.rb
agilibox-1.9.14 app/models/concerns/agilibox/model_i18n.rb
agilibox-1.9.13 app/models/concerns/agilibox/model_i18n.rb
agilibox-1.9.12 app/models/concerns/agilibox/model_i18n.rb
agilibox-1.9.11 app/models/concerns/agilibox/model_i18n.rb
agilibox-1.9.10 app/models/concerns/agilibox/model_i18n.rb
agilibox-1.9.9 app/models/concerns/agilibox/model_i18n.rb
agilibox-1.9.8 app/models/concerns/agilibox/model_i18n.rb
agilibox-1.9.7 app/models/concerns/agilibox/model_i18n.rb