Crowdin / Upload translations / upload-translations (push) Skipped
Historical data migration test / test (14-alpine) (push) Canceled after 0s
Historical data migration test / test (15-alpine) (push) Canceled after 0s
Historical data migration test / test (16-alpine) (push) Canceled after 0s
Historical data migration test / test (17-alpine) (push) Canceled after 0s
Bundler Audit / security (push) Canceled after 0s
Haml Linting / lint (push) Canceled after 0s
JavaScript Linting / lint (push) Canceled after 0s
Ruby Linting / lint (push) Canceled after 0s
JavaScript Testing / test (push) Canceled after 0s
Ruby Testing / build (production) (push) Canceled after 0s
Ruby Testing / test (.ruby-version) (push) Canceled after 0s
Ruby Testing / test (3.3) (push) Canceled after 0s
Ruby Testing / test (3.4) (push) Canceled after 0s
Ruby Testing / End to End testing (.ruby-version) (push) Canceled after 0s
Ruby Testing / End to End testing (3.3) (push) Canceled after 0s
Ruby Testing / End to End testing (3.4) (push) Canceled after 0s
Ruby Testing / Elastic Search integration testing (.ruby-version, docker.elastic.co/elasticsearch/elasticsearch:7.17.29) (push) Canceled after 0s
Check i18n / check-i18n (push) Canceled after 0s
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (javascript) (push) Canceled after 0s
CodeQL / Analyze (ruby) (push) Canceled after 0s
Ruby Testing / Elastic Search integration testing (.ruby-version, opensearchproject/opensearch:2) (push) Canceled after 0s
Ruby Testing / Elastic Search integration testing (3.3, docker.elastic.co/elasticsearch/elasticsearch:7.17.29) (push) Canceled after 0s
Ruby Testing / Elastic Search integration testing (3.4, docker.elastic.co/elasticsearch/elasticsearch:7.17.29) (push) Canceled after 0s
Check formatting / lint (push) Canceled after 0s
CSS Linting / lint (push) Canceled after 0s
Ruby Testing / Elastic Search integration testing (.ruby-version, docker.elastic.co/elasticsearch/elasticsearch:8.19.2) (push) Canceled after 0s
Chromatic / Check for relevant changes (push) Canceled after 0s
Chromatic / Run Chromatic (push) Canceled after 0s
Snapshot of upstream tag v4.7.3 (commit 8ff11c2bb7b9f0714e947b10fa3629c6542c7121) from https://github.com/mastodon/mastodon, unmodified.
62 lines
1.7 KiB
Ruby
62 lines
1.7 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class TranslationService::LibreTranslate < TranslationService
|
|
def initialize(base_url, api_key)
|
|
super()
|
|
|
|
@base_url = base_url
|
|
@api_key = api_key
|
|
end
|
|
|
|
def translate(texts, source_language, target_language)
|
|
body = { q: texts, source: source_language.presence || 'auto', target: target_language, format: 'html', api_key: @api_key }.to_json
|
|
request(:post, '/translate', body: body) do |res|
|
|
transform_response(res.body_with_limit, source_language)
|
|
end
|
|
end
|
|
|
|
def languages
|
|
request(:get, '/languages') do |res|
|
|
languages = JSON.parse(res.body_with_limit).to_h do |language|
|
|
[language['code'], language['targets'].without(language['code'])]
|
|
end
|
|
languages[nil] = languages.values.flatten.uniq.sort
|
|
languages
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def request(verb, path, **)
|
|
req = Request.new(verb, "#{@base_url}#{path}", allow_local: true, **)
|
|
req.add_headers('Content-Type': 'application/json')
|
|
req.perform do |res|
|
|
case res.code
|
|
when 429
|
|
raise TooManyRequestsError
|
|
when 403
|
|
raise QuotaExceededError
|
|
when 200...300
|
|
yield res
|
|
else
|
|
raise UnexpectedResponseError
|
|
end
|
|
end
|
|
end
|
|
|
|
def transform_response(json, source_language)
|
|
data = JSON.parse(json)
|
|
raise UnexpectedResponseError unless data.is_a?(Hash)
|
|
|
|
data['translatedText'].map.with_index do |text, index|
|
|
Translation.new(
|
|
text: text,
|
|
detected_source_language: data.dig('detectedLanguage', index, 'language') || source_language,
|
|
provider: 'LibreTranslate'
|
|
)
|
|
end
|
|
rescue JSON::ParserError
|
|
raise UnexpectedResponseError
|
|
end
|
|
end
|