diff options
Diffstat (limited to 'app/lib')
-rw-r--r-- | app/lib/translation_service.rb | 23 | ||||
-rw-r--r-- | app/lib/translation_service/deepl.rb | 53 | ||||
-rw-r--r-- | app/lib/translation_service/libre_translate.rb | 43 | ||||
-rw-r--r-- | app/lib/translation_service/translation.rb | 5 |
4 files changed, 124 insertions, 0 deletions
diff --git a/app/lib/translation_service.rb b/app/lib/translation_service.rb new file mode 100644 index 000000000..526e26ae5 --- /dev/null +++ b/app/lib/translation_service.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +class TranslationService + class Error < StandardError; end + class NotConfiguredError < Error; end + class TooManyRequestsError < Error; end + class QuotaExceededError < Error; end + class UnexpectedResponseError < Error; end + + def self.configured + if ENV['DEEPL_API_KEY'].present? + TranslationService::DeepL.new(ENV.fetch('DEEPL_PLAN', 'free'), ENV['DEEPL_API_KEY']) + elsif ENV['LIBRE_TRANSLATE_ENDPOINT'].present? + TranslationService::LibreTranslate.new(ENV['LIBRE_TRANSLATE_ENDPOINT'], ENV['LIBRE_TRANSLATE_API_KEY']) + else + raise NotConfiguredError + end + end + + def translate(_text, _source_language, _target_language) + raise NotImplementedError + end +end diff --git a/app/lib/translation_service/deepl.rb b/app/lib/translation_service/deepl.rb new file mode 100644 index 000000000..89ccf01e5 --- /dev/null +++ b/app/lib/translation_service/deepl.rb @@ -0,0 +1,53 @@ +# frozen_string_literal: true + +class TranslationService::DeepL < TranslationService + include JsonLdHelper + + def initialize(plan, api_key) + super() + + @plan = plan + @api_key = api_key + end + + def translate(text, source_language, target_language) + request(text, source_language, target_language).perform do |res| + case res.code + when 429 + raise TooManyRequestsError + when 456 + raise QuotaExceededError + when 200...300 + transform_response(res.body_with_limit) + else + raise UnexpectedResponseError + end + end + end + + private + + def request(text, source_language, target_language) + req = Request.new(:post, endpoint_url, form: { text: text, source_lang: source_language.upcase, target_lang: target_language, tag_handling: 'html' }) + req.add_headers('Authorization': "DeepL-Auth-Key #{@api_key}") + req + end + + def endpoint_url + if @plan == 'free' + 'https://api-free.deepl.com/v2/translate' + else + 'https://api.deepl.com/v2/translate' + end + end + + def transform_response(str) + json = Oj.load(str, mode: :strict) + + raise UnexpectedResponseError unless json.is_a?(Hash) + + Translation.new(text: json.dig('translations', 0, 'text'), detected_source_language: json.dig('translations', 0, 'detected_source_language')&.downcase) + rescue Oj::ParseError + raise UnexpectedResponseError + end +end diff --git a/app/lib/translation_service/libre_translate.rb b/app/lib/translation_service/libre_translate.rb new file mode 100644 index 000000000..66acdeed7 --- /dev/null +++ b/app/lib/translation_service/libre_translate.rb @@ -0,0 +1,43 @@ +# 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(text, source_language, target_language) + request(text, source_language, target_language).perform do |res| + case res.code + when 429 + raise TooManyRequestsError + when 403 + raise QuotaExceededError + when 200...300 + transform_response(res.body_with_limit, source_language) + else + raise UnexpectedResponseError + end + end + end + + private + + def request(text, source_language, target_language) + req = Request.new(:post, "#{@base_url}/translate", body: Oj.dump(q: text, source: source_language, target: target_language, format: 'html', api_key: @api_key)) + req.add_headers('Content-Type': 'application/json') + req + end + + def transform_response(str, source_language) + json = Oj.load(str, mode: :strict) + + raise UnexpectedResponseError unless json.is_a?(Hash) + + Translation.new(text: json['translatedText'], detected_source_language: source_language) + rescue Oj::ParseError + raise UnexpectedResponseError + end +end diff --git a/app/lib/translation_service/translation.rb b/app/lib/translation_service/translation.rb new file mode 100644 index 000000000..a55b82574 --- /dev/null +++ b/app/lib/translation_service/translation.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +class TranslationService::Translation < ActiveModelSerializers::Model + attributes :text, :detected_source_language +end |