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

Convert translation key to string as necessary #40773

Merged
merged 1 commit into from Dec 9, 2020
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
1 change: 1 addition & 0 deletions actionview/lib/action_view/helpers/translation_helper.rb
Expand Up @@ -69,6 +69,7 @@ module TranslationHelper
#
def translate(key, **options)
return key.map { |k| translate(k, **options) } if key.is_a?(Array)
key = key.to_s unless key.is_a?(Symbol)

alternatives = if options.key?(:default)
options[:default].is_a?(Array) ? options.delete(:default).compact : [options.delete(:default)]
Expand Down
6 changes: 6 additions & 0 deletions actionview/test/template/translation_helper_test.rb
Expand Up @@ -71,6 +71,12 @@ def test_delegates_localize_to_i18n
assert_equal "Tue, 08 Jul 2008 12:18:38 +0000", localize(@time, locale: "en")
end

def test_converts_key_to_string_as_necessary
key = Struct.new(:to_s).new("translations.foo")
assert_equal "Foo", translate(key)
assert_equal key, translate(:"translations.missing", default: key)
end

def test_returns_missing_translation_message_without_span_wrap
old_value = ActionView::Base.debug_missing_translation
ActionView::Base.debug_missing_translation = false
Expand Down