class Translatomatic::Provider::Yandex
Interface to the Yandex
translation API @see tech.yandex.com/translate/
Constants
- BASE_URL
- LANGUAGES_URL
- LIMIT
- TRANSLATE_URL
Public Class Methods
new(options = {})
click to toggle source
Create a new Yandex
provider instance
Calls superclass method
Translatomatic::Provider::Base::new
# File lib/translatomatic/provider/yandex.rb, line 10 def initialize(options = {}) super(options) @api_key = options[:yandex_api_key] || ENV['YANDEX_API_KEY'] raise t('provider.yandex.key_required') if @api_key.nil? end
Public Instance Methods
languages()
click to toggle source
(see Base#languages
)
# File lib/translatomatic/provider/yandex.rb, line 17 def languages @languages ||= begin response = http_client.post(LANGUAGES_URL, key: @api_key, ui: 'en') data = JSON.parse(response.body) || {} langs = data['langs'] || {} langs.keys.flatten.uniq end end
Private Instance Methods
fetch_translations(strings, from, to)
click to toggle source
# File lib/translatomatic/provider/yandex.rb, line 40 def fetch_translations(strings, from, to) body = request_body(strings, from, to) response = http_client.post(TRANSLATE_URL, body) log.debug("#{name} response: #{response.body}") data = JSON.parse(response.body) result = data['text'] || [] strings.zip(result).each do |original, translated| add_translations(original, translated) end end
perform_translate(strings, from, to)
click to toggle source
# File lib/translatomatic/provider/yandex.rb, line 33 def perform_translate(strings, from, to) batcher(strings, max_count: LIMIT[0], max_length: LIMIT[1]) .each_batch do |texts| fetch_translations(texts, from, to) end end
request_body(strings, from, to)
click to toggle source
# File lib/translatomatic/provider/yandex.rb, line 51 def request_body(strings, from, to) { key: @api_key, text: strings, lang: from.language + '-' + to.language, format: 'plain' # 'html' } end