Rails cache with locale在Rails视图中自动添加当前应用程序区域设置(I18n.locale)作为缓存键的一部分。

Original: https://github.com/igorkasyanchuk/cache_with_locale

Build Status

Rails cache with locale

Automatically adding current application locale (I18n.locale) as a part of caching key in Rails views.

Problem & Solution

For example you have:

  • Rails app with different locales
  • you want to add fragment caching in views

When you have a different locales and you adding a caching you writing code like this

  = cache [@user, I18n.locale] do
    = render @user

As you see you need to add I18n.locale as a part of caching key. And you need to do it everywhere. What if you forget about it in some view?

This gem is a simple solution which allows you to DRY your code. So now you can write

  = cache @user do
    = render @user

And let gem handle all work. You don’t need to specify locale as a cache sufix/prefix.


Just add this gem to your Gemfile.


Add this line to your application’s Gemfile:

gem 'cache_with_locale'

And then execute:

$ bundle


Contribution directions go here.


The gem is available as open source under the terms of the MIT License.


Leave a Reply

Your email address will not be published. Required fields are marked *