Sha256: 04bc811aa19700bc48808b93b89811052a2811478f3d51d52658a2dff4cf19b5

Contents?: true

Size: 1.45 KB

Versions: 98

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

# Memoize module simply memoizes the values returned by lookup using
# a flat hash and can tremendously speed up the lookup process in a backend.
#
# To enable it you can simply include the Memoize module to your backend:
#
#   I18n::Backend::Simple.include(I18n::Backend::Memoize)
#
# Notice that it's the responsibility of the backend to define whenever the
# cache should be cleaned.
module I18n
  module Backend
    module Memoize
      def available_locales
        @memoized_locales ||= super
      end

      def store_translations(locale, data, options = EMPTY_HASH)
        reset_memoizations!(locale)
        super
      end

      def reload!
        reset_memoizations!
        super
      end

      def eager_load!
        memoized_lookup
        available_locales
        super
      end

      protected

        def lookup(locale, key, scope = nil, options = EMPTY_HASH)
          flat_key  = I18n::Backend::Flatten.normalize_flat_keys(locale,
            key, scope, options[:separator]).to_sym
          flat_hash = memoized_lookup[locale.to_sym]
          flat_hash.key?(flat_key) ? flat_hash[flat_key] : (flat_hash[flat_key] = super)
        end

        def memoized_lookup
          @memoized_lookup ||= I18n.new_double_nested_cache
        end

        def reset_memoizations!(locale=nil)
          @memoized_locales = nil
          (locale ? memoized_lookup[locale.to_sym] : memoized_lookup).clear
        end
    end
  end
end

Version data entries

98 entries across 86 versions & 26 rubygems

Version Path
tailscale_middleware-0.0.3 vendor/cache/ruby/3.4.0/gems/i18n-1.14.7/lib/i18n/backend/memoize.rb
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.3.0/gems/i18n-1.14.6/lib/i18n/backend/memoize.rb
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.1.0/gems/i18n-1.14.6/lib/i18n/backend/memoize.rb
i18n-1.14.7 lib/i18n/backend/memoize.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/i18n-1.14.5/lib/i18n/backend/memoize.rb
i18n-1.14.6 lib/i18n/backend/memoize.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/i18n-1.14.5/lib/i18n/backend/memoize.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/i18n-1.14.1/lib/i18n/backend/memoize.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/i18n-1.14.1/lib/i18n/backend/memoize.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/i18n-1.14.1/lib/i18n/backend/memoize.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/i18n-1.14.5/lib/i18n/backend/memoize.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/i18n-1.14.5/lib/i18n/backend/memoize.rb
i18n-1.14.5 lib/i18n/backend/memoize.rb
i18n-1.14.4 lib/i18n/backend/memoize.rb
i18n-1.14.3 lib/i18n/backend/memoize.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/i18n-1.14.1/lib/i18n/backend/memoize.rb
honeybadger-5.4.0 vendor/bundle/ruby/3.2.0/gems/i18n-1.14.1/lib/i18n/backend/memoize.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/i18n-1.12.0/lib/i18n/backend/memoize.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/i18n-1.14.1/lib/i18n/backend/memoize.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/i18n-1.10.0/lib/i18n/backend/memoize.rb