class I18n::Backend::ActiveRecord::Translation

Constants

FALSY_CHAR
TRUTHY_CHAR

Public Class Methods

available_locales() click to toggle source
# File lib/i18n/backend/active_record/translation.rb, line 84
def available_locales
  Translation.select('DISTINCT locale').to_a.map { |t| t.locale.to_sym }
end
locale(locale) click to toggle source
# File lib/i18n/backend/active_record/translation.rb, line 67
def locale(locale)
  where(locale: locale.to_s)
end
lookup(keys, *separator) click to toggle source
# File lib/i18n/backend/active_record/translation.rb, line 71
def lookup(keys, *separator)
  column_name = connection.quote_column_name('key')
  keys = Array(keys).map!(&:to_s)

  unless separator.empty?
    warn '[DEPRECATION] Giving a separator to Translation.lookup is deprecated. ' \
         'You can change the internal separator by overwriting FLATTEN_SEPARATOR.'
  end

  namespace = "#{keys.last}#{I18n::Backend::Flatten::FLATTEN_SEPARATOR}%"
  where("#{column_name} IN (?) OR #{column_name} LIKE ?", keys, namespace)
end
to_h() click to toggle source
# File lib/i18n/backend/active_record/translation.rb, line 88
def to_h
  Translation.all.each.with_object({}) do |t, memo|
    locale_hash = (memo[t.locale.to_sym] ||= {})
    keys = t.key.split('.')
    keys.each.with_index.inject(locale_hash) do |iterator, (key_part, index)|
      key = key_part.to_sym
      iterator[key] = keys[index + 1] ? (iterator[key] || {}) : t.value
      iterator[key] # rubocop:disable Lint/UnmodifiedReduceAccumulator
    end
  end
end

Public Instance Methods

interpolates?(key) click to toggle source
# File lib/i18n/backend/active_record/translation.rb, line 101
def interpolates?(key)
  interpolations&.include?(key)
end
invalidate_translations_cache() click to toggle source
# File lib/i18n/backend/active_record/translation.rb, line 129
def invalidate_translations_cache
  I18n.backend.reload! if I18n::Backend::ActiveRecord.config.cache_translations
end
value() click to toggle source
# File lib/i18n/backend/active_record/translation.rb, line 105
def value
  value = read_attribute(:value)
  if is_proc
    Kernel.eval(value)
  elsif value == FALSY_CHAR
    false
  elsif value == TRUTHY_CHAR
    true
  else
    value
  end
end
value=(value) click to toggle source
# File lib/i18n/backend/active_record/translation.rb, line 118
def value=(value)
  case value
  when false
    value = FALSY_CHAR
  when true
    value = TRUTHY_CHAR
  end

  write_attribute(:value, value)
end