From be48a6c295691d8a8dc288ecc89aa444f6440755 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Sun, 30 Apr 2023 00:36:17 +0100 Subject: [PATCH] Implement cache_with_locale helper for fragment caching with locales --- app/helpers/application_helper.rb | 16 ++++++++++++++++ config/application.rb | 6 ++++++ 2 files changed, 22 insertions(+) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index afefbf5bf5..5d493c5a32 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -68,4 +68,20 @@ module ApplicationHelper wicked_pdf_stylesheet_pack_tag(source) end end + + def cache_with_locale(key = nil, options = {}, &block) + cache(cache_key_with_locale(key, I18n.locale), options) do + yield(block) + end + end + + def cache_key_with_locale(key, locale) + Array.wrap(key) + [locale.to_s, locale_digest(locale)] + end + + private + + def locale_digest(locale) + Rails.application.config.x.i18n_digests[locale] + end end diff --git a/config/application.rb b/config/application.rb index 101a35e2e2..943cc05474 100644 --- a/config/application.rb +++ b/config/application.rb @@ -184,6 +184,12 @@ module Openfoodnetwork config.i18n.available_locales = OpenFoodNetwork::I18nConfig.available_locales I18n.locale = config.i18n.locale = config.i18n.default_locale + # Calculate digests for locale files so we can know when they change + config.i18n.available_locales.each do |locale| + (config.x.i18n_digests ||= {})[locale.to_sym] = + Digest::MD5.hexdigest(File.read(Rails.root.join("config/locales/#{locale}.yml"))) + end + # Setting this to true causes a performance regression in Rails 3.2.17 # When we're on a version with the fix below, we can set it to true # https://github.com/svenfuchs/i18n/issues/230