3

I am upgrading to Rails 6.1.3 and with it, the latest version of rails-i18n (1.8.9).

I used to be able to pass a hash of interpolation arguments like this:

I18n.with_locale(:en) do
  message = I18n.translate("foo", message_args.merge({default: ""}))
end

But this now raises:

ArgumentError (wrong number of arguments (given 2, expected 0..1)):

Passing them as arguments as described works fine:

I18n.with_locale(:en) do
  message = I18n.translate("foo", bar: "baz", default: "")
end

When checking out the gems translate method, it seems it changed between versions:

# 1.5.3
def translate(*args)
  ...
end

# 1.8.9
def translate(key = nil, throw: false, raise: false, locale: nil, **options)
  ...
end

Anyone who knows how I regain my ability to pass a dynamic set of interpolation arguments?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
JohnSmith1976
  • 536
  • 2
  • 12
  • 35

1 Answers1

2

Since the translate method in the translation helper (https://api.rubyonrails.org/classes/ActionView/Helpers/TranslationHelper.html) accepts a keyword argument hash you need to use the double splat operator **. More info in this answer:

https://stackoverflow.com/a/45338680

Christopher Oezbek
  • 23,994
  • 6
  • 61
  • 85