Skip to content

Commit

Permalink
user ruby 2.7 'Forward all arguments' syntax: (...) to forward arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
rocket-turtle authored and Jonas Schoeler committed Apr 30, 2024
1 parent bb57f6f commit f8dcfb4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 47 deletions.
4 changes: 2 additions & 2 deletions lib/phraseapp-in-context-editor-ruby/adapters/i18n.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
if defined?(I18n)
module I18n
class << self
def translate_with_phraseapp(*args)
PhraseApp::InContextEditor.backend.translate(*args)
def translate_with_phraseapp(...)
PhraseApp::InContextEditor.backend.translate(...)
end
alias_method :translate_without_phraseapp, :translate
alias_method :translate, :translate_with_phraseapp
Expand Down
36 changes: 8 additions & 28 deletions lib/phraseapp-in-context-editor-ruby/backend_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,49 +9,29 @@ def initialize(args = {})
self
end

def translate(*args)
if to_be_translated_without_phraseapp?(args)
# *Ruby 2.7+ keyword arguments warning*
#
# This method uses keyword arguments.
# There is a breaking change in ruby that produces warning with ruby 2.7 and won't work as expected with ruby 3.0
# The "hash" parameter must be passed as keyword argument.
#
# Good:
# I18n.t(:salutation, :gender => 'w', :name => 'Smith')
# I18n.t(:salutation, **{ :gender => 'w', :name => 'Smith' })
# I18n.t(:salutation, **any_hash)
#
# Bad:
# I18n.t(:salutation, { :gender => 'w', :name => 'Smith' })
# I18n.t(:salutation, any_hash)
#
kw_args = args[1]
if kw_args.present?
I18n.translate_without_phraseapp(args[0], **kw_args)
else
I18n.translate_without_phraseapp(args[0])
end
def translate(...)
if to_be_translated_without_phraseapp?(...)
I18n.translate_without_phraseapp(...)
else
phraseapp_delegate_for(args)
phraseapp_delegate_for(...)
end
end

protected

def to_be_translated_without_phraseapp?(args)
PhraseApp::InContextEditor.disabled? || has_been_forced_to_resolve_with_phraseapp?(args)
def to_be_translated_without_phraseapp?(...)
PhraseApp::InContextEditor.disabled? || has_been_forced_to_resolve_with_phraseapp?(...)
end

def has_been_forced_to_resolve_with_phraseapp?(args)
def has_been_forced_to_resolve_with_phraseapp?(*args)
(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)
key = given_key_from_args(args)
return nil unless present?(key)

Expand Down
32 changes: 15 additions & 17 deletions spec/phraseapp-in-context-editor-ruby/backend_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@
allow(I18n).to receive(:translate_without_phraseapp).with(key_name).and_return(i18n_translation)
end

subject { phraseapp_service.translate(*args) }

context "phrase is enabled" do
before(:each) do
PhraseApp::InContextEditor.enabled = true
end

context "resolve: false given as argument" do
let(:args) { [key_name, resolve: false] }
subject { phraseapp_service.translate(key_name, resolve: false) }

before(:each) do
allow(I18n).to receive(:translate_without_phraseapp).with(key_name, resolve: false).and_return(i18n_translation)
Expand All @@ -34,35 +32,36 @@
end

context "resolve: true given as argument" do
let(:args) { [key_name, resolve: true] }
subject { phraseapp_service.translate(key_name, resolve: true) }

it { is_expected.to be_a String }
it { is_expected.to eql "{{__phrase_foo.bar__}}" }
end

describe "different arguments given" do
context "default array given", vcr: {cassette_name: "fetch list of keys filtered by fallback key names"} do
let(:args) { [:key, {default: [:first_fallback, :second_fallback]}] }
subject { phraseapp_service.translate(:key, default: [:first_fallback, :second_fallback]) }

it { is_expected.to eql "{{__phrase_key__}}" }
end

context "default string given" do
let(:args) { [:key, {default: "first fallback"}] }
subject { phraseapp_service.translate(:key, default: "first fallback") }

it { is_expected.to eql "{{__phrase_key__}}" }
end

context "scope array given" do
subject { phraseapp_service.translate(:key, scope: [:context]) }
let(:context_key_translation) { double }
let(:args) { [:key, {scope: [:context]}] }

it { is_expected.to eql "{{__phrase_context.key__}}" }
end
end
end

context "phrase is disabled" do
let(:args) { [key_name] }
subject { phraseapp_service.translate(key_name) }

before(:each) do
PhraseApp::InContextEditor.enabled = false
Expand All @@ -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) }
let(:ru_translation) { double }

before(:each) do
Expand All @@ -87,27 +86,26 @@
end

context "default array given" do
let(:args) { [:key, {default: [:first_fallback, :second_fallback]}] }
subject { phraseapp_service.translate(:key, default: [:first_fallback, :second_fallback]) }

it { is_expected.to eql "Translation missing. Options considered were:\n- en.key\n- en.first_fallback\n- en.second_fallback" }
end

context "default string given" do
let(:args) { [:key, {default: "first fallback"}] }
subject { phraseapp_service.translate(:key, default: "first fallback") }

it { is_expected.to eql "first fallback" }
end

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

it { is_expected.to eql "Translation missing: en.context.key" }
it { is_expected.to eql "first fallback" }
end

context "scope array given in rails 3 style" do
context "scope array given" do
subject { phraseapp_service.translate(:key, scope: [:context]) }
let(:context_key_translation) { double }
let(:args) { [:key, scope: [:context]] }

it { is_expected.to eql "Translation missing: en.context.key" }
end
Expand Down

0 comments on commit f8dcfb4

Please sign in to comment.