class I18n::Tasks::Translators::DeeplTranslator

Public Class Methods

new(*) click to toggle source
# File lib/i18n/tasks/translators/deepl_translator.rb, line 7
def initialize(*)
  begin
    require 'deepl'
  rescue LoadError
    raise ::I18n::Tasks::CommandError, "Add gem 'deepl-rb' to your Gemfile to use this command"
  end
  super
  configure_api_key!
end

Protected Instance Methods

no_results_error_message() click to toggle source
# File lib/i18n/tasks/translators/deepl_translator.rb, line 57
def no_results_error_message
  I18n.t('i18n_tasks.deepl_translate.errors.no_results')
end
options_for_html() click to toggle source
# File lib/i18n/tasks/translators/deepl_translator.rb, line 32
def options_for_html
  { tag_handling: 'xml' }
end
options_for_plain() click to toggle source
# File lib/i18n/tasks/translators/deepl_translator.rb, line 36
def options_for_plain
  { preserve_formatting: true }
end
options_for_translate_values(**options) click to toggle source
# File lib/i18n/tasks/translators/deepl_translator.rb, line 28
def options_for_translate_values(**options)
  { ignore_tags: %w[i18n] }.merge(options)
end
replace_interpolations(value) click to toggle source

@param [String] value @return [String] 'hello, %{name}' => 'hello, <i18n>%{name}</i18n>'

# File lib/i18n/tasks/translators/deepl_translator.rb, line 42
def replace_interpolations(value)
  value.gsub(INTERPOLATION_KEY_RE, '<i18n>\0</i18n>')
end
restore_interpolations(untranslated, translated) click to toggle source

@param [String] untranslated @param [String] translated @return [String] 'hello, <i18n>%{name}</i18n>' => 'hello, %{name}'

# File lib/i18n/tasks/translators/deepl_translator.rb, line 49
def restore_interpolations(untranslated, translated)
  return translated if untranslated !~ INTERPOLATION_KEY_RE

  translated.gsub(%r{</?i18n>}, '')
rescue StandardError => e
  raise_interpolation_error(untranslated, translated, e)
end
translate_values(list, from:, to:, **options) click to toggle source
# File lib/i18n/tasks/translators/deepl_translator.rb, line 19
def translate_values(list, from:, to:, **options)
  result = DeepL.translate(list, to_deepl_compatible_locale(from), to_deepl_compatible_locale(to), options)
  if result.is_a?(DeepL::Resources::Text)
    [result.text]
  else
    result.map(&:text)
  end
end

Private Instance Methods

configure_api_key!() click to toggle source
# File lib/i18n/tasks/translators/deepl_translator.rb, line 68
def configure_api_key!
  api_key = @i18n_tasks.translation_config[:deepl_api_key]
  host = @i18n_tasks.translation_config[:deepl_host]
  version = @i18n_tasks.translation_config[:deepl_version]
  fail ::I18n::Tasks::CommandError, I18n.t('i18n_tasks.deepl_translate.errors.no_api_key') if api_key.blank?

  DeepL.configure { |config|
    config.auth_key = api_key
    config.host = host unless host.blank?
    config.version = version unless version.blank?
  }
end
to_deepl_compatible_locale(locale) click to toggle source

Convert 'es-ES' to 'ES'

# File lib/i18n/tasks/translators/deepl_translator.rb, line 64
def to_deepl_compatible_locale(locale)
  locale.to_s.split('-', 2).first.upcase
end