Class: Translatomatic::Translator::Yandex

Inherits:
Base
  • Object
show all
Defined in:
lib/translatomatic/translator/yandex.rb

Overview

Interface to the Yandex translation API

Instance Attribute Summary

Attributes inherited from Base

#listener

Instance Method Summary collapse

Methods inherited from Base

#name, #translate

Constructor Details

#initialize(options = {}) ⇒ Yandex

Create a new Yandex translator instance



13
14
15
16
17
# File 'lib/translatomatic/translator/yandex.rb', line 13

def initialize(options = {})
  super(options)
  @api_key = options[:yandex_api_key] || ENV["YANDEX_API_KEY"]
  raise t("translator.yandex_key_required") if @api_key.nil?
end

Instance Method Details

#languagesArray<String>

Returns A list of languages supported by this translator.

Returns:

  • (Array<String>)

    A list of languages supported by this translator.



20
21
22
23
24
25
26
27
28
# File 'lib/translatomatic/translator/yandex.rb', line 20

def languages
  @languages ||= begin
    request = Translatomatic::HTTPRequest.new(LANGUAGES_URL)
    response = request.post(key: @api_key, ui: "en")
    data = JSON.parse(response.body) || {}
    langs = data["langs"] || {}
    langs.keys.flatten.uniq
  end
end