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

View File

@@ -96,6 +96,8 @@ GEM
parser (>= 2.4)
smart_properties
bindex (0.8.1)
bing_translator (6.2.0)
json
builder (3.2.4)
bullet (7.0.3)
activesupport (>= 3.0.0)
@@ -178,31 +180,6 @@ GEM
devise (>= 4.3.0, < 5.0)
diff-lcs (1.5.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)
htmlentities (~> 4.3.3)
launchy (~> 2.1)
@@ -309,7 +286,6 @@ GEM
rails-i18n
rainbow (>= 2.2.2, < 4.0)
terminal-table (>= 1.5.1)
ice_nine (0.11.2)
image_processing (1.12.2)
mini_magick (>= 4.9.5, < 5)
ruby-vips (>= 2.0.17, < 3)
@@ -640,9 +616,6 @@ GEM
thread_safe (0.3.6)
tilt (2.0.10)
tomlrb (1.3.0)
translator-text (0.1.0)
dry-struct (~> 0.5.0)
httparty (~> 0.15)
turbolinks (5.2.1)
turbolinks-source (~> 5.2)
turbolinks-source (5.2.0)
@@ -701,6 +674,7 @@ DEPENDENCIES
ancestry (~> 4.2.0)
audited (~> 5.0.2)
autoprefixer-rails (~> 8.2.0)
bing_translator (~> 6.2.0)
bullet (~> 7.0.3)
byebug (~> 11.1.3)
cancancan (~> 3.4.0)
@@ -785,7 +759,6 @@ DEPENDENCIES
spring (~> 2.1.1)
spring-commands-rspec (~> 1.0.4)
sprockets (~> 4.1.1)
translator-text (~> 0.1.0)
turbolinks (~> 5.2.1)
turnout (~> 2.5.0)
uglifier (~> 4.2.0)

View File

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

View File

@@ -6,9 +6,9 @@ describe RemoteTranslations::Microsoft::Client do
describe "#call" do
context "when characters from request are less than the characters limit" 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)
@@ -16,9 +16,9 @@ describe RemoteTranslations::Microsoft::Client do
end
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)
@@ -34,13 +34,13 @@ describe RemoteTranslations::Microsoft::Client do
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_another_text = create_response(another_translated_text_es)
response_text = [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
.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
.and_return(response_another_text)
@@ -58,14 +58,14 @@ describe RemoteTranslations::Microsoft::Client do
start_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
response_start_text = create_response(start_translated_text_es)
response_end_text = create_response(end_translated_text_es)
response_start_text = [start_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)
.times
.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)
.times
.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_end_translated_text_es = Faker::Lorem.characters(number: 12)
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_end_text = create_response(another_end_translated_text_es)
response_another_start_text = [another_start_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)
.times
.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)
.times
.and_return(response_another_end_text)
@@ -146,16 +146,3 @@ describe RemoteTranslations::Microsoft::Client do
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