Sha256: 952620fab5de8d8b9beb8a6ff0c456bf56d8c8fdd94f8a8ce5da40f5274cdd50

Contents?: true

Size: 1.36 KB

Versions: 50

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

require 'openssl'

module I18n
  module Backend
    # Overwrites the Base load_file method to cache loaded file contents.
    module CacheFile
      # Optionally provide path_roots array to normalize filename paths,
      # to make the cached i18n data portable across environments.
      attr_accessor :path_roots

      protected

      # Track loaded translation files in the `i18n.load_file` scope,
      # and skip loading the file if its contents are still up-to-date.
      def load_file(filename)
        initialized = !respond_to?(:initialized?) || initialized?
        key = I18n::Backend::Flatten.escape_default_separator(normalized_path(filename))
        old_mtime, old_digest = initialized && lookup(:i18n, key, :load_file)
        return if (mtime = File.mtime(filename).to_i) == old_mtime ||
                  (digest = OpenSSL::Digest::SHA256.file(filename).hexdigest) == old_digest
        super
        store_translations(:i18n, load_file: { key => [mtime, digest] })
      end

      # Translate absolute filename to relative path for i18n key.
      def normalized_path(file)
        return file unless path_roots
        path = path_roots.find(&file.method(:start_with?)) ||
               raise(InvalidLocaleData.new(file, 'outside expected path roots'))
        file.sub(path, path_roots.index(path).to_s)
      end
    end
  end
end

Version data entries

50 entries across 45 versions & 17 rubygems

Version Path
i18n-1.14.6 lib/i18n/backend/cache_file.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/i18n-1.14.5/lib/i18n/backend/cache_file.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/i18n-1.14.1/lib/i18n/backend/cache_file.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/i18n-1.14.1/lib/i18n/backend/cache_file.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/i18n-1.14.1/lib/i18n/backend/cache_file.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/i18n-1.14.5/lib/i18n/backend/cache_file.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/i18n-1.14.5/lib/i18n/backend/cache_file.rb
i18n-1.14.5 lib/i18n/backend/cache_file.rb
i18n-1.14.4 lib/i18n/backend/cache_file.rb
i18n-1.14.3 lib/i18n/backend/cache_file.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/i18n-1.14.1/lib/i18n/backend/cache_file.rb
honeybadger-5.4.0 vendor/bundle/ruby/3.2.0/gems/i18n-1.14.1/lib/i18n/backend/cache_file.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/i18n-1.10.0/lib/i18n/backend/cache_file.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/i18n-1.14.1/lib/i18n/backend/cache_file.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/i18n-1.12.0/lib/i18n/backend/cache_file.rb
honeybadger-5.3.0 vendor/bundle/ruby/3.2.0/gems/i18n-1.14.1/lib/i18n/backend/cache_file.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/i18n-1.14.1/lib/i18n/backend/cache_file.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/i18n-1.14.1/lib/i18n/backend/cache_file.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/i18n-1.14.1/lib/i18n/backend/cache_file.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/i18n-1.12.0/lib/i18n/backend/cache_file.rb