Class: I18n::Tasks::Translators::OpenAiTranslator

Inherits:
BaseTranslator show all
Defined in:
lib/i18n/tasks/translators/openai_translator.rb

Constant Summary collapse

BATCH_SIZE =

max allowed texts per request

50
DEFAULT_SYSTEM_PROMPT =
<<~PROMPT.squish
  You are a professional translator that translates content from the %{from} locale
  to the %{to} locale in an i18n locale array.

  The array has a structured format and contains multiple strings. Your task is to translate
  each of these strings and create a new array with the translated strings.

  HTML markups (enclosed in < and > characters) must not be changed under any circumstance.
  Variables (starting with %%{ and ending with }) must not be changed under any circumstance.

  Keep in mind the context of all the strings for a more accurate translation.
PROMPT

Constants included from Logging

Logging::MUTEX, Logging::PROGRAM_NAME

Instance Method Summary collapse

Methods inherited from BaseTranslator

#translate_forest

Methods included from Logging

log_error, log_stderr, log_verbose, log_warn, program_name, warn_deprecated

Constructor Details

#initializeOpenAiTranslator

Returns a new instance of OpenAiTranslator.



23
24
25
26
27
28
29
30
# File 'lib/i18n/tasks/translators/openai_translator.rb', line 23

def initialize(*)
  begin
    require 'openai'
  rescue LoadError
    raise ::I18n::Tasks::CommandError, "Add gem 'ruby-openai' to your Gemfile to use this command"
  end
  super
end

Instance Method Details

#no_results_error_messageObject



47
48
49
# File 'lib/i18n/tasks/translators/openai_translator.rb', line 47

def no_results_error_message
  I18n.t('i18n_tasks.openai_translate.errors.no_results')
end

#options_for_htmlObject



39
40
41
# File 'lib/i18n/tasks/translators/openai_translator.rb', line 39

def options_for_html
  {}
end

#options_for_plainObject



43
44
45
# File 'lib/i18n/tasks/translators/openai_translator.rb', line 43

def options_for_plain
  {}
end

#options_for_translate_values(from:, to:, **options) ⇒ Object



32
33
34
35
36
37
# File 'lib/i18n/tasks/translators/openai_translator.rb', line 32

def options_for_translate_values(from:, to:, **options)
  options.merge(
    from: from,
    to: to
  )
end