Implement cache_with_locale helper for fragment caching with locales

This commit is contained in:
Matt-Yorkley
2023-04-30 00:36:17 +01:00
parent 11f45dc2f8
commit be48a6c295
2 changed files with 22 additions and 0 deletions

View File

@@ -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

View File

@@ -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