Class: TranslationAPI::Provider::DeepL

Inherits:
Object
  • Object
show all
Defined in:
lib/translation_api/provider/deepl.rb

Constant Summary collapse

SYSTEM_PROMPT_BASE =
"Keep symbols\n"
API_KEY_ERROR_MESSAGE =
"API key is not found."
LANGUAGE_UNSUPPORTED_MESSAGE =
"This language is unsupported by DeepL."

Instance Method Summary collapse

Constructor Details

#initialize(pro:, except_words: [], language: "japanese") ⇒ DeepL

Returns a new instance of DeepL.



17
18
19
20
21
22
23
24
25
26
# File 'lib/translation_api/provider/deepl.rb', line 17

def initialize(pro:, except_words: [], language: "japanese")
  @pro = pro
  @language = language

  setup_deepl_config!
  validate_supported_language!

  @system_content = SYSTEM_PROMPT_BASE + except_option_text(except_words)
  @language = supported_languages[language.to_sym]
end

Instance Method Details

#translate(text) ⇒ Object



28
29
30
31
32
# File 'lib/translation_api/provider/deepl.rb', line 28

def translate(text)
  return text if text.strip.empty?

  ::DeepL.translate(text, nil, @language, context: @system_content).text
end