Class: Releaf::I18nDatabase::Backend

Inherits:
Object
  • Object
show all
Includes:
I18n::Backend::Base, I18n::Backend::Flatten
Defined in:
lib/releaf/i18n_database/backend.rb

Constant Summary collapse

UPDATED_AT_KEY =
'releaf.i18n_database.translations.updated_at'
DEFAULT_CONFIG =
{
  translation_auto_creation: true,
  translation_auto_creation_patterns: [/.*/],
  translation_auto_creation_exclusion_patterns: [/^attributes\./]
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#translations_cacheObject

Returns the value of attribute translations_cache.



14
15
16
# File 'lib/releaf/i18n_database/backend.rb', line 14

def translations_cache
  @translations_cache
end

Class Method Details

.backend_instanceObject



36
37
38
39
40
41
42
# File 'lib/releaf/i18n_database/backend.rb', line 36

def self.backend_instance
  if I18n.backend.is_a? I18n::Backend::Chain
    I18n.backend.backends.find{|b| b.is_a?(Releaf::I18nDatabase::Backend) }
  elsif I18n.backend.is_a? Releaf::I18nDatabase::Backend
    I18n.backend
  end
end

.configure_componentObject



26
27
28
29
30
# File 'lib/releaf/i18n_database/backend.rb', line 26

def self.configure_component
  Releaf.application.config.add_configuration(
    Releaf::I18nDatabase::Configuration.new(DEFAULT_CONFIG)
  )
end

.draw_component_routes(router) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/releaf/i18n_database/backend.rb', line 44

def self.draw_component_routes router
  router.namespace :releaf, path: nil do
    router.namespace :i18n_database, path: nil do
      router.resources :translations, only: [:index] do
        router.collection do
          router.get :edit
          router.post :update
          router.get :export
          router.post :import
        end
      end
    end
  end
end

.initialize_componentObject



16
17
18
# File 'lib/releaf/i18n_database/backend.rb', line 16

def self.initialize_component
  I18n.backend = I18n::Backend::Chain.new(new, I18n.backend)
end

.locales_pluralizationsObject



20
21
22
23
24
# File 'lib/releaf/i18n_database/backend.rb', line 20

def self.locales_pluralizations
  Releaf.application.config.all_locales.map do|locale|
    TwitterCldr::Formatters::Plurals::Rules.all_for(locale) if TwitterCldr.supported_locale?(locale)
  end.flatten.uniq.compact
end

.reset_cacheObject



32
33
34
# File 'lib/releaf/i18n_database/backend.rb', line 32

def self.reset_cache
  backend_instance.translations_cache = nil
end

.translations_updated_atObject



67
68
69
# File 'lib/releaf/i18n_database/backend.rb', line 67

def self.translations_updated_at
  Releaf::Settings[UPDATED_AT_KEY]
end

.translations_updated_at=(value) ⇒ Object



71
72
73
# File 'lib/releaf/i18n_database/backend.rb', line 71

def self.translations_updated_at= value
  Releaf::Settings[UPDATED_AT_KEY] = value
end

Instance Method Details

#lookup(locale, key, scope = [], options = {}) ⇒ Object

Lookup translation from database



82
83
84
85
86
87
88
89
90
91
# File 'lib/releaf/i18n_database/backend.rb', line 82

def lookup(locale, key, scope = [], options = {})
  key = normalize_flat_keys(locale, key, scope, options[:separator])

  return if translations.missing?(locale, key)

  result = translations.lookup(locale, key, options)
  translations.missing(locale, key, options) if result.nil?

  result
end

#store_translations(locale, data, options = {}) ⇒ Object



75
76
77
78
79
# File 'lib/releaf/i18n_database/backend.rb', line 75

def store_translations(locale, data, options = {})
  # pass to simple backend

  I18n.backend.backends.last.store_translations(locale, data, options)
end

#translationsObject



59
60
61
62
63
64
65
# File 'lib/releaf/i18n_database/backend.rb', line 59

def translations
  if translations_cache && !translations_cache.expired?
    translations_cache
  else
    self.translations_cache = Releaf::I18nDatabase::TranslationsStore.new
  end
end