Class: GoogleTranslate

Inherits:
Object
  • Object
show all
Defined in:
lib/google_translate/version.rb,
lib/google_translate/google_translate.rb

Constant Summary collapse

VERSION =
"1.1.1"
GOOGLE_TRANSLATE_SERVICE_URL =
"https://translate.google.com"
GOOGLE_SPEECH_SERVICE_URL =
"http://translate.google.com/translate_tts"

Instance Method Summary collapse

Instance Method Details

#say(lang, text) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/google_translate/google_translate.rb', line 35

def say lang, text
  speech_content = call_speech_service(lang, text)

  file = Tempfile.new('.google_translate_speech---')

  file.write(speech_content)

  file.close

  system "afplay #{file.path}"

  file.unlink
end

#supported_languagesObject



12
13
14
15
16
17
18
19
# File 'lib/google_translate/google_translate.rb', line 12

def supported_languages
  response = call_service GOOGLE_TRANSLATE_SERVICE_URL

  from_languages = collect_languages response.body, 0, 'sl', 'gt-sl'
  to_languages   = collect_languages response.body, 1, 'tl', 'gt-tl'

  [from_languages, to_languages]
end

#translate(from_lang, to_lang, text, options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/google_translate/google_translate.rb', line 21

def translate(from_lang, to_lang, text, options={})
  raise("Missing 'from' language") unless from_lang
  raise("Missing 'to' language") unless to_lang
  raise("Missing text for translation") unless text

  r = call_translate_service(from_lang, to_lang, text)

  result = JSON.parse(r.gsub('[,', '['))

  raise("Translate Server is down") if (!result || result.empty?)

  result
end