module I18n::Backend::Vulgata::Implementation

Public Class Methods

new(store = {}) click to toggle source
Calls superclass method
# File lib/i18n/backend/vulgata.rb, line 12
def initialize(store = {})
  @vulgata_translations = store
  super()
end

Public Instance Methods

store_vulgata_translation(locale, key, value) click to toggle source
# File lib/i18n/backend/vulgata.rb, line 17
def store_vulgata_translation(locale, key, value)
  @vulgata_translations[key(locale, key)] = value
end
translations() click to toggle source
Calls superclass method
# File lib/i18n/backend/vulgata.rb, line 25
def translations
  super
end
vulgata_translate(locale, label, options = {}) click to toggle source
# File lib/i18n/backend/vulgata.rb, line 29
def vulgata_translate(locale, label, options = {})

  key = key(locale, label)

  entry = @vulgata_translations[key]

  # contains false value if source or no active record translation
  entry = label if entry == false

  if entry.nil?
    translation = ::Vulgata::I18nTranslation.find_by(locale: locale, key: label)
    if translation.present?
      entry = translation.value
      @vulgata_translations[key] = entry
    else
      # create a source translation - creates all the pending states
      ::Vulgata::I18nTranslation.find_or_create_by(locale: I18n.default_locale, key: label) do |new_translation|
        new_translation.value = label
      end
      @vulgata_translations[key] = (locale.to_sym == I18n.default_locale ? label : false) # false indicates translation is not available
      entry = label
    end
  end

  deep_interpolation = options[:deep_interpolation]
  values = options.except(*RESERVED_KEYS)
  if values
    entry = if deep_interpolation
      deep_interpolate(locale, entry, values)
    else
      interpolate(locale, entry, values)
    end
  end
  entry.html_safe
end
vulgata_translations() click to toggle source
# File lib/i18n/backend/vulgata.rb, line 21
def vulgata_translations
  @vulgata_translations
end

Protected Instance Methods

key(locale, key) click to toggle source
# File lib/i18n/backend/vulgata.rb, line 67
def key(locale, key)
  "#{locale}.#{key}"
end
lookup(locale, key, scope = [], options = {}) click to toggle source
Calls superclass method
# File lib/i18n/backend/vulgata.rb, line 71
def lookup(locale, key, scope = [], options = {})
  store_key = key(locale, key)

  result = @vulgata_translations[store_key]

  return result if result.present?

  result = super

  if result.present? && String === result

    if locale == I18n.default_locale
      ::Vulgata::Helpers.available_locales.each do |translation_locale|
        next if translation_locale == locale # created later the for deafult locale
        locale_result = super(translation_locale.to_sym, key, scope, options)
        if locale_result.present?
          translation = ::Vulgata::I18nTranslation.find_or_create_by(locale: translation_locale, key: key) do |new_translation|
            new_translation.value = locale_result
          end
          @vulgata_translations[key(translation_locale, key)] = translation.value
        else
          @vulgata_translations[key(translation_locale, key)] = false
        end
      end
    end

    translation = ::Vulgata::I18nTranslation.find_or_create_by(locale: locale, key: key) do |new_translation|
      new_translation.value = result
    end

    @vulgata_translations[store_key] = translation.value
    return translation.value
  end

  result
end