Skip to content

Commit

Permalink
Should not allow noop arguments for I18n.t
Browse files Browse the repository at this point in the history
Related #501, #500, and #471.

In Ruby 3.0, the behavior of `I18n.t("key", { value: "foo" })` has
silently changed to `{ value: "foo" }` is no longer regarded as
`**options` but regarded as noop slpat arguments `*`.

https://github.com/ruby-i18n/i18n/blob/4709391dceab9096d5988576f93935843023a6ef/lib/i18n.rb#L195

So allowing (discarding) noop arguments will be more harmful in Ruby 3.0.

This removes the allowing noop arguments, it makes `{ value: "foo" }`
will raise ArgumentError instead of silently ignored the options.
  • Loading branch information
kamipo committed Jan 1, 2021
1 parent 4709391 commit e66613e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
8 changes: 4 additions & 4 deletions lib/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def eager_load!
# I18n.t(:salutation, { :gender => 'w', :name => 'Smith' })
# I18n.t(:salutation, any_hash)
#
def translate(key = nil, *, throw: false, raise: false, locale: nil, **options) # TODO deprecate :raise
def translate(key = nil, throw: false, raise: false, locale: nil, **options) # TODO deprecate :raise
locale ||= config.locale
raise Disabled.new('t') if locale == false
enforce_available_locales!(locale)
Expand All @@ -217,8 +217,8 @@ def translate(key = nil, *, throw: false, raise: false, locale: nil, **options)

# Wrapper for <tt>translate</tt> that adds <tt>:raise => true</tt>. With
# this option, if no translation is found, it will raise <tt>I18n::MissingTranslationData</tt>
def translate!(key, options = EMPTY_HASH)
translate(key, **options.merge(:raise => true))
def translate!(key, **options)
translate(key, **options, raise: true)
end
alias :t! :translate!

Expand Down Expand Up @@ -281,7 +281,7 @@ def exists?(key, _locale = nil, locale: _locale, **options)
# I18n.transliterate("Jürgen") # => "Juergen"
# I18n.transliterate("Jürgen", :locale => :en) # => "Jurgen"
# I18n.transliterate("Jürgen", :locale => :de) # => "Juergen"
def transliterate(key, *, throw: false, raise: false, locale: nil, replacement: nil, **options)
def transliterate(key, throw: false, raise: false, locale: nil, replacement: nil, **options)
locale ||= config.locale
raise Disabled.new('transliterate') if locale == false
enforce_available_locales!(locale)
Expand Down
6 changes: 3 additions & 3 deletions test/api/override_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

class I18nOverrideTest < I18n::TestCase
module OverrideInverse
def translate(*args, **options)
super(*args, **options).reverse
def translate(key, **options)
super(key, **options).reverse
end
alias :t :translate
end
Expand Down Expand Up @@ -33,7 +33,7 @@ def setup

test "make sure modules can overwrite I18n signature" do
exception = catch(:exception) do
@I18n.t('Hello', 'Welcome message on home page', :tokenize => true, :throw => true)
@I18n.t('Hello', :tokenize => true, :throw => true)
end
assert exception.message
@I18n.extend OverrideSignature
Expand Down

0 comments on commit e66613e

Please sign in to comment.