Class: AionRequestTranslations::Backend

Inherits:
I18n::Backend::Chain
  • Object
show all
Defined in:
lib/aion_request_translations/backend.rb

Overview

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.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_backend(backend) ⇒ I18n::Backend::Base

Adds a i18n backend to the beginning of the chain of backends.

Returns:

  • (I18n::Backend::Base)

    an array of i18n backends



17
18
19
# 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) ⇒ I18n::Backend::Base

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

Parameters:

  • translations (Hash)

    a hash with locale as keys

Returns:

  • (I18n::Backend::Base)

    an array of i18n backends



26
27
28
29
30
31
32
# 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

Instance Method Details

#backendsObject



34
35
36
# File 'lib/aion_request_translations/backend.rb', line 34

def backends
  RequestStore[REQUEST_STORE_KEY] ||= []
end

#backends=(backends) ⇒ Object



38
39
40
# File 'lib/aion_request_translations/backend.rb', line 38

def backends=(backends)
  RequestStore[REQUEST_STORE_KEY] = backends
end