Skip to content

Commit 8877b5f

Browse files
swrobelkaspth
authored andcommitted
Fix translate method with default: nil
```ruby I18n.translate('missing.translation', default: nil) # => nil helper.translate('missing.translation', default: nil) # Before # => "<span class=\"translation_missing\" title=\"translation missing: en.missing.translation\">Translation</span>" # After # => nil ```
1 parent 8818d47 commit 8877b5f

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

actionview/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
* `ActionView::Helpers::TranslationHelper#translate` returns nil when
2+
passed `default: nil` without a translation matching `I18n#translate`.
3+
4+
*Stefan Wrobel*
5+
16
* `OptimizedFileSystemResolver` prefers template details in order of locale,
27
formats, variants, handlers.
38

actionview/lib/action_view/helpers/translation_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ module TranslationHelper
5858
# when you call translate in a template and translators know which keys
5959
# they can provide HTML values for.
6060
def translate(key, **options)
61-
if options.has_key?(:default)
61+
unless options[:default].nil?
6262
remaining_defaults = Array.wrap(options.delete(:default)).compact
6363
options[:default] = remaining_defaults unless remaining_defaults.first.kind_of?(Symbol)
6464
end

actionview/test/template/translation_helper_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,16 @@ def test_translate_with_array_of_array_default
242242
assert_equal [], translation
243243
end
244244

245+
def test_translate_with_false_default
246+
translation = translate(:'translations.missing', default: false)
247+
assert_equal false, translation
248+
end
249+
250+
def test_translate_with_nil_default
251+
translation = translate(:'translations.missing', default: nil)
252+
assert_nil translation
253+
end
254+
245255
def test_translate_does_not_change_options
246256
options = {}
247257
if RUBY_VERSION >= "2.7"

0 commit comments

Comments
 (0)