diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index abcb439..af88ec0 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -20,10 +20,11 @@ en: failed: "The translator is unable to translate this language." not_supported: "This language is not supported by the translator." too_long: "This post is too long to be translated by the translator." + not_available: "The translator service is currently not available." microsoft: missing_token: "The translator was unable to retrieve a valid token." missing_key: "No Azure Subscription Key provided." - not_in_group: + not_in_group: user_not_in_group: "You don't belong to a group allowed to translate." poster_not_in_group: "Post wasn't made by an user in a allowlisted group. If empty it is disabled." \ No newline at end of file diff --git a/services/discourse_translator/google.rb b/services/discourse_translator/google.rb index dea4281..4854094 100644 --- a/services/discourse_translator/google.rb +++ b/services/discourse_translator/google.rb @@ -131,7 +131,11 @@ def self.result(url, body) end if response.status != 200 - raise TranslatorError.new(body || response.inspect) + if body && body["error"] + raise TranslatorError.new(body["error"]["message"]) + else + raise TranslatorError.new(response.inspect) + end else body["data"] end diff --git a/services/discourse_translator/libretranslate.rb b/services/discourse_translator/libretranslate.rb index dea4281..d5c280e 100644 --- a/services/discourse_translator/libretranslate.rb +++ b/services/discourse_translator/libretranslate.rb @@ -115,22 +115,24 @@ def self.translate(post) def self.result(url, body) body[:key] = access_token - response = - Excon.post( - url, - body: URI.encode_www_form(body), - headers: { - "Content-Type" => "application/x-www-form-urlencoded", - }, - ) - - body = nil begin + response = + Excon.post( + url, + body: URI.encode_www_form(body), + headers: { + "Content-Type" => "application/x-www-form-urlencoded", + }, + ) + body = JSON.parse(response.body) - rescue JSON::ParserError + status = response.status + rescue JSON::ParserError, Excon::Error::Socket, Excon::Error::Timeout + body = I18n.t("translator.not_available") + status = 500 end - if response.status != 200 + if status != 200 raise TranslatorError.new(body || response.inspect) else body["data"]