Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Network traffic will always be slower than local work. You might consider an in-memory cache, and then just pull the current localization version on each request (or even just on a short timer) to determine whether to invalidate the cache. It looks like there's a Memoization module (per <a href="https://github.com/svenfuchs/i18n/blob/master/lib/i18n/backend/key_value.rb" rel="nofollow">the source here</a>) that you can just mix into an I18n interface. Then, we just tweak the <code>#lookup</code> method so that every 5 minutes, it checks Redis for an updated locale version, and ensures that it increments the locale version when new translations are saved.</p> <p>This gives you an in-memory cache of all your translations so it's a very fast lookup, while giving you the ability to make translation changes on-the-fly - your translations may take up to 5 minutes to update, but you don't have to do any explicit cache purging.</p> <p>If you wanted, you could make it do the check on every request with a <code>before_filter</code> rather than just using the lazy 5-minute expiration, which means more requests to redis, but you wouldn't see any stale translations.</p> <pre><code>module I18n module Backend class CachedKeyValueStore &lt; KeyValue include Memoize def store_translations(locale, data, options = {}) @store.incr "locale_version:#{locale}" reset_memoizations!(locale) super end def lookup(locale, key, scope = nil, options = {}) ensure_freshness(locale) 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 ensure_freshness(locale) @last_check ||= 0 if @last_check &lt; 5.minutes.ago @last_check = Time.now current_version = @store.get "locale_version:#{locale}" if @last_version != current_version reset_memoizations! locale @last_version = current_version end end end end end end </code></pre> <p>I just hacked this up from reading the I18n source, and I haven't tested it at all, so it might need some work, but I think it communicates the idea well enough.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload