class I18n::JS::Middleware

Public Class Methods

new(app) click to toggle source
# File lib/i18n/js/middleware.rb, line 6
def initialize(app)
  @app = app
  clear_cache
end

Public Instance Methods

call(env) click to toggle source
# File lib/i18n/js/middleware.rb, line 11
def call(env)
  @cache = nil
  verify_locale_files!
  @app.call(env)
end

Private Instance Methods

cache() click to toggle source
# File lib/i18n/js/middleware.rb, line 26
def cache
  @cache ||= begin
    if cache_path.exist?
      YAML.load_file(cache_path) || {}
    else
      {}
    end
  end
end
cache_dir() click to toggle source
# File lib/i18n/js/middleware.rb, line 22
def cache_dir
  @cache_dir ||= Rails.root.join("tmp/cache")
end
cache_path() click to toggle source
# File lib/i18n/js/middleware.rb, line 18
def cache_path
  @cache_path ||= cache_dir.join("i18n-js.yml")
end
clear_cache() click to toggle source
# File lib/i18n/js/middleware.rb, line 36
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
save_cache(new_cache) click to toggle source
# File lib/i18n/js/middleware.rb, line 46
def save_cache(new_cache)
  # path could be a symbolic link
  FileUtils.mkdir_p(cache_dir) unless File.exist?(cache_dir)
  File.open(cache_path, "w+") do |file|
    file << new_cache.to_yaml
  end
end
verify_locale_files!() click to toggle source

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

# File lib/i18n/js/middleware.rb, line 61
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