Class: I18nDashboard::Engine

Inherits:
Rails::Engine
  • Object
show all
Defined in:
lib/i18n_dashboard/engine.rb

Constant Summary collapse

@@redis =
Redis.new

Class Method Summary collapse

Class Method Details

.extract_i18n_keys(hash, parent_keys = []) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/i18n_dashboard/engine.rb', line 37

def extract_i18n_keys(hash, parent_keys = [])
  hash.inject([]) do |keys, (key, value)|
    full_key = parent_keys + [key]
    if value.is_a?(Hash)
      # Nested hash
      keys += extract_i18n_keys(value, full_key)
    elsif value.present?
      # String leaf node
      keys << full_key.join(".")
    end
    keys
  end
end

.load!Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/i18n_dashboard/engine.rb', line 11

def load!
  I18n.backend = I18n::Backend::Simple.new
  I18n.t('hello')
  translations = I18n.backend.send(:translations)
  keys = extract_i18n_keys(translations)


  Engine.redis.del('system_keys')
  to_resave = {}
  keys.each do |full_key|
    key = full_key.split('.')[1..-1].join('.')
    locale = full_key.split('.')[0]
    Engine.redis.sadd('system_keys', key)
    to_resave[full_key] = I18n.t(key, locale: locale)
  end

  I18n.backend = I18n::Backend::Chain.new(I18n::Backend::KeyValue.new(Engine.redis), I18n.backend)
  to_resave.each do |full_key, value|
    key = full_key.split('.')[1..-1].join('.')
    locale = full_key.split('.')[0]
    I18n.backend.store_translations(locale, {key => value}, :escape => false)
  end

  I18nDashboard::Translation.rehash!
end