Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should not allow noop arguments for I18n.t #545

Merged
merged 1 commit into from
Jan 1, 2021

Conversation

kamipo
Copy link
Contributor

@kamipo kamipo commented Jan 1, 2021

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 *.

def translate(key = nil, *, throw: false, raise: false, locale: nil, **options) # TODO deprecate :raise

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.

Related ruby-i18n#501, ruby-i18n#500, and ruby-i18n#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.
@radar radar force-pushed the should_not_allow_noop_arguments branch from e66613e to c4dfcda Compare January 1, 2021 22:29
@radar
Copy link
Collaborator

radar commented Jan 1, 2021

Updated to the latest master to ensure that this PR has checks ran for Ruby 3 + Rails 6.1. The code looks good to me. Let's see what CI says.

@radar
Copy link
Collaborator

radar commented Jan 1, 2021

CI's happy. Let's go.

@radar radar merged commit d8ecb5a into ruby-i18n:master Jan 1, 2021
@kamipo kamipo deleted the should_not_allow_noop_arguments branch January 2, 2021 00:30
ChrisBAshton added a commit to alphagov/collections that referenced this pull request Jan 13, 2021
A change was introduced in v1.8.6 to support a change in behaviour
in Ruby 3: ruby-i18n/i18n#545

> 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 `*`.

As such, we have to change our calls to `I18n.t` to take 2 args
rather than 3. This immediately causes an issue whereby if you
pass a count of 2 and no pluralization data, it raises a
`I18n::InvalidPluralizationData`, so we've had to catch that and
set the response to `nil`, which appears to be the behaviour we
were expecting prior.
Wynndow added a commit to alphagov/verify-frontend that referenced this pull request Mar 3, 2021
We've bumped the version of the `i18n` module from 1.8.5 to 1.8.9.
Version [1.8.6](https://github.com/ruby-i18n/i18n/releases/tag/v1.8.6)
introduced a change to the parameters for the `translate` method. It
removed the `*` operator, which allows any number of args to be passed
in. These args were just discarded.

You can see the change to the source here: ruby-i18n/i18n#545

We were passing in an extra arg in a couple of places in our tests. This
removes them and prevents an `ArgumentError` being thrown.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants