Skip to content

Commit

Permalink
Merge pull request #40788 from jonathanhefner/translate-nil-key
Browse files Browse the repository at this point in the history
Handle nil translation key
  • Loading branch information
rafaelfranca committed Dec 16, 2020
2 parents 7ee3115 + b8b18bb commit 3701930
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
5 changes: 5 additions & 0 deletions actionview/CHANGELOG.md
@@ -1,3 +1,8 @@
* The `translate` helper now resolves `default` values when a `nil` key is
specified, instead of always returning `nil`.

*Jonathan Hefner*

* Add `config.action_view.image_loading` to configure the default value of
the `image_tag` `:loading` option.

Expand Down
10 changes: 5 additions & 5 deletions actionview/lib/action_view/helpers/translation_helper.rb
Expand Up @@ -69,7 +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)
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 All @@ -78,13 +78,12 @@ def translate(key, **options)
options[:raise] = true if options[:raise].nil? && ActionView::Base.raise_on_missing_translations
default = MISSING_TRANSLATION

translation = while key
translation = while key || alternatives.present?
if alternatives.blank? && !options[:raise].nil?
default = NO_DEFAULT # let I18n handle missing translation
end

key = scope_key_by_partial(key)
first_key ||= key

if html_safe_translation_key?(key)
html_safe_options ||= html_escape_translation_options(options)
Expand All @@ -97,10 +96,11 @@ def translate(key, **options)

break alternatives.first if alternatives.present? && !alternatives.first.is_a?(Symbol)

first_key ||= key
key = alternatives&.shift
end

if key.nil?
if key.nil? && !first_key.nil?
translation = missing_translation(first_key, options)
key = first_key
end
Expand Down Expand Up @@ -130,7 +130,7 @@ def self.i18n_option?(name)
end

def scope_key_by_partial(key)
if key.start_with?(".")
if key&.start_with?(".")
if @current_template&.virtual_path
@_scope_key_by_partial_cache ||= {}
@_scope_key_by_partial_cache[@current_template.virtual_path] ||= @current_template.virtual_path.gsub(%r{/_?}, ".")
Expand Down
10 changes: 10 additions & 0 deletions actionview/test/template/translation_helper_test.rb
Expand Up @@ -77,6 +77,16 @@ def test_converts_key_to_string_as_necessary
assert_equal key, translate(:"translations.missing", default: key)
end

def test_returns_nil_for_nil_key_without_default
assert_nil translate(nil)
end

def test_returns_default_for_nil_key_with_default
assert_equal "Foo", translate(nil, default: "Foo")
assert_equal "Foo", translate(nil, default: :"translations.foo")
assert_predicate translate(nil, default: :"translations.html"), :html_safe?
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

0 comments on commit 3701930

Please sign in to comment.