Class: Releaf::I18nDatabase::Backend

Inherits:
Object
  • Object
show all
Includes:
I18n::Backend::Base, I18n::Backend::Flatten, I18n::Backend::Pluralization
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\./, /^i18n\./]
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#translations_cacheObject

Returns the value of attribute translations_cache.



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

def translations_cache
  @translations_cache
end

Class Method Details

.backend_instanceObject



41
42
43
44
45
46
47
# File 'lib/releaf/i18n_database/backend.rb', line 41

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



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

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

.draw_component_routes(router) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/releaf/i18n_database/backend.rb', line 49

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



19
20
21
# File 'lib/releaf/i18n_database/backend.rb', line 19

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

.locales_pluralizationsObject



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

def self.locales_pluralizations
  keys = Releaf.application.config.all_locales.map{ |locale| I18n.t(:'i18n.plural.keys', locale: locale) }.flatten
  # always add zero as it skipped for some locales even when there is zero form (lv for example)
  keys << :zero

  keys.uniq
end

.reset_cacheObject



37
38
39
# File 'lib/releaf/i18n_database/backend.rb', line 37

def self.reset_cache
  backend_instance.translations_cache = nil
end

.translations_updated_atObject



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

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

.translations_updated_at=(value) ⇒ Object



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

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



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/releaf/i18n_database/backend.rb', line 87

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
  # As localization can be used in routes and as routes is loaded also when running `rake db:create`
  # we want to supress those errors and silently return nil as developer/user will get database errors
  # anyway when call to models will be made (let others do this)
rescue ActiveRecord::NoDatabaseError, ActiveRecord::StatementInvalid
  nil
end

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



80
81
82
83
84
# File 'lib/releaf/i18n_database/backend.rb', line 80

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

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

#translationsObject



64
65
66
67
68
69
70
# File 'lib/releaf/i18n_database/backend.rb', line 64

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