Sha256: 813d39a6e8a75dc5667102d421f5cbeacd57c468503dbd468e0eacb2da71b852

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

# Speeds up the reload! method (usefull in development mode) by first making sure
# the locale files have actually changed (this is done using their last mtime).
# Usage:
# 
#   I18n::Backend::Simple.send(:include, I18n::Backend::LazyReloading)

module I18n
  module Backend
    module LazyReloading
      def reload!
        flush_translations if stale?
      end
      
    protected
      def init_translations
        load_translations(*load_paths)
        @initialized = true
      end
      
      def load_paths
        I18n.load_path.flatten
      end

      def flush_translations
        @initialized  = false
        @translations = nil
        @file_mtimes  = {}
      end
      
      def file_mtimes
        @file_mtimes ||= {}
      end
      
      def load_file(filename)
        super
        record_mtime_of(filename)
      end
      
      def record_mtime_of(filename)
        file_mtimes[filename] = File.mtime(filename)
      end

      def stale_translation_file?(filename)
        (mtime = file_mtimes[filename]).nil? || !File.file?(filename) || mtime < File.mtime(filename)
      end

      def stale?
        translation_path_removed? || translation_file_updated_or_added?
      end

      def translation_path_removed?
        (file_mtimes.keys - load_paths).any?
      end

      def translation_file_updated_or_added?
        load_paths.any? {|path| stale_translation_file?(path)}
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
thedarkone-i18n-0.2.0 lib/i18n/backend/lazy_reloading.rb