Change gem from TranslatorText to BingTranslator

TranslatorText isn't compatible with Ruby 3, so we need to use a
different gem.

The 'translator-text' gem response was an array of one or more objects.
Now with the 'bing_translator' gem the response is an array with one or
several translated texts.

We remove the concept of object in the code. And we also remove the
"create_response" method from the specs since it is no longer necessary
to emulate that object and we can simply use arrays with texts to emulate
the new response.
This commit is contained in:
taitus
2023-03-09 06:00:41 +01:00
parent 63d0e316cf
commit c64b49b128
4 changed files with 34 additions and 77 deletions

View File

@@ -9,6 +9,7 @@ gem "airbrake", "~> 13.0.2"
gem "ancestry", "~> 4.2.0" gem "ancestry", "~> 4.2.0"
gem "audited", "~> 5.0.2" gem "audited", "~> 5.0.2"
gem "autoprefixer-rails", "~> 8.2.0" gem "autoprefixer-rails", "~> 8.2.0"
gem "bing_translator", "~> 6.2.0"
gem "cancancan", "~> 3.4.0" gem "cancancan", "~> 3.4.0"
gem "caxlsx", "~> 3.2.0" gem "caxlsx", "~> 3.2.0"
gem "caxlsx_rails", "~> 0.6.3" gem "caxlsx_rails", "~> 0.6.3"
@@ -55,7 +56,6 @@ gem "savon", "~> 2.13.0"
gem "sitemap_generator", "~> 6.3.0" gem "sitemap_generator", "~> 6.3.0"
gem "social-share-button", "~> 1.2.4" gem "social-share-button", "~> 1.2.4"
gem "sprockets", "~> 4.1.1" gem "sprockets", "~> 4.1.1"
gem "translator-text", "~> 0.1.0"
gem "turbolinks", "~> 5.2.1" gem "turbolinks", "~> 5.2.1"
gem "turnout", "~> 2.5.0" gem "turnout", "~> 2.5.0"
gem "uglifier", "~> 4.2.0" gem "uglifier", "~> 4.2.0"

View File

@@ -96,6 +96,8 @@ GEM
parser (>= 2.4) parser (>= 2.4)
smart_properties smart_properties
bindex (0.8.1) bindex (0.8.1)
bing_translator (6.2.0)
json
builder (3.2.4) builder (3.2.4)
bullet (7.0.3) bullet (7.0.3)
activesupport (>= 3.0.0) activesupport (>= 3.0.0)
@@ -178,31 +180,6 @@ GEM
devise (>= 4.3.0, < 5.0) devise (>= 4.3.0, < 5.0)
diff-lcs (1.5.0) diff-lcs (1.5.0)
docile (1.4.0) docile (1.4.0)
dry-configurable (0.7.0)
concurrent-ruby (~> 1.0)
dry-container (0.6.0)
concurrent-ruby (~> 1.0)
dry-configurable (~> 0.1, >= 0.1.3)
dry-core (0.4.7)
concurrent-ruby (~> 1.0)
dry-equalizer (0.2.1)
dry-inflector (0.1.2)
dry-logic (0.4.2)
dry-container (~> 0.2, >= 0.2.6)
dry-core (~> 0.2)
dry-equalizer (~> 0.2)
dry-struct (0.5.1)
dry-core (~> 0.4, >= 0.4.3)
dry-equalizer (~> 0.2)
dry-types (~> 0.13)
ice_nine (~> 0.11)
dry-types (0.13.3)
concurrent-ruby (~> 1.0)
dry-container (~> 0.3)
dry-core (~> 0.4, >= 0.4.4)
dry-equalizer (~> 0.2)
dry-inflector (~> 0.1, >= 0.1.2)
dry-logic (~> 0.4, >= 0.4.2)
email_spec (2.2.0) email_spec (2.2.0)
htmlentities (~> 4.3.3) htmlentities (~> 4.3.3)
launchy (~> 2.1) launchy (~> 2.1)
@@ -309,7 +286,6 @@ GEM
rails-i18n rails-i18n
rainbow (>= 2.2.2, < 4.0) rainbow (>= 2.2.2, < 4.0)
terminal-table (>= 1.5.1) terminal-table (>= 1.5.1)
ice_nine (0.11.2)
image_processing (1.12.2) image_processing (1.12.2)
mini_magick (>= 4.9.5, < 5) mini_magick (>= 4.9.5, < 5)
ruby-vips (>= 2.0.17, < 3) ruby-vips (>= 2.0.17, < 3)
@@ -640,9 +616,6 @@ GEM
thread_safe (0.3.6) thread_safe (0.3.6)
tilt (2.0.10) tilt (2.0.10)
tomlrb (1.3.0) tomlrb (1.3.0)
translator-text (0.1.0)
dry-struct (~> 0.5.0)
httparty (~> 0.15)
turbolinks (5.2.1) turbolinks (5.2.1)
turbolinks-source (~> 5.2) turbolinks-source (~> 5.2)
turbolinks-source (5.2.0) turbolinks-source (5.2.0)
@@ -701,6 +674,7 @@ DEPENDENCIES
ancestry (~> 4.2.0) ancestry (~> 4.2.0)
audited (~> 5.0.2) audited (~> 5.0.2)
autoprefixer-rails (~> 8.2.0) autoprefixer-rails (~> 8.2.0)
bing_translator (~> 6.2.0)
bullet (~> 7.0.3) bullet (~> 7.0.3)
byebug (~> 11.1.3) byebug (~> 11.1.3)
cancancan (~> 3.4.0) cancancan (~> 3.4.0)
@@ -785,7 +759,6 @@ DEPENDENCIES
spring (~> 2.1.1) spring (~> 2.1.1)
spring-commands-rspec (~> 1.0.4) spring-commands-rspec (~> 1.0.4)
sprockets (~> 4.1.1) sprockets (~> 4.1.1)
translator-text (~> 0.1.0)
turbolinks (~> 5.2.1) turbolinks (~> 5.2.1)
turnout (~> 2.5.0) turnout (~> 2.5.0)
uglifier (~> 4.2.0) uglifier (~> 4.2.0)

View File

@@ -1,5 +1,3 @@
require "translator-text"
class RemoteTranslations::Microsoft::Client class RemoteTranslations::Microsoft::Client
include SentencesParser include SentencesParser
CHARACTERS_LIMIT_PER_REQUEST = 5000 CHARACTERS_LIMIT_PER_REQUEST = 5000
@@ -24,7 +22,7 @@ class RemoteTranslations::Microsoft::Client
private private
def client def client
@client ||= TranslatorText::Client.new(Tenant.current_secrets.microsoft_api_key) @client ||= BingTranslator.new(Tenant.current_secrets.microsoft_api_key)
end end
def request_translation(texts, locale) def request_translation(texts, locale)
@@ -32,7 +30,7 @@ class RemoteTranslations::Microsoft::Client
split_response = false split_response = false
if characters_count(texts) <= CHARACTERS_LIMIT_PER_REQUEST if characters_count(texts) <= CHARACTERS_LIMIT_PER_REQUEST
response = client.translate(texts, to: locale) response = client.translate_array(texts, to: locale)
else else
texts.each do |text| texts.each do |text|
response << translate_text(text, locale) response << translate_text(text, locale)
@@ -45,27 +43,26 @@ class RemoteTranslations::Microsoft::Client
def translate_text(text, locale) def translate_text(text, locale)
fragments_for(text).map do |fragment| fragments_for(text).map do |fragment|
client.translate([fragment], to: locale) client.translate_array([fragment], to: locale)
end.flatten end.flatten
end end
def parse_response(response, split_response) def parse_response(response, split_response)
response.map do |object| response.map do |translation|
if split_response if split_response
build_translation(object) build_translation(translation)
else else
get_field_value(object) get_field_value(translation)
end end
end end
end end
def build_translation(objects) def build_translation(translations)
objects.map { |object| get_field_value(object) }.join translations.map { |translation| get_field_value(translation) }.join
end end
def get_field_value(object) def get_field_value(translation)
text = object.translations[0].text notranslate?(translation) ? nil : translation
notranslate?(text) ? nil : text
end end
def prepare_texts(texts) def prepare_texts(texts)

View File

