Sha256: 6f10a6a2b874a404349c9e31c09a7a8e4b79c0ed92cbac4f02262add7ab0e929

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

module I18nDashboard
  class Engine < ::Rails::Engine
    isolate_namespace I18nDashboard


    cattr_accessor :redis
    @@redis = Redis.new

    class << self
      def load!
        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
      end

      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
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
i18n_dashboard-0.1.4 lib/i18n_dashboard/engine.rb
i18n_dashboard-0.1.3 lib/i18n_dashboard/engine.rb