Fragment collection caching with localized app

We started to introduce fragment caching in our app as follows:

        = render partial: "memberships/program", 
                 collection: @programs, 
                 cached: true

However the app is bilingual and when switching the language it keeps the data from the other language (e.g. both the English and French page show the text in English).

As a workaround to restore the correct language, the code above can be expanded as follows:

        - @programs.each do |program|
          - cache [I18n.locale, program] do
            = render partial: "memberships/program", 
                     locals: { program: program }

But this degrades the performance compared to the first snippet according to this article and is not as succint.

Is there any way to get back the performance and succinctness of the first snippet while taking into account I18n.locale?

One possibility might be to override your model’s cache_key_with_version to take the current locale into account.

I would suggest benchmarking yourself by the way. There is a high chance that the difference in performance is not worth the hassle. And of course, the ‘succinctness’ can be obtained again by abstracting it away in a helper function.

Thanks, I gave a quick try with this:

  # app/models/program.rb
  def cache_key_with_version
    "#{super}-#{I18n.locale}"
  end

But with a breakpoint I see that this method is not called and the issue persists.

As for benchmarking at least I run in dev with rack-mini-profiler so I have a general idea whether caching is doing its job or not (page load times gets shown in top-left corner).