translation_service.rb 832 B

123456789101112131415161718192021222324252627
  1. # frozen_string_literal: true
  2. class TranslationService
  3. class Error < StandardError; end
  4. class NotConfiguredError < Error; end
  5. class TooManyRequestsError < Error; end
  6. class QuotaExceededError < Error; end
  7. class UnexpectedResponseError < Error; end
  8. def self.configured
  9. if ENV['DEEPL_API_KEY'].present?
  10. TranslationService::DeepL.new(ENV.fetch('DEEPL_PLAN', 'free'), ENV['DEEPL_API_KEY'])
  11. elsif ENV['LIBRE_TRANSLATE_ENDPOINT'].present?
  12. TranslationService::LibreTranslate.new(ENV['LIBRE_TRANSLATE_ENDPOINT'], ENV['LIBRE_TRANSLATE_API_KEY'])
  13. else
  14. raise NotConfiguredError
  15. end
  16. end
  17. def self.configured?
  18. ENV['DEEPL_API_KEY'].present? || ENV['LIBRE_TRANSLATE_ENDPOINT'].present?
  19. end
  20. def translate(_text, _source_language, _target_language)
  21. raise NotImplementedError
  22. end
  23. end