Search code examples
ruby-on-railscachinginternationalization

How to expire fragment cache when locale changes?


I'm trying to use fragment cache to cache the footer and navigation bar on a Ruby on Rails Site that uses I18n. The problem is, changing the language then shows you the footer and navigation bar in the wrong language. How do you go about expiring fragment cache when locale changes?


Solution

  • Rather than expiring the fragment cache, you should make the locale part of the cache key, i.e. something like

    cache :locale => I18n.locale, ... do
      ...
    end
    

    This way different users can see different language versions of the footer/navigation bar but all will see cached versions.