Sha256: bb970009f286a6383556dd823ee27599fc00540b1364d28c63952054ab9eb861

Contents?: true

Size: 1.37 KB

Versions: 2

Compression:

Stored size: 1.37 KB

Contents

module BMC::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(...)
    self.class.t(...)
  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 = BMC::ModelI18n.raise_on_missing_translations
      exception_class = BMC::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 # BMC::ModelI18n

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bmc-1.6.1 app/models/concerns/bmc/model_i18n.rb
bmc-1.6.0 app/models/concerns/bmc/model_i18n.rb