class AionRequestTranslations::Backend

An i18n backend that stores a list of i18n backends on the request. This makes it possible to load translations in a thread safe manner that cleans up after the request.

Public Class Methods

add_backend(backend) click to toggle source

Adds a i18n backend to the beginning of the chain of backends. @return [I18n::Backend::Base] an array of i18n backends

# File lib/aion_request_translations/backend.rb, line 17
def self.add_backend(backend)
  RequestStore.fetch(REQUEST_STORE_KEY) { [] }.unshift(backend)
end
add_translations(translations) click to toggle source

Adds a i18n backend with the given translations. Translations should be a hash with locale as key.

@param translations [Hash] a hash with locale as keys @return [I18n::Backend::Base] an array of i18n backends

# File lib/aion_request_translations/backend.rb, line 26
def self.add_translations(translations)
  backend = I18n::Backend::KeyValue.new({}, true)
  translations.each { |locale, data|
    backend.store_translations(locale, data || {})
  }
  add_backend(backend)
end

Public Instance Methods

backends() click to toggle source
# File lib/aion_request_translations/backend.rb, line 34
def backends
  RequestStore[REQUEST_STORE_KEY] ||= []
end
backends=(backends) click to toggle source
# File lib/aion_request_translations/backend.rb, line 38
def backends=(backends)
  RequestStore[REQUEST_STORE_KEY] = backends
end