Class: Youdao::Translate

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Singleton
Defined in:
lib/youdao/translate.rb,
lib/youdao/translate/version.rb

Constant Summary collapse

URL =
'https://aidemo.youdao.com/trans'
VERSION =
'0.2.4'

Instance Method Summary collapse

Instance Method Details

#get(content, options = {}) ⇒ Object

Get a tranlsated string

Examples:

Youdao::Translate.get("hello")

Youdao::Translate.get("hello", from: "zh-CHS", to: "ja")

Youdao::Translate.get("hello", to: "ko")

Youdao::Translate.instance.get("hello")

Youdao::Translate.instance.get("hello", from: "zh-CHS", to: "ja")

Parameters:

  • content (String)

    the content you want to translate

  • from (String)

    the language from

  • to (String)

    the language translate to



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/youdao/translate.rb', line 42

def get(content, options = {})
  @content = content

  form = payload.merge(options)

  res = HTTP.post(URL, form: form)
  result = res.parse :json

  result['translation'].first
rescue StandardError
  nil
end