Replace instance variable usage with a method
We usually use this approach because methods are easier to override and stub.
This commit is contained in:
@@ -5,11 +5,6 @@ class RemoteTranslations::Microsoft::Client
|
|||||||
CHARACTERS_LIMIT_PER_REQUEST = 5000
|
CHARACTERS_LIMIT_PER_REQUEST = 5000
|
||||||
PREVENTING_TRANSLATION_KEY = "notranslate".freeze
|
PREVENTING_TRANSLATION_KEY = "notranslate".freeze
|
||||||
|
|
||||||
def initialize
|
|
||||||
api_key = Tenant.current_secrets.microsoft_api_key
|
|
||||||
@client = TranslatorText::Client.new(api_key)
|
|
||||||
end
|
|
||||||
|
|
||||||
def call(fields_values, locale)
|
def call(fields_values, locale)
|
||||||
texts = prepare_texts(fields_values)
|
texts = prepare_texts(fields_values)
|
||||||
valid_locale = RemoteTranslations::Microsoft::AvailableLocales.parse_locale(locale)
|
valid_locale = RemoteTranslations::Microsoft::AvailableLocales.parse_locale(locale)
|
||||||
@@ -28,12 +23,16 @@ class RemoteTranslations::Microsoft::Client
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def client
|
||||||
|
@client ||= TranslatorText::Client.new(Tenant.current_secrets.microsoft_api_key)
|
||||||
|
end
|
||||||
|
|
||||||
def request_translation(texts, locale)
|
def request_translation(texts, locale)
|
||||||
response = []
|
response = []
|
||||||
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(texts, to: locale)
|
||||||
else
|
else
|
||||||
texts.each do |text|
|
texts.each do |text|
|
||||||
response << translate_text(text, locale)
|
response << translate_text(text, locale)
|
||||||
@@ -46,7 +45,7 @@ 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([fragment], to: locale)
|
||||||
end.flatten
|
end.flatten
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user