From e38c1d637b8ad98b33d6c63e9274de1f3d844aaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Sun, 13 Dec 2020 14:33:35 +0100 Subject: [PATCH] Cache content cache key on a per-request basis We do a similar thing with the settings helper, caching settings on a per-request basis. Using an instance variable in a helper reduces the amount of times we need to calculate the cache key during a single request. Even if Rails caches SQL queries per request, the test suite is faster with this change, and we get rid of many redundant queries in the logs. --- config/initializers/i18n_translation.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/config/initializers/i18n_translation.rb b/config/initializers/i18n_translation.rb index e681570a7..ae43e5fc0 100644 --- a/config/initializers/i18n_translation.rb +++ b/config/initializers/i18n_translation.rb @@ -8,7 +8,11 @@ module ActionView def t(key, options = {}) current_locale = options[:locale].presence || I18n.locale - translation = I18nContent.translations_hash(current_locale)[key] + + @i18n_content_translations ||= {} + @i18n_content_translations[current_locale] ||= I18nContent.translations_hash(current_locale) + + translation = @i18n_content_translations[current_locale][key] if translation.present? translation % options