Sha256: b26396c497b4f23d502e6ddfa741a0d9762b6b6149075450675df9146a7bfba4

Contents?: true

Size: 1.02 KB

Versions: 8

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

module FastGettext
  class Cache
    def initialize
      @store = {}
      reload!
    end

    def fetch(key)
      translation = @current[key]
      if translation.nil? # uncached
        @current[key] = yield || false # TODO: get rid of this false hack and cache :missing
      else
        translation
      end
    end

    # TODO: only used for tests, maybe if-else around it ...
    def []=(key, value)
      @current[key] = value
    end

    # key performance gain:
    # - no need to lookup locale on each translation
    # - no need to lookup text_domain on each translation
    # - super-simple hash lookup
    def switch_to(text_domain, locale)
      @store[text_domain] ||= {}
      @store[text_domain][locale] ||= {}
      @store[text_domain][locale][""] = false # ignore gettext meta key when translating
      @current = @store[text_domain][locale]
    end

    def delete(key)
      @current.delete(key)
    end

    def reload!
      @current = {}
      @current[""] = false
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
fast_gettext-3.1.0 lib/fast_gettext/cache.rb
fast_gettext-2.3.0 lib/fast_gettext/cache.rb
fast_gettext-2.2.0 lib/fast_gettext/cache.rb
fast_gettext-2.1.0 lib/fast_gettext/cache.rb
fast_gettext-2.0.3 lib/fast_gettext/cache.rb
fast_gettext-2.0.2 lib/fast_gettext/cache.rb
fast_gettext-2.0.1 lib/fast_gettext/cache.rb
fast_gettext-2.0.0 lib/fast_gettext/cache.rb