mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Implement cache_with_locale helper for fragment caching with locales
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user