如何使带有翻译的视图的缓存无效? [英] How can the cache for the views with translations be invalidated?

查看:54
本文介绍了如何使带有翻译的视图的缓存无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下,您有两个使用如下代码的视图:

Imagine you have two views with code like the following:

controller_a / a.html.erb

  <%= content_tag(:div) do %>
     <%= I18n.t "some.key" %>
  <% end %>

controller_b / b.html.erb

  <%= content_tag(:div) do %>
     <%= I18n.t "some.key" %>
  <% end %>

  <%= content_tag(:div) do %>
     <%= I18n.t "some.other_key" %>
  <% end %>

因此, a.html.erb 是在controller_a#a上,而 b.html.erb 在controller_b#b上。这两个动作均由 caches_action 缓存。如何更改 some.key 转换键时,两个视图都无效?我该如何构建通用机制?

So, a.html.erb is on controller_a#a, while b.html.erb is on controller_b#b. Both actions are cached by caches_action. How can I make sure that when I change the some.key translation key, both views are invalidated? How could I build a generic mechanism?

推荐答案

说,在您的 ApplicationController 创建以下类方法(或在lib中,然后扩展 ):

Say, in your ApplicationController create the following class-method (or in a lib and extend by it):

def self.i18n_digest(*scopes)
    Digest::MD5.hexdigest I18n.t(scopes).to_s
end

然后您可以在 caches_action :cache_path 选项>这样:

Then you can use :cache_path option in your caches_action this way:

caches_action :some_action, cache_path: { some_key: i18n_digest('some', 'foo') }

只需确保将语言环境设置为 before_filter 在此语句之前。

Just make sure that you set the locale in a before_filter before this statement.

cache_path 上的文档。

Docs on cache_path.

注意:我正在使用翻译范围('some')以将其所有嵌套消息作为哈希值获取。

Note: I'm using the scope of translation ('some') to get all its nested messages as a hash.

这篇关于如何使带有翻译的视图的缓存无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