class I18n::PostgresJson::Translation

Public Class Methods

available_locales() click to toggle source
# File lib/i18n/postgres_json/translation.rb, line 6
def self.available_locales
  (
    distinct.order(locale: :asc).pluck(:locale) + [I18n.default_locale]
  ).map(&:to_sym).uniq
end
locale(locale) click to toggle source
# File lib/i18n/postgres_json/translation.rb, line 20
def self.locale(locale)
  find_or_initialize_by(locale: locale)
end
store_translations(locale, data) click to toggle source
# File lib/i18n/postgres_json/translation.rb, line 12
def self.store_translations(locale, data)
  record = self.locale(locale)

  record.translations.deep_merge!(data.deep_stringify_keys)

  record.save!
end

Public Instance Methods

dig(*keys) click to toggle source
# File lib/i18n/postgres_json/translation.rb, line 24
def dig(*keys)
  keys = Array(keys)

  translations.with_indifferent_access.dig(*keys)
end
lookup(key, scope = [], separator:) click to toggle source
# File lib/i18n/postgres_json/translation.rb, line 30
def lookup(key, scope = [], separator:)
  _, *keys = I18n.normalize_keys(locale, key, scope, separator)

  translated = keys.each_with_index.reduce(nil) { |translation, (_, index)|
    if translation.present?
      translation
    else
      leading = keys.take(index)
      trailing = keys.drop(index)

      dig(*([leading.join(separator)] + keys - leading)) ||
        dig(*(keys - trailing + [trailing.join(separator)]))
    end
  }

  if translated.respond_to?(:deep_symbolize_keys)
    translated.deep_symbolize_keys
  else
    translated
  end
end