@@ -6,9 +6,9 @@ describe RemoteTranslations::Microsoft::Client do
describe "#call" do describe "#call" do
context "when characters from request are less than the characters limit" do context "when characters from request are less than the characters limit" do
it "response has the expected result" do it "response has the expected result" do
response = create_response("Nuevo título", "Nueva descripción") response = ["Nuevo título", "Nueva descripción"]
expect_any_instance_of(TranslatorText::Client).to receive(:translate).and_return(response) expect_any_instance_of(BingTranslator).to receive(:translate_array).and_return(response)
result = client.call(["New title", "New description"], :es) result = client.call(["New title", "New description"], :es)
@@ -16,9 +16,9 @@ describe RemoteTranslations::Microsoft::Client do
end end
it "response nil has the expected result when request has nil value" do it "response nil has the expected result when request has nil value" do
response = create_response("Notranslate", "Nueva descripción") response = ["Notranslate", "Nueva descripción"]
expect_any_instance_of(TranslatorText::Client).to receive(:translate).and_return(response) expect_any_instance_of(BingTranslator).to receive(:translate_array).and_return(response)
result = client.call([nil, "New description"], :es) result = client.call([nil, "New description"], :es)
@@ -34,13 +34,13 @@ describe RemoteTranslations::Microsoft::Client do
translated_text_es = Faker::Lorem.characters(number: 11) translated_text_es = Faker::Lorem.characters(number: 11)
another_translated_text_es = Faker::Lorem.characters(number: 11) another_translated_text_es = Faker::Lorem.characters(number: 11)
response_text = create_response(translated_text_es) response_text = [translated_text_es]
response_another_text = create_response(another_translated_text_es) response_another_text = [another_translated_text_es]
expect_any_instance_of(TranslatorText::Client).to receive(:translate).exactly(1) expect_any_instance_of(BingTranslator).to receive(:translate_array).exactly(1)
.times .times
.and_return(response_text) .and_return(response_text)
expect_any_instance_of(TranslatorText::Client).to receive(:translate).exactly(1) expect_any_instance_of(BingTranslator).to receive(:translate_array).exactly(1)
.times .times
.and_return(response_another_text) .and_return(response_another_text)
@@ -58,14 +58,14 @@ describe RemoteTranslations::Microsoft::Client do
start_translated_text_es = Faker::Lorem.characters(number: 10) + " " start_translated_text_es = Faker::Lorem.characters(number: 10) + " "
end_translated_text_es = Faker::Lorem.characters(number: 10) end_translated_text_es = Faker::Lorem.characters(number: 10)
translated_text_es = start_translated_text_es + end_translated_text_es translated_text_es = start_translated_text_es + end_translated_text_es
response_start_text = create_response(start_translated_text_es) response_start_text = [start_translated_text_es]
response_end_text = create_response(end_translated_text_es) response_end_text = [end_translated_text_es]
expect_any_instance_of(TranslatorText::Client).to receive(:translate).with([start_text_en], to: :es) expect_any_instance_of(BingTranslator).to receive(:translate_array).with([start_text_en], to: :es)
.exactly(1) .exactly(1)
.times .times
.and_return(response_start_text) .and_return(response_start_text)
expect_any_instance_of(TranslatorText::Client).to receive(:translate).with([end_text_en], to: :es) expect_any_instance_of(BingTranslator).to receive(:translate_array).with([end_text_en], to: :es)
.exactly(1) .exactly(1)
.times .times
.and_return(response_end_text) .and_return(response_end_text)
@@ -77,14 +77,14 @@ describe RemoteTranslations::Microsoft::Client do
another_start_translated_text_es = Faker::Lorem.characters(number: 12) + "." another_start_translated_text_es = Faker::Lorem.characters(number: 12) + "."
another_end_translated_text_es = Faker::Lorem.characters(number: 12) another_end_translated_text_es = Faker::Lorem.characters(number: 12)
another_translated_text_es = another_start_translated_text_es + another_end_translated_text_es another_translated_text_es = another_start_translated_text_es + another_end_translated_text_es
response_another_start_text = create_response(another_start_translated_text_es) response_another_start_text = [another_start_translated_text_es]
response_another_end_text = create_response(another_end_translated_text_es) response_another_end_text = [another_end_translated_text_es]
expect_any_instance_of(TranslatorText::Client).to receive(:translate).with([start_another_text_en], to: :es) expect_any_instance_of(BingTranslator).to receive(:translate_array).with([start_another_text_en], to: :es)
.exactly(1) .exactly(1)
.times .times
.and_return(response_another_start_text) .and_return(response_another_start_text)
expect_any_instance_of(TranslatorText::Client).to receive(:translate).with([end_another_text_en], to: :es) expect_any_instance_of(BingTranslator).to receive(:translate_array).with([end_another_text_en], to: :es)
.exactly(1) .exactly(1)
.times .times
.and_return(response_another_end_text) .and_return(response_another_end_text)
@@ -146,16 +146,3 @@ describe RemoteTranslations::Microsoft::Client do
end end
end end
end end
def create_response(*args)
# response = [#<TranslatorText::Types::TranslationResult translations=[#<TranslatorText::Types::Translation text="Nuevo título" to=:es>] detectedLanguage={"language"=>"en", "score"=>1.0}>, #<TranslatorText::Types::TranslationResult translations=[#<TranslatorText::Types::Translation text="Nueva descripción" to=:es>] detectedLanguage={"language"=>"en", "score"=>1.0}>]
translations = Struct.new(:translations)
text = Struct.new(:text)
response = []
args.each do |text_to_response|
response << translations.new([text.new(text_to_response)])
end
response
end