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.
26 lines
621 B
Ruby
26 lines
621 B
Ruby
require "i18n/exceptions"
|
|
require "action_view/helpers/tag_helper"
|
|
|
|
module ActionView
|
|
module Helpers
|
|
module TranslationHelper
|
|
include TagHelper
|
|
|
|
def t(key, options = {})
|
|
current_locale = options[:locale].presence || I18n.locale
|
|
|
|
@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
|
|
else
|
|
translate(key, options)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|