Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix caching of missed translations #45572

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions actionview/lib/action_view/helpers/translation_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def translate(key, **options)

translated = ActiveSupport::HtmlSafeTranslation.translate(key, **options, default: default)

break translated unless translated.equal?(MISSING_TRANSLATION)
break translated unless translated == MISSING_TRANSLATION

if alternatives.present? && !alternatives.first.is_a?(Symbol)
break alternatives.first && I18n.translate(**options, default: alternatives)
Expand Down Expand Up @@ -119,7 +119,7 @@ def localize(object, **options)
alias :l :localize

private
MISSING_TRANSLATION = Object.new
MISSING_TRANSLATION = -(2**60)
private_constant :MISSING_TRANSLATION

NO_DEFAULT = [].freeze
Expand Down
22 changes: 22 additions & 0 deletions actionview/test/template/translation_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -405,4 +405,26 @@ def test_translate_does_not_change_options
translate(:"translations.missing", **options)
assert_equal({}, options)
end

def test_translate_caching_backend
caching_backend = Class.new(I18n::Backend::Simple) do
include I18n::Backend::Cache
end

previous_backend = I18n.backend
previous_cache_store = I18n.cache_store

I18n.backend = caching_backend.new
I18n.backend.store_translations(:en, translations: { foo: "Foo" })
I18n.cache_store = ActiveSupport::Cache.lookup_store(:memory_store)

assert_equal "Foo", translate(:"translations.foo")

expected = '<span class="translation_missing" title="translation missing: en.translations.missing">Missing</span>'
assert_equal expected, translate(:"translations.missing")
assert_equal expected, translate(:"translations.missing") # returns cached translation
ensure
I18n.backend = previous_backend
I18n.cache_store = previous_cache_store
end
end
2 changes: 1 addition & 1 deletion activemodel/lib/active_model/naming.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def uncountable?
end

private
MISSING_TRANSLATION = Object.new # :nodoc:
MISSING_TRANSLATION = -(2**60) # :nodoc:

def _singularize(string)
ActiveSupport::Inflector.underscore(string).tr("/", "_")
Expand Down
2 changes: 1 addition & 1 deletion activemodel/lib/active_model/translation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def lookup_ancestors
ancestors.select { |x| x.respond_to?(:model_name) }
end

MISSING_TRANSLATION = Object.new # :nodoc:
MISSING_TRANSLATION = -(2**60) # :nodoc:

# Transforms attribute names into a more human format, such as "First name"
# instead of "first_name".
Expand Down