Module: I18n::MissingTranslation::Base

Included in:
I18n::MissingTranslation, I18n::MissingTranslationData
Defined in:
lib/i18n/exceptions.rb

Constant Summary collapse

PERMITTED_KEYS =
[:scope, :default].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



52
53
54
# File 'lib/i18n/exceptions.rb', line 52

def key
  @key
end

#localeObject (readonly)

Returns the value of attribute locale.



52
53
54
# File 'lib/i18n/exceptions.rb', line 52

def locale
  @locale
end

#optionsObject (readonly)

Returns the value of attribute options.



52
53
54
# File 'lib/i18n/exceptions.rb', line 52

def options
  @options
end

Instance Method Details

#initialize(locale, key, options = EMPTY_HASH) ⇒ Object



54
55
56
57
# File 'lib/i18n/exceptions.rb', line 54

def initialize(locale, key, options = EMPTY_HASH)
  @key, @locale, @options = key, locale, options.slice(*PERMITTED_KEYS)
  options.each { |k, v| self.options[k] = v.inspect if v.is_a?(Proc) }
end

#keysObject



59
60
61
62
63
# File 'lib/i18n/exceptions.rb', line 59

def keys
  @keys ||= I18n.normalize_keys(locale, key, options[:scope]).tap do |keys|
    keys << 'no key' if keys.size < 2
  end
end

#messageObject Also known as: to_s



65
66
67
68
69
70
71
72
# File 'lib/i18n/exceptions.rb', line 65

def message
  if (default = options[:default]).is_a?(Array) && default.any?
    other_options = ([key, *default]).map { |k| normalized_option(k).prepend('- ') }.join("\n")
    "Translation missing. Options considered were:\n#{other_options}"
  else
    "Translation missing: #{keys.join('.')}"
  end
end

#normalized_option(key) ⇒ Object



74
75
76
# File 'lib/i18n/exceptions.rb', line 74

def normalized_option(key)
  I18n.normalize_keys(locale, key, options[:scope]).join('.')
end

#to_exceptionObject



80
81
82
# File 'lib/i18n/exceptions.rb', line 80

def to_exception
  MissingTranslationData.new(locale, key, options)
end