Sha256: 6c0f4d422709d660f962ac3787f4ede2112126998ab2e684d9383f379aede864
Contents?: true
Size: 1.49 KB
Versions: 4
Compression:
Stored size: 1.49 KB
Contents
module I18n module JS 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 ||= 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 save_cache(new_cache) FileUtils.mkdir_p(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
4 entries across 4 versions & 1 rubygems
Version | Path |
---|---|
i18n-js-3.0.0.rc9 | lib/i18n/js/middleware.rb |
i18n-js-3.0.0.rc8 | lib/i18n/js/middleware.rb |
i18n-js-3.0.0.rc7 | lib/i18n/js/middleware.rb |
i18n-js-3.0.0.rc6 | lib/i18n/js/middleware.rb |