Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Broken LibreTranslate support #128

Merged
merged 4 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 58 additions & 28 deletions services/discourse_translator/libretranslate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,9 @@
require "json"

module DiscourseTranslator
class Google < Base
TRANSLATE_URI = "https://www.googleapis.com/language/translate/v2".freeze
DETECT_URI = "https://www.googleapis.com/language/translate/v2/detect".freeze
SUPPORT_URI = "https://www.googleapis.com/language/translate/v2/languages".freeze
class LibreTranslate < Base
MAXLENGTH = 5000

# Hash which maps Discourse's locale code to Google Translate's locale code found in
# https://cloud.google.com/translate/docs/languages
SUPPORTED_LANG_MAPPING = {
en: "en",
en_GB: "en",
Expand Down Expand Up @@ -55,8 +50,8 @@ class Google < Base
th: "th",
uk: "uk",
uz: "uz",
zh_CN: "zh-CN",
zh_TW: "zh-TW",
zh_CN: "zh",
zh_TW: "zh",
tr_TR: "tr",
pt_BR: "pt",
pl_PL: "pl",
Expand All @@ -65,31 +60,48 @@ class Google < Base
fa_IR: "fa",
}

def self.translate_uri
SiteSetting.translator_libretranslate_endpoint + "/translate"
end

def self.detect_uri
SiteSetting.translator_libretranslate_endpoint + "/detect"
end

def self.support_uri
SiteSetting.translator_libretranslate_endpoint + "/languages"
end

def self.access_token_key
"google-translator"
"libretranslate-translator"
end

def self.access_token
SiteSetting.translator_google_api_key ||
(raise TranslatorError.new("NotFound: Google Api Key not set."))
SiteSetting.translator_libretranslate_api_key
end

def self.detect(post)
post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD] ||= result(
DETECT_URI,
q: post.cooked.truncate(MAXLENGTH, omission: nil),
)[
"detections"
][
0
].max { |a, b| a.confidence <=> b.confidence }[
"language"
]
res =
result(
detect_uri,
q:
ActionController::Base
.helpers
.strip_tags(post.cooked)
.truncate(MAXLENGTH, omission: nil),
)

if !res.empty?
post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD] ||= res[0]["language"]
else
post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD] ||= "en"
end
end

def self.translate_supported?(source, target)
res = result(SUPPORT_URI, target: SUPPORTED_LANG_MAPPING[target])
res["languages"].any? { |obj| obj["language"] == source }
lang = SUPPORTED_LANG_MAPPING[target]
res = get(support_uri)
res.any? { |obj| obj["code"] == source } && res.any? { |obj| obj["code"] == lang }
end

def self.translate(post)
Expand All @@ -101,21 +113,39 @@ def self.translate(post)
from_custom_fields(post) do
res =
result(
TRANSLATE_URI,
translate_uri,
q: post.cooked.truncate(MAXLENGTH, omission: nil),
source: detected_lang,
target: SUPPORTED_LANG_MAPPING[I18n.locale],
format: "html",
)
res["translations"][0]["translatedText"]
res["translatedText"]
end

[detected_lang, translated_text]
end

def self.result(url, body)
body[:key] = access_token
def self.get(url)
begin
response = Excon.get(url)
body = JSON.parse(response.body)
status = response.status
rescue JSON::ParserError, Excon::Error::Socket, Excon::Error::Timeout
body = I18n.t("translator.not_available")
status = 500
end

if status != 200
raise TranslatorError.new(body || response.inspect)
else
body
end
end

def self.result(url, body)
begin
body[:api_key] = access_token

response =
Excon.post(
url,
Expand All @@ -135,7 +165,7 @@ def self.result(url, body)
if status != 200
raise TranslatorError.new(body || response.inspect)
else
body["data"]
body
end
end
end
Expand Down
62 changes: 62 additions & 0 deletions spec/services/libretranslate_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe DiscourseTranslator::LibreTranslate do
let(:mock_response) { Struct.new(:status, :body) }

describe ".access_token" do
describe "when set" do
api_key = "12345"
before { SiteSetting.translator_libretranslate_api_key = api_key }
it "should return back translator_libretranslate_api_key" do
expect(described_class.access_token).to eq(api_key)
end
end
end

describe ".translate_supported?" do
it "should equate source language to target" do
SiteSetting.translator_libretranslate_endpoint = "http://localhost:5000"
source = "en"
target = :fr

data = [{ code: "en" }, { code: "fr" }]

Excon.expects(:get).returns(mock_response.new(200, data.to_json))
expect(described_class.translate_supported?(source, target)).to be true
end
end

describe ".translate" do
let(:post) { Fabricate(:post) }

before { SiteSetting.translator_libretranslate_endpoint = "http://localhost:5000" }

it "raises an error on failure" do
described_class.expects(:access_token).returns("12345")
described_class.expects(:detect).returns("en")

Excon.expects(:post).returns(
mock_response.new(
400,
{
error: "something went wrong",
error_description: "you passed in a wrong param",
}.to_json,
),
)

expect { described_class.translate(post) }.to raise_error DiscourseTranslator::TranslatorError
end

it "raises an error when the response is not JSON" do
described_class.expects(:access_token).returns("12345")
described_class.expects(:detect).returns("en")

Excon.expects(:post).returns(mock_response.new(413, "<html><body>some html</body></html>"))

expect { described_class.translate(post) }.to raise_error DiscourseTranslator::TranslatorError
end
end
end
Loading