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

user ruby 2.7 'Forward all arguments' syntax: (...) to forward arguments #78

Merged
merged 1 commit into from
May 21, 2024

Conversation

rocket-turtle
Copy link
Contributor

If the key is omitted in the translation call and a default is given, the result is wrong and confusing.

PhraseApp::InContextEditor::BackendService.new.translate(default: "first fallback")
"Translation missing: de.{:default=>\"first fallback\"}"

In Rails till 7.1 this is the case in the ActionView::Helpers::TranslationHelper#translate call:
rails/rails@58c8a93

The params can be passed arround using the 'Forward all arguments' syntax: (...)

https://rubyreferences.github.io/rubychanges/2.7.html#keyword-argument-related-changes

@rocket-turtle rocket-turtle requested a review from a team as a code owner April 30, 2024 13:51
(args.last.is_a?(Hash) && args.last[:resolve] == false)
end

def given_key_from_args(args)
extract_normalized_key_from_args(args)
end

def phraseapp_delegate_for(args)
def phraseapp_delegate_for(*args)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed method so it behaves the same as befor.

end

def has_been_forced_to_resolve_with_phraseapp?(args)
def has_been_forced_to_resolve_with_phraseapp?(*args)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed method so it behaves the same as befor.

@@ -71,7 +70,7 @@
it { is_expected.to eql i18n_translation }

context "given arguments other than key_name" do
let(:args) { [key_name, locale: :ru] }
subject { phraseapp_service.translate(key_name, locale: :ru) }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If phrase is disabled the params need to be real keys_word args so I changed all places

Copy link
Contributor

@Bilka2 Bilka2 May 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: This is a behaviour change for direct calls to I18n.t when this gem is installed but disabled. Right now on master, I18n.t("foo", {bar: "baz"}) works. With this PR, I18n.t("foo", {bar: "baz"}) errors.

The new behaviour of this PR matches the behaviour of t in views (https://api.rubyonrails.org/classes/ActionView/Helpers/TranslationHelper.html#method-i-translate) both without the gem and with the disabled gem, and I18n.t when this gem is not installed - they all error when a hash is provided instead of keyword arguments.

So I think this is a good change to make.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gem disabled

>  PhraseApp::InContextEditor.enabled?
 => false
 > I18n.t("foo", { bar: "baz" })
 => "Translation missing: de.foo"

Gem not installed

> I18n.t("foo", { bar: "baz" })
.../ruby-3.2.3@mhmr/gems/i18n-1.14.4/lib/i18n.rb:210:in `translate': wrong number of arguments (given 2, expected 0..1) (ArgumentError)

So this could be a breaking change for some apps and should be released in a major version.

Thanks for the review.

let(:context_key_translation) { double }
let(:args) { [:key, {scope: [:context]}] }
context "default string given without key" do
subject { phraseapp_service.translate(default: "first fallback") }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That spec is new and was failing

Copy link
Contributor

@Bilka2 Bilka2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can confirm the issue, otwarchive monkeypatched it. I disabled the linked monkeypatch and then tested this PR in my otwarchive dev environment (Ruby 3.1): I can confirm that this PR fixes the issue.

@Varpuspaavi Varpuspaavi merged commit 99ae28e into phrase:master May 21, 2024
2 checks passed
github-actions bot pushed a commit that referenced this pull request May 21, 2024
# [3.0.0](v2.1.2...v3.0.0) (2024-05-21)

* BREAKING CHANGE: Use ruby 2.7 'Forward all arguments' syntax: (...) to forward arguments (#78) ([99ae28e](99ae28e)), closes [#78](#78)

### BREAKING CHANGES

* Hash arguments will throw an error. Keyword arguments must be used instead.
@Varpuspaavi
Copy link
Contributor

Thank you for your contribution 🙏

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.

None yet

3 participants