Sha256: d7f5c5be015ace589191ba12e49d1d34ad5387845383c2eead8553b70c0b16fc

Contents?: true

Size: 1.94 KB

Versions: 30

Compression:

Stored size: 1.94 KB

Contents

require "fileutils"

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

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

      private
      def cache_path
        @cache_path ||= cache_dir.join("i18n-js.yml")
      end

      def cache_dir
        @cache_dir ||= Rails.root.join("tmp/cache")
      end

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

      def clear_cache
        # `File.delete` will raise error when "multiple worker"
        # Are running at the same time, like in a parallel test
        #
        # `FileUtils.rm_f` is tested manually
        #
        # See https://github.com/fnando/i18n-js/issues/436
        FileUtils.rm_f(cache_path) if File.exist?(cache_path)
      end

      def save_cache(new_cache)
        # path could be a symbolic link
        FileUtils.mkdir_p(cache_dir) unless File.exists?(cache_dir)
        File.open(cache_path, "w+") do |file|
          file << new_cache.to_yaml
        end
      end

      # Check if translations should be regenerated.
      # ONLY REGENERATE when these conditions are met:
      #
      # # Cache file doesn't exist
      # # Translations and cache size are different (files were removed/added)
      # # Translation file has been updated
      #
      def verify_locale_files!
        valid_cache = []
        new_cache = {}

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

        ::I18n.load_path.each do |path|
          changed_at = File.mtime(path).to_i
          valid_cache.push changed_at == cache[path]
          new_cache[path] = changed_at
        end

        return if valid_cache.all?

        save_cache(new_cache)

        ::I18n::JS.export
      end
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
i18n-js-3.8.1 lib/i18n/js/middleware.rb
i18n-js-3.8.0 lib/i18n/js/middleware.rb
i18n-js-3.7.1 lib/i18n/js/middleware.rb
i18n-js-3.7.0 lib/i18n/js/middleware.rb
i18n-js-3.6.0 lib/i18n/js/middleware.rb
i18n-js-3.5.1 lib/i18n/js/middleware.rb
i18n-js-3.5.0 lib/i18n/js/middleware.rb
i18n-js-3.4.2 lib/i18n/js/middleware.rb
i18n-js-3.4.1 lib/i18n/js/middleware.rb
i18n-js-3.4.0 lib/i18n/js/middleware.rb
i18n-js-3.3.0 lib/i18n/js/middleware.rb
i18n-js-3.2.3 lib/i18n/js/middleware.rb
i18n-js-3.2.2 lib/i18n/js/middleware.rb
i18n-js-3.2.1 lib/i18n/js/middleware.rb
i18n-js-3.2.0 lib/i18n/js/middleware.rb
i18n-js-3.1.0 lib/i18n/js/middleware.rb
i18n-js-3.0.11 lib/i18n/js/middleware.rb
i18n-js-3.0.10 lib/i18n/js/middleware.rb
i18n-js-3.0.9 lib/i18n/js/middleware.rb
i18n-js-3.0.8 lib/i18n/js/middleware.rb