Sha256: 7f8ffaf70741fd4b5b47d5cfcfe1d82341f5d4a8eb398666c5b86cedee09cfc5

Contents?: true

Size: 1.12 KB

Versions: 2

Compression:

Stored size: 1.12 KB

Contents

module SimplesIdeias
  module I18n
    class Middleware
      def initialize(app)
        @app = app
      end

      def call(env)
        @cache = nil
        verify_locale_files!
        @app.call(env)
      end

      private
      def cache_path
        @cache_path ||= Rails.root.join("tmp/cache/i18n-js.yml")
      end

      def cache
        @cache ||= begin
          if cache_path.exist?
            YAML.load_file(cache_path) || {}
          else
            {}
          end
        end
      end

      def verify_locale_files!
        valid_cache = []
        changed_files = []

        valid_cache.push cache_path.exist?
        valid_cache.push ::I18n.load_path.size == cache.size

        ::I18n.load_path.each do |path|
          change = File.mtime(path).to_i
          file_has_changed = change != cache[path]
          valid_cache.push file_has_changed
          changed_files << path if file_has_changed
          cache[path] = change
        end

        File.open(cache_path, "w+") do |file|
          file << cache.to_yaml
        end

        SimplesIdeias::I18n.export! unless valid_cache.all?
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
i18n-js-2.0.1 lib/i18n-js/middleware.rb
i18n-js-2.0.0 lib/i18n-js/middleware.rb