# require 'i18n/backend/active_record' # require 'vulgata/backend/active_record/translation' # require "vulgata/app/models/i18n_tr.rb" module I18n module Backend class Vulgata < I18n::Backend::Simple module Implementation include Flatten def initialize(store = {}) @vulgata_translations = store super() end def store_vulgata_translation(locale, key, value) @vulgata_translations[key(locale, key)] = value end def vulgata_translations @vulgata_translations end def translations super end 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 protected def key(locale, key) "#{locale}.#{key}" end 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 end include Implementation end end end