Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3.1 i18n and memcache

In my Rails 3.1.2 project I use ActiveRecord as I18n.backend. I want to cache values in MemCache. But I have strange behaviour differences between my development and beta environment.

My 'config/initializers/i18n.rb'

# https://github.com/svenfuchs/i18n-active_record/blob/master/lib/i18n/backend/active_record/translation.rb
require 'i18n/backend/active_record'
require 'i18n_cache_backend'
I18n.default_locale = :en
I18n.backend.class.send(:include, I18n::Backend::Fallbacks)
active_record_backend = I18n::Backend::ActiveRecord.new
active_record_backend.class.send(:include, I18n::Backend::Fallbacks)
I18n::Backend::ActiveRecord.send :include, I18n::Backend::Cache
I18n.cache_store = Rails.application.config.i18n_cache_store
I18n.backend = I18n::Backend::Chain.new(active_record_backend, I18n.backend)

My 'lib/i18n_cache_backend', overwritten :store_translation method to clean-up cache.

module I18n::Backend::Cache

  def store_translations(locale, data, options = {})
    flatten_keys(data, true) do |key, value|
      c_key = cache_key(locale, key.to_s, options)
      I18n.cache_store.delete c_key
    end
    super
  end
end

In 'config/environments/development.rb' I have:

config.i18n_cache_store = ActiveSupport::Cache.lookup_store(:mem_cache_store,
  'localhost', :namespace => "development")

In 'config/environments/beta.rb' I have:

config.i18n_cache_store = ActiveSupport::Cache.lookup_store(:mem_cache_store,
  'memcache.v.l', :namespace => "beta")

In development rails console:

ruby-1.8.7-p352 :008 > I18n.cache_store.clear
=> [<MemCache::Server: localhost:11211 [1] (CONNECTED)>]
ruby-1.8.7-p352 :009 > helper.t 'main.wire'
[SQL QUERY GOES HERE]
=> "Wire"
ruby-1.8.7-p352 :010 > helper.t 'main.wire'
=> "Wire"
ruby-1.8.7-p352 :011 > I18n.backend.store_translations(:en, {:main => {:wire => "foo bar   baz"}}, {:rescue_format=>:html})
[SQL UPDATES GOES HERE]
=> {:"main.wire"=>"foo bar baz"}
ruby-1.8.7-p352 :012 > helper.t 'main.wire'
[SQL QUERY GOES HERE]
=> "foo bar baz"
ruby-1.8.7-p352 :013 > helper.t 'main.wire'
=> "foo bar baz"

In beta env rails console:

ruby-1.8.7-p352 :001 > I18n.cache_store.clear
=> [<MemCache::Server: memcache.v.l:11211 [1] (CONNECTED)>]
ruby-1.8.7-p352 :002 > helper.t 'main.wire'
[SQL QUERY GOES HERE]
=> "Wire"
ruby-1.8.7-p352 :003 > helper.t 'main.wire'
=> "Wire"
ruby-1.8.7-p352 :004 > I18n.backend.store_translations(:en, {:main => {:wire => "foo bar   baz"}}, {:rescue_format=>:html})
[SQL UPDATES GOES HERE]
=> {:"main.wire"=>"foo bar baz"}
ruby-1.8.7-p352 :005 > helper.t 'main.wire'
=> "Wire"
ruby-1.8.7-p352 :006 > helper.t 'main.wire'
=> "Wire"
ruby-1.8.7-p352 :007 > I18n.cache_store.clear
=> [<MemCache::Server: memcache.v.l:11211 [1] (CONNECTED)>]
ruby-1.8.7-p352 :008 >  helper.t 'main.wire'
[SQL QUERY GOES HERE]
=> "foo bar baz"
ruby-1.8.7-p352 :009 > helper.t 'main.wire'
=> "foo bar baz"

Anybody have an idea? In development cache for translation is clrear after every :store_translation, in beta cached value is not refreshed unless I do I18n.cache_store.clear

like image 686
Sebastian Avatar asked Dec 07 '25 20:12

Sebastian


1 Answers

I found a solution for my problem. In beta and production environemtn there is config.i18n.fallbacks = true with results with other options set passed to I18n.translate in ActionView::Helpers::TranslationHelper#translate helper.

I used to do

I18n.backend.store_translations(:en, {:main => {:wire => "foo bar   baz"}}, {:rescue_format=>:html})

but correct is:

I18n.backend.store_translations(:en, {:main => {:wire => "foo bar   baz"}}, {:rescue_format=>:html, :fallback => true})
like image 194
Sebastian Avatar answered Dec 09 '25 12:12

Sebastian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!