translation_service.rb 862 B

12345678910111213141516171819202122232425262728293031
  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 languages
  21. {}
  22. end
  23. def translate(_text, _source_language, _target_language)
  24. raise NotImplementedError
  25. end
  26. end