Sha256: 4931ad44fff781b41362cdcdde7bec314661ea18cb7813f297c15c1c8ed7ce02

Contents?: true

Size: 1.37 KB

Versions: 17

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(*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 = 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

17 entries across 17 versions & 1 rubygems

Version Path
bmc-1.5.1 app/models/concerns/bmc/model_i18n.rb
bmc-1.5.0 app/models/concerns/bmc/model_i18n.rb
bmc-1.4.3 app/models/concerns/bmc/model_i18n.rb
bmc-1.4.2 app/models/concerns/bmc/model_i18n.rb
bmc-1.4.1 app/models/concerns/bmc/model_i18n.rb
bmc-1.4.0 app/models/concerns/bmc/model_i18n.rb
bmc-1.3.5 app/models/concerns/bmc/model_i18n.rb
bmc-1.3.4 app/models/concerns/bmc/model_i18n.rb
bmc-1.3.3 app/models/concerns/bmc/model_i18n.rb
bmc-1.3.2 app/models/concerns/bmc/model_i18n.rb
bmc-1.3.1 app/models/concerns/bmc/model_i18n.rb
bmc-1.3.0 app/models/concerns/bmc/model_i18n.rb
bmc-1.2.1 app/models/concerns/bmc/model_i18n.rb
bmc-1.2.0 app/models/concerns/bmc/model_i18n.rb
bmc-1.1.0 app/models/concerns/bmc/model_i18n.rb
bmc-1.0.1 app/models/concerns/bmc/model_i18n.rb
bmc-1.0.0 app/models/concerns/bmc/model_i18n.rb