JohnSmith1976
JohnSmith1976

Reputation: 666

Passing a dynamic set of interpolation arguments to rails-i18n?

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?

Upvotes: 3

Views: 2379

Answers (1)

Christopher Oezbek
Christopher Oezbek

Reputation: 26303

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

Upvotes: 2

Related Questions