2011-12-12 100 views
3

在我的Rails 3.1.2项目中,我使用ActiveRecord作为I18n.backend。我想缓存MemCache中的值。但是我的开发和测试环境之间存在奇怪的行为差异。Rails 3.1 i18n和memcache

我的 '配置/初始化/ 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) 

我 '的lib/i18n_cache_backend',改写:store_translation方法来清理缓存。

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 

在 '配置/环境/ development.rb' 我有:

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

在 '配置/环境/ beta.rb' 我有:

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

在发展轨道控制台:

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" 

在公测ENV轨道控制台:

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" 

有人有想法吗?在发展高速缓存翻译是clrear后每次:store_translation,在测试缓冲值不刷新,除非我做I18n.cache_store.clear

回答

1

,我发现我的问题的解决方案。在beta和生产environemtn有config.i18n.fallbacks = true结果与其他选项集传递给ActionView :: Helpers :: TranslationHelper#translate helper中的I18n.translate。

我用来做

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

但正确的是:

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