class I18n::Tasks::Translators::YandexTranslator

Public Class Methods

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

Protected Instance Methods

no_results_error_message() click to toggle source
# File lib/i18n/tasks/translators/yandex_translator.rb, line 37
def no_results_error_message
  I18n.t('i18n_tasks.yandex_translate.errors.no_results')
end
options_for_html() click to toggle source
# File lib/i18n/tasks/translators/yandex_translator.rb, line 29
def options_for_html
  { format: 'html' }
end
options_for_plain() click to toggle source
# File lib/i18n/tasks/translators/yandex_translator.rb, line 33
def options_for_plain
  { format: 'plain' }
end
options_for_translate_values(from:, to:, **options) click to toggle source
# File lib/i18n/tasks/translators/yandex_translator.rb, line 22
def options_for_translate_values(from:, to:, **options)
  options.merge(
    from: to_yandex_compatible_locale(from),
    to: to_yandex_compatible_locale(to)
  )
end
translate_values(list, **options) click to toggle source
# File lib/i18n/tasks/translators/yandex_translator.rb, line 18
def translate_values(list, **options)
  list.map { |item| translator.translate(item, options) }
end

Private Instance Methods

api_key() click to toggle source
# File lib/i18n/tasks/translators/yandex_translator.rb, line 54
def api_key
  @api_key ||= begin
    key = @i18n_tasks.translation_config[:yandex_api_key]
    fail ::I18n::Tasks::CommandError, I18n.t('i18n_tasks.yandex_translate.errors.no_api_key') if key.blank?

    key
  end
end
to_yandex_compatible_locale(locale) click to toggle source

Convert 'es-ES' to 'es'

# File lib/i18n/tasks/translators/yandex_translator.rb, line 44
def to_yandex_compatible_locale(locale)
  return locale unless locale.include?('-')

  locale.split('-', 2).first
end
translator() click to toggle source
# File lib/i18n/tasks/translators/yandex_translator.rb, line 50
def translator
  @translator ||= Yandex::Translator.new(api_key)
end