Module: I18n::Backend::ActiveRecord::Missing

Includes:
Flatten, TranslationModel
Defined in:
lib/i18n/backend/active_record/missing.rb

Instance Method Summary collapse

Instance Method Details

#store_default_translation(locale, key, interpolations) ⇒ Object



53
54
55
56
57
# File 'lib/i18n/backend/active_record/missing.rb', line 53

def store_default_translation(locale, key, interpolations)
  translation = translation_model.new locale: locale.to_s, key: key
  translation.interpolations = interpolations
  translation.save
end

#store_default_translations(locale, key, options = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/i18n/backend/active_record/missing.rb', line 41

def store_default_translations(locale, key, options = {})
  count, scope, _, separator = options.values_at(:count, :scope, :default, :separator)
  separator ||= I18n.default_separator
  key = normalize_flat_keys(locale, key, scope, separator)

  return if translation_model.locale(locale).lookup(key).exists?

  interpolations = options.keys - I18n::RESERVED_KEYS
  keys = count ? I18n.t('i18n.plural.keys', locale: locale).map { |k| [key, k].join(FLATTEN_SEPARATOR) } : [key]
  keys.each { |k| store_default_translation(locale, k, interpolations) }
end

#translate(locale, key, options = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/i18n/backend/active_record/missing.rb', line 59

def translate(locale, key, options = {})
  result = catch(:exception) { super }

  if result.is_a?(I18n::MissingTranslation)
    store_default_translations(locale, key, options)
    throw(:exception, result)
  else
    result
  end
end